Can WebDriver replace ChromeDriver in order to make Selenium tests work in all browsers - selenium

I'm automating my tests using TestNG and Java:
Can WebDriver replace ChromeDriver in order to make our tests work in all browsers such as Chrome, Firefox, Mozilla, Safari, Opera ... ?
How should we configure the browser so as to use the same code for all browsers?

Essentially, you just new to instantiate a different class derived from RemoteWebDriver depending on the browser you are testing.
e.g.
void GetWebDriver(String browserName) {
if (CHROME.equals(browserName))
return new ChromeDriver(capability);
else if (FIREFOX.equals(browserName))
return new FirefoxDriver(capability);
else if (EDGE.equals(browserName))
return new EdgeDriver(capability);
else if (INTERNET_EXPLORER.equals(browserName))
return new InternetExplorerDriver(capability);
else if (OPERA.equals(browserName))
return new OperaDriver(capability);
else if (SAFARI.equals(browserName))
return new SafariDriver(capability);
}
I suggest you look into this githob project: https://github.com/sebarmeli/Selenium2-Java-QuickStart-Archetype
Specifically, the WebDriverFactory.java file.

The easiest way to run your code in different browsers to use Selenium Grid and RemoteWebDriver. You can find the doc on the following link:
https://github.com/SeleniumHQ/selenium/wiki/Grid2

Related

How Can I set chrome browser to automatically download a pdf using QAF and WebDriverManager

Using a datasheet, I usually pass a browser name into a class I created to select which browser I want to run my tests from. Recently, I've been working on an app in which I need to download a PDF and then verify its contents. I have everything working successfully other than downloading the PDF. With WebDriverManager, it creates a browser profile every time a test runs, and so, I need to update chromeOptions to download PDFs automatically before at the start of the script.
Here is the code that I already have. I just need help with what to put in the prefs for this to work. -
public static void selectBrowser(String strBrowser) {
switch (strBrowser) {
case "Chrome":
String chromePrefs = "{'goog:chromeOptions':{'prefs':{'profile.default_content_settings.popups':0}}}";
ConfigurationManager.getBundle().setProperty("chrome.additional.capabilities", chromePrefs);
TestBaseProvider.instance().get().setDriver("chromeDriver");
Reporter.log("Chrome Browser was set", MessageTypes.Info);
break;
}
}
After researching for hours, finally found that I needed to use the plugins.always_open_pdf_externally preference. Here is the code for anyone who might need it.
Note, the "goog:" before chromeOptions is necessary since I have WebDriverManager enabled. With 3rd party driver managers, we need to put "goog:" before chromeOptions for it to work.
You can simply put it in the application.properties like this -
chrome.additional.capabilities={"goog:chromeOptions":{"args":[--disable-extensions],"prefs":{"plugins.always_open_pdf_externally":true}}}
or you can put it in the code I have up top like this
String chromePrefs = "{'goog:chromeOptions':{'args':[],'prefs':{\"plugins.always_open_pdf_externally\":true}}}";

what is this driver = WebDriverManager.startDriver(browser, useragent) means?

For this line of code in Selenium:
driver = WebDriverManager.startDriver(browser, useragent)
where
browser = context.getCurrentXmlTest().getParameter("browser"); and
useragent = context.getCurrentXmlTest().getParameter(useragent);
Does anybody know what this line is doing? And where do we use WebDriver Manager?
I assume the "where", "and" stuff is just setting the parameters for that function, cucumber type coding. So you would be pulling those parameters from some kind of context configuration.
It looks like WebDriverManager helps you set up the type of driver you want. Making it easy to change from firefox, chrome, IE by hiding the configuration into that class.
ITestContext is interface in TestNG which helps here, to get current Test which is under execution and fetching variables provided for that test. If i want to say in Java prospective, lets say you provided variable 'browser' with value as 'chrome' in testng.xml file for this test. This peace of code
browser = context.getCurrentXmlTest().getParameter("browser");
get that variable value 'chrome' and assigning this to 'browser' variable in this class.
Regarding WebDriverManager, i am not yet used but for code provide it looks like peace of code or library which helps you start driver. To start the driver you are passing which driver need to start like chrome, firefox etc.. For example as here browser value is chrome, so its instantiate ChromeDriver nothing but opens chrome browser and continues execution. I hope you need to use this code to start the driver in normally as replacement of driver=new soandsodriver();
Thank You,
Murali

Multiple Browser WebDriver Selenium

I am working on a Selenium test project where I need to launch two browsers at initial setup .
Then I need to do switching between these browsers.
So I will have [Window1] [Window2]
I would like to run test through [Window1] and then switch to [Window2] to check result of actions done in [Window1]
Any idea on how to do it?
I tried driver.switchTo().window() but no luck.
Any help would be greatly appreciated. Thanks
driver.switchTo().window() will work only if new window is opened by any action in existing window. If you are using different drivers to open different windows then it wont work.
In such case you need to choose appropriate instance of driver to control the new window.
Suppose you have instance of webdriver
// Window 1
WebDriver chrome = new ChromeDriver()
// Window 2
WebDriver firefox = new FirefoxDriver()
Now use chrome whenever you want to interact with Window 1 and use firefox to interact with Window 2.
Just use two driver instancess:
WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new FirefoxDriver()
You can make them both same flavour if you want.
You need to pass the parameter as window name or you can get all the window handles and then switch to the particular window handle.
You could use:
driver.switchTo().window("windowName");
or:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

