How to instantiate different versions of InternetExplorerDriver - Selenium 2? - testing

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?

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}}}";

Could anyone explain use of DesiredCapabilities class in Selenium Webdriver with an example?

Could someone explain to me what is the use of DesiredCapabilities in Selenium Webdriver with an example?
I am confused with setting a profile and using the DesiredCapabilities.
You, as a user of WebDriver, have the flexibility to create a session for a browser with your own set of desired capabilities that a browser should or shouldn't have. Using the capabilities feature in WebDriver, you are given a way to specify your choice of how your browser should behave.
Some of the examples of browser capabilities include enabling a browser session to support taking screenshots of the webpage, executing custom JavaScript on the webpage, enabling the browser session to interact with window alerts, and so on.
There are many capabilities that are specific to individual browsers, but there are some specific capabilities that are generic to all the browsers. We will discuss some of them here, and the remaining, as and when we come across those features in this book. The browser-specific capabilities will be discussed in greater detail in the next chapter.
Capabilities is an interface in the WebDriver library whose direct implementation is the DesiredCapabilities class. The series of steps involved in creating a browser session with specific capabilities is as follows:
Identify all of the capabilities that you want to arm your browser with.
Create a DesiredCapabilities class instance and set all of the capabilities to it.
Now, create an instance of WebDriver with all of the above capabilities passed to it.
This will create an instance of Firefox/IE/Chrome or whichever browser you have instantiated with all of your desired capabilities.
Let's create an instance of FirefoxDriver while enabling the takesScreenShot capability:
public class BrowserCapabilities {
public static void main(String... args) {
Map capabilitiesMap = new HashMap();
capabilitiesMap.put("takesScreenShot", true);
DesiredCapabilities capabilities
= new DesiredCapabilities(capabilitiesMap);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
}
}
In the preceding code, we set all of the capabilities that we desire in a map and created an instance of DesiredCapabilities using that map. Now, we have created an instance of FirefoxDriver with these capabilities. This will now launch a Firefox browser that will have support for taking screenshots of the webpage. If you see the definition of the DesiredCapabilities class, the constructor of the class is overloaded in many different ways. Passing a map is one of them. You can use the default constructor and create an instance of the DesiredCapabilities class, and then set the capabilities using the setCapability() method.
Some of the default capabilities that are common across browsers are shown in the following table:
Capability
What it is used for
takesScreenShot
Tells whether the browser session can take a screenshot of the webpage
handlesAlert
Tells whether the browser session can handle modal dialogs
cssSelectorsEnabled
Tells whether the browser session can use CSS selectors while searching for elements
javascriptEnabled
Enables/disables user-supplied JavaScript execution in the context of the webpage
acceptSSLCerts
Enables/disables the browser to accept all of the SSL certificates by default
webStorageEnabled
This is an HTML5 feature, and it is possible to enable or disable the browser session to interact with storage objects
There are many other capabilities of WebDriver.
Source: Book "Selenium WebDriver Practical Guide" by Satya Avasarala
Desired capability is a series of key/value pairs that stores browser properties like browser name, versioN and the path of the browser driver in the system, etc. to determine the behaviour of the browser at run time.
It can also be used to configure the driver instance of Selenium WebDriver
like FirefoxDriver, ChromeDriver, InternetExplorerDriver.
An Example:
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.ie.InternetExplorerDriver;
importorg.openqa.selenium.remote.DesiredCapabilities;
public class IEtestforDesiredCapabilities {
public static void main(String[] args) {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "IE");
capabilities.setCapability(InternetExplorerDriver.
INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
System.setProperty("webdriver.ie.driver", "Put IEDriverServer path here");
WebDriver driver = newInternetExplorerDriver(capabilities);
driver.manage().window().maximize();
driver.get("http://www.yahoo.com");
driver.quit();
}
}

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

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

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);

WebDriver - is it possible to use Firefox or IE javascript engine when using HtmlUnitDriver using .NET Selenium bindings?

I am running into inconsistent behaviour when running my test using DesiredCapabilities.Firefox() vs. DesiredCapabilities.HtmlUnitWithJavaScript().
For the most part, my issue lies with DOM elements not appearing on the page when they should be there.
I've used a number of different strategies for "Wait"-ing for said element, but still, HtmlUnit driver seems to think that the element is not visible.
With that said, I am now asking if it's possible to run HtmlUnit providing a Firefox or IE handle; similar to how Java's HtmlUnitDriver implementation works:
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
try WebDriver driver = new HtmlUnitDriver(DesiredCapabilities.firefox());