In web world users use different browsers such as Chrome, Firefox, Internet Explorer, Opera, and Safari and so on. These browsers have different behaviors and features. Thus, we should verify web applications functionalities are working as expected for all browsers and this testing activity is called cross browser testing. Selenium webdriver supports HtmlUnit Driver, Firefox Driver, Chrome Driver, Opera Driver, MS Edge Driver, MS Internet Explorer Driver, Safari Driver, GhostDriver (PhamtomJS) and so on.

Selenium webdriver also provides us Remote Webdriver. There are some services such as Sauce Labs or Browser Stack that present us all browsers and their versions in their servers. In order to connect those servers and test web applications with different browsers on the cloud, we use Remote Webdriver. Also, Remote Webdriver is being used for Grid implementations. In another post, I will explain how to use Remote Webdriver and how to test on the cloud.

Due to nature of diversity of browsers we face with different browser behaviors. In order to cover all browsers while we are automating a website, we should think different synchronizations, different startup procedures, and so on.

FireFox Driver

Firefox driver is the built-in driver so you do not need to do for installation. Its startup period is relatively slow.

Declaration: driver = new FirefoxDriver();

Firefox Driver Profile

Profiles provides us to declare driver with extra capabilities and configurations.

Profile Declaration

FirefoxProfile profile = new FirefoxProfile();

Webdriver driver = new FirefoxDriver(profile);

How to use Profiles?

1) Profile Level Methods (Example: Enable NativeEvents)

NativeEvents is disabled by default. In WebDriver, advanced user interactions are provided by either simulating the Javascript events directly (i.e. synthetic events) or by letting the browser generate the Javascript events (i.e. native events). Native events simulate the user interactions better whereas synthetic events are platform independent, which can be important in Linux when alternative window managers are used. Native events should be used whenever it is possible.

[1]

FirefoxProfile profile = new FirefoxProfile();

profile.setEnableNativeEvents(true);

WebDriver driver = new FirefoxDriver(profile);

2) Browser Preferences (Example: Set Language)

profile.setPreference( “intl.accept_languages”, “no,en-us,en” );

3) Load Addons (Example: Load Firebug)

Firefox driver is loaded without extensions/addons by default. Sometimes we need these addons such as firebug for debugging purposes while our test script is running. In order to fix this problem, we can load addons by using their XPI files and webdriver profiles. An example code is shown below.

Test Result:

cross browser

Desired Capabilities

DesiredCapabilities provides to set properties for the WebDriver such as BrowserName, Platform, Version, etc. We can configure all driver instance like FirefoxDriver, ChromeDriver, InternetExplorerDriver using desired capabilities. It is very useful when we are using RemoteWebdriver to test on the cloud by using Browserstack or Saucelabs. Also, we can set a manual proxy with DesiredCapabilities. This temporarily changes the system’s proxy settings and when the test finishes, it will change the system to the original state. [2]

I will show you a proxy test example but before run this proxy test code you should install Burp Suite and set localhost:8080 (127.0.0.1:8080) as a proxy as shown below.

cross-browser-2

Proxy Example:

Test Result:
After running above test code, you will see the GET methods as shown below.

cross_browser_1

HTMLUnit Driver

It is developed by sourgeforge.net (http://htmlunit.sourceforge.net/)

HtmlUnit is a “GUI-Less browser for Java programs”. It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc… just like you do in your “normal” browser.

It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating Chrome, Firefox or Internet Explorer depending on the configuration used. [3]

It is faster but I recommend to use HTMLUnit Driver for basic automation scenarios.

Ghost Driver (PhantomJS)

It is a headless driver, it means when you run it, you won’t see a browser window but you can do everything related web testing. In order to use GhostDriver, you should add below maven dependency into your project. (link: phantomjsdriver 1.2.1)

And you must download PhantomJS executable from http://phantomjs.org/download.html and place it under your project directory and declare it in your test automation code.

A sample example is shown below.

You can find detailed information about PhantomJS on this page https://github.com/detro/ghostdriver

Chrome Driver

ChromeDriver is a standalone server which implements WebDriver’s wire protocol for Chromium. ChromeDriver is available for Chrome on Android and Chrome on Desktop (Mac, Linux, Windows and ChromeOS). [4]

In order to use ChromeDriver, you should specify its location via the webdriver.chrome.driver system property. The sample test code for chrome driver is shown below.

Problem & Solution: If you get java.lang.NoClassDefFoundError: org/openqa/selenium/remote/service/DriverService$Builder error, then you should add below dependency into your pom.xml.   After that that problem will dissepear. I solved this problem in this way.

Note: You should change version with the latest Selenium Webdriver version.

For extra information about Chromium Command Line Switches you can visit http://peter.sh/experiments/chromium-command-line-switches/

IE Driver

First you have to download IEdriver executable file from here: http://www.seleniumhq.org/download/

Then we should set the “webdriver.ie.driver” to the location of the driver executable and declare IEdriver as follows.

Declaration: Driver = new InternetExplorerDriver();

Important Note!

Problem: When I tried to do test with IEdriver I got OpenQA.Selenium.NoSuchWindowException : Unable to get browser” problem.

Solution: I solved this problem to set all security zones to the same Protected Mode setting.

Setting the Local Intranet zone’s Enable Protected Mode setting to true solved the problem

  1. Select Tools > Internet Options and go to the Security tab.
  2. Select each zone (Internet, Local intranet, Trusted sites, Restricted sites) and check the Enable Protected Mode check box.

Also, if above solution doesn’t work, then try to add the test domain to the list of “Trusted Sites”. This may solve this problem. You can do this on the Security tab of Internet Options.

Reference: http://www.michael-whelan.net/selenium-webdriver-and-ie11/

Test Code:

MS Edge Driver

If you are using Windows 10 and if you need to do test automation with MS Edge browser, you should do this in this way.

First you have to download MS Edge Driver from https://www.microsoft.com/en-us/download/details.aspx?id=48212

Then, you should set driver path, declare a EdgeDriver and use it as follows:

There is a nice article about MS Edge Driver here: http://afourtech.com/automation-testing-on-microsoft-edge-browser-using-selenium-web-driver/

Opera Driver

Important Note: The OperaDriver has been deprecated so now the maximum Selenium WebDriver version that it supports is 2.34.0, and it supports a maximum of Opera version 12.17.

You can find information and examples of operachromiumdriver example here: https://github.com/operasoftware/operachromiumdriver

Safari Driver

SafariDriver on Windows will most likely not work on newer versions of Safari and/or Windows because of Apple’s decision to drop Safari on Windows.

For more information and examples, you can check here: https://github.com/SeleniumHQ/selenium/wiki/SafariDriver

Common Problems of Cross Browser Testing

While doing cross browser testing, you may face with problems and fails. Different browsers have different driver implementations and these drivers have different behaviors, timing issues, selector differences, implementations bugs, driver implementation scope is not complete (some functionalities are missing), and so on. When you get this kind of errors, I suggest you to read the driver log and search this problem on the internet. Also if you are setting up a new test automation framework, try to implement cross browser testing as early as possible and see the problems at early phases. You can solve your problems easily in early phases of you test automation framework.

References

https://code.google.com/p/selenium/wiki/AdvancedUserInteractions [1]

https://code.google.com/p/selenium/wiki/DesiredCapabilities [2]

http://htmlunit.sourceforge.net/ [3]

https://sites.google.com/a/chromium.org/chromedriver/ [4]