How to run Selenium system tests without requiring to open the browser?

I have a test method created using Selenium, something similar to this:
[TestFixture]
public class Test_Google
{
IWebDriver driver;
[SetUp]
public void Setup()
{
driver = new InternetExplorerDriver();
}
[TearDown]
public void Teardown()
{
driver.Quit();
}
[Test]
public void TestSearchGoogleForTheAutomatedTester()
{
//Navigate to the site
driver.Navigate().GoToUrl("http://www.google.co.uk");
//Find the Element and create an object so we can use it
IWebElement queryBox = driver.FindElement(By.Name("q"));
//Work with the Element that's on the page
queryBox.SendKeys("The Automated Tester");
queryBox.SendKeys(Keys.ArrowDown);
queryBox.Submit();
//Check that the Title is what we are expecting
Assert.True(driver.Title.IndexOf("The Automated Tester") > -1);
}
}
When the test runs, it opens an IE and carries out its test.
Imagine there are 200 test methods like this spread across multiple test fixtures, which means IE has to be opened and closed many times (as many as test fixtures since 1 browser will be opened per test fixture).
How to run Selenium system tests without requiring to open the browser?
I mean for example I was thinking it might be possible to develop a windows service to run the Selenium tests in the WinForms Web Browser Control, in which case the browser doesn't have to be opened each time and the tests can run automatically and seemlessly. Not sure how to implement this though?
Or is there any other better known way?
Thanks,
No. Selenium is written in JavaScript; it's designed to run in a real browser to test compatibility with a real browser. There are a variety of other tools out there designed to run tests that simulate a browser; you can look into HtmlUnit or Canoo WebTest.
Hope this will help you.
Have you tried XLT?
It doesnt require an open browser at all
http://www.xceptance.com/products/xlt/what-is-xlt.html
You can run all your tests in one browser instance if you like. You just have to pass your webdriver instance to each test. Like having a singelton WebDriver in a static class from where all your testcases can access the WebDriver. Works fine and is usefull if you got a session to keep
driver = new HtmlUnitDriver(true);
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

How to instantiate different versions of InternetExplorerDriver - Selenium 2?

just wondering how I can instantiate different versions of InternetExplorerDriver.
That's how I can create a IE driver:
WebDriver ieWebDriver = new InternetExplorerDriver();
but I am not able to differentiate between IE6, IE7, IE8 and IE9.
Cheers,
Yes, you can. DesiredCapabilities have a public method which you can use:
this.SetCapability(CapabilityType.BrowserName, "internet explorer");
this.SetCapability(CapabilityType.Version, "8");
this.SetCapability(CapabilityType.Platform, "WINDOWS");
I've written extension methods to make it easier to instantiate any version by this call:
DesiredCapabilities internetExplorer8 =
DesiredCapabilities.InternetExplorer().SetVersion("8");
IWebDriver webDriver = new RemoteWebDriver(seleniumHubUrl, internetExplorer8);
This really makes sense if you use RemoteWebDriver and have a Selenium2 Grid/Hub set up with multiple nodes, e.g. multiple virtual machines each having a different version of Internet Explorer and each being a node connected to the hub.
And the extension:
public static class DesiredCapabilitiesExtension
{
public static DesiredCapabilities SetBrowserName(this DesiredCapabilities desiredCapabilities, string browserName)
{
// make sure the browser name is lowercase
desiredCapabilities.SetCapability(CapabilityType.BrowserName, browserName.ToLowerInvariant());
return desiredCapabilities;
}
public static DesiredCapabilities SetVersion(this DesiredCapabilities desiredCapabilities, string version)
{
desiredCapabilities.SetCapability(CapabilityType.Version, version);
return desiredCapabilities;
}
public static DesiredCapabilities SetPlatform(this DesiredCapabilities desiredCapabilities, string platform)
{
// make sure the platform is case sensitive, uppercase to make it work
desiredCapabilities.SetCapability(CapabilityType.Platform, platform.ToUpperInvariant());
return desiredCapabilities;
}
}
Windows only supports installing a single IE version. Although some hacks exist to run multiple versions, I'm pretty sure you won't get them working with WebDriver (although I'd love to be proven wrong).
In your shoes, I would probably set up a Windows VM for each version you want to test and use RemoteWebDriver to talk to them.
To instantiate different versions, you can set the version using capability.setVersion to the required version number. At the same time, while starting the node, you need to add the following parameters in the command line:
-browser "browserName=internet explorer,maxInstances=5,platform=WINDOWS, version=8"
For supporting multiple versions at the same node, you can use "-browser" multiple times.
However, the latest IE supports "browser mode" - just press F12 and choose the browsing mode.
AFAIK it works quite well - at least compared to IE8 and IE7.
I'm curious if it can be accessed by javascript and changed automatically in Selenium?