Run tests on multiple browsers

Running tests on multiple browsers helps ensure that the behavior and look of your application is consistent for all your users. Selenium offers the possibility to use most common browsers to run your tests against. However, if your application needs to run also on mobile devices, from within their browsers (not from within native applications that is), there is an easy way to simulate the mobile devices your users would open your website on. You can use Chrome ‘s user agent capabilities. Also, you can run tests on your regular browsers by having some extensions active in the browser session (you might need some additional actions to be performed with the help of these extensions).

What would be ideal to do is create a class, a single point of browser instantiation, from where you would initialize the browser you want to run your tests on. Here, you could have a method that, based on a parameter provided to it, would instantiate the browser having the same name as the parameter’s value. To do this, it would call another method that deals only with initializing the specified browser. Within the latter method, the size of the browser window should also be set. For example, for regular browsers, it would be recommended to open these up in a maximized state, so that other interaction with the windows (outside of tests) would not let them in a poorly sized state. For mobile device browsers, the window should be resized to the size of the actual device it simulates. See below for examples.

Instantiate Firefox

Instantiating a plain Firefox session is very easy:

 public WebDriver initializeFirefoxDriver() {
 WebDriver driver = new FirefoxDriver();
 return driver;

The ‘driver.manager().window().maximize()’ line of code simply sets the window to its’ maximum size, so that it fits totally in your computer’s screen.

Instantiate Chrome

To instantiate a plain Chrome browser, the code below will do the trick:

public WebDriver initializeChromeDriver() {
 System.setProperty("", "src/test/resources/browserBinaries/chromedriver.exe");
 WebDriver driver = new ChromeDriver();
 return driver;

Here, you will need to specify the location of the Chrome driver file that you have attached to your project, since this is the file that runs the browser session (not the Chrome installation you have on your computer, if you installed Chrome as one of your browsers). The window will be set to its’ maximum size after being initialized.

Instantiate Safari

To initialize a Safari driver, on Windows and iOS only, the following method can be used:

 public WebDriver initializeSafariDriver() {
 WebDriver driver = new SafariDriver();
 return driver;

Instantiate Firefox with an extension

If you want to use Firefox, but with an added extension that can perform some actions needed in your tests, first you will need to download the extension in the ‘xpi’ format and attach it to your project (in the same location where the browser binaries are placed, if there are any). The code below is used to first specify the location of the extension to be applied, and then to open the Firefox driver having the extension active.

 public WebDriver initializeFirefoxWithExtension() {
 File file = new File("src/test/resources/browserBinaries/extensionName.xpi");
 FirefoxProfile firefoxProfile = new FirefoxProfile();
 try {
 } catch (IOException e) {

 WebDriver driver = new FirefoxDriver(firefoxProfile);
 return driver;

Instantiate Chrome with an extension

To instantiate a Chrome driver instance,  having an extension active, you need to first download the extension in a .crx format, and then apply it to the driver instance, as shown below:

 public WebDriver initializeChromeWithExtension() {
 ChromeOptions options = new ChromeOptions();
 options.addExtensions(new File("/src/test/resources/extensionName.crx"));
 DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
 ChromeDriver driver = new ChromeDriver(capabilities);
 return driver;

 Instantiate a mobile browser

To simulate a mobile device’s browser, you first need to decide which mobile device you will need to simulate. Then, perform a search on the internet that contains the mobile device’s name and the words ‘user agent’. You will see some results that will take to a page that has ‘wurfl’ in its’ name. Wurfl is a library that holds a large number of browser user agents. A user agent is simply a string that identifies the browser and operating system on which the browser runs.  After finding the appropriate user agent, instantiate the browser with the following code (that adds the appropriate user agent):

public WebDriver initializeSomeDevice() {
 System.setProperty("", "src/test/resources/browserBinaries/chromedriver");
 ChromeOptions chromeOptions = new ChromeOptions();
 WebDriver driver = new ChromeDriver(chromeOptions);
 driver.manage().window().setSize(new Dimension(width, height));
 return driver;

For example, to simulate a Samsung Galaxy S3, the following line should declare the correct user agent:

chromeOptions.addArguments("--user-agent=Mozilla/5.0 (Linux; U; Android 4.0; xx-xx; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");

The appropriate window size for this particular device is 1280×720, so the dimension line of code would look like:

  driver.manage().window().setSize(new Dimension(720, 1280));

Putting it all together

The method that would take as parameter the name of the browser to be instantiated would look something like:

public WebDriver initializeSpecifiedDriver(String driverName) {
 WebDriver driver = null;
 switch (driverName) {
 case "Firefox": driver = initializeFirefoxDriver();
 case "Chrome": driver = initializeChromeDriver();
 case "S3" : driver = initializeSamsungGalaxyS3();
 case "Firebug": driver = initializeFirefoxWithFirebug();
 default: driver = initializeChromeDriver();
 return driver;

Here, depending on the value of the supplied parameter, the appropriate instantiation method is called. In case the parameter has an invalid value (one that is not specified as a value in any ‘case’ element from this code), the ‘default’ code is going to be run, which will simply instantiate a Chrome driver in this case (so as not to throw an exception).

Initializing and destroying a browser session

Within a test class, the browser should only be instantiated once, before any test methods are run. This makes tests run faster (as opposed to starting the browser before each test). Instantiating a browser means creating a blank browser session, without any cache or cookies. To make sure the session remains clean before each test method runs (in case this is the desired behavior), before each method run the cookies should be deleted. This can be done by using the @BeforeClass and @BeforeMethod annotations from TestNG:

  • in @BeforeClass – ‘driver = initializationClassName.initializeSpecifiedDriver(driverName)’
  • in @BeforeMethod – ‘driver.manage().deleteAllCookies()’

This ensures the browser starts only once per test, however each test is sure to have a clean browser session.

After all the tests have run, a cleanup is necessary , which means the browser instance must be destroyed. Keeping in mind that more than one session can be active at a time (if a test clicked a link that opened another browser, for example), in the @AfterClass method, the browser will be closed by calling the ‘driver.quit()’ method.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s