Interacting with the browser | Running test on different browsers

Learn how to execute the same Selenium script on different browsers like Mozilla Firefox, Apple Safari, Microsoft Edge, and Internet Explorer.

5 min read
  • What are browser drivers?
  • Execute a script in Google Chrome browser
  • Execute a script in Mozilla Firefox browser
  • Troubleshooting
  • Execute a script in Internet Explorer execution
  • Prerequisites for IE
  • Sample code to do cross-browser execution

In the Previous Tutorial, we learned to handle popup windows and alert dialogues.

Some of our readers must be wondering, in all previous tutorials, we are running test in Chrome browser only. What if we want to execute the same script on different browsers like Mozilla Firefox, Apple Safari, Microsoft Edge, Internet Explorer etc? In order to run the test in any browser, we need the driver for that specific browser.

What are browser drivers?

Well, if you can remember in the Previous Tutorial we downloaded a bunch of Selenium dependencies like ChromeDriver, GeckoDriver and IEDriverServer.

WebDriver APIs communicate with the browser drivers in WebDriver JSONWire protocol and the browser drivers, in turn, convey the message to the respective browsers through Websocket connection type protocols like DevTools protocol for Chrome. Till Selenium 3 this is how communication used to happen. With Selenium 4, the WebDriver protocol has become a W3C standard so the browser driver will talk to the browsers in WebDriver protocol.

If you did not do that please go through This Tutorial and download all browser drivers before following this tutorial.

Execute a script in Google Chrome browser

Instantiate ChromeDriver and specify the location of chromedriver.

String chromeDriverPath = "/Users/username/Desktop/chromedriver";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);

WebDriver driver = new ChromeDriver();

In the above example, we are assuming chromedriver is on the Desktop of a Mac user. Please change the path accordingly as per your environment.

That is the only change we need. All further lines of codes will be the same for all browsers after driver instantiation.

driver.get("https://cosmocode.io/automation-practice");
WebElement inpFirstName = driver.findElement(By.id("firstname"));
inpFirstName.sendKeys("TestFirstName");
driver.quit();

Execute a script in Mozilla Firefox browser

Instantiate FirefoxDriver and specify the location of geckodriver.

System.setProperty("webdriver.gecko.driver", "/Users/username/Desktop/geckodriver");

WebDriver driver = new FirefoxDriver();

Execute a script in Internet Explorer execution

Prerequisites for IE

  • Launch Internet Explorer. In menu-bar, select "Tools -> Internet Options". If you are not able to see menu-bar, press 'Alt' key.
  • Select the 'Security' tab. We can see there are four zones – Internet, Local intranet, Trusted sites and Restricted sites. Also, there is a check-box to Enable or disable Protected Mode.
  • The bottom line is that Protected Mode should be the same for all four zones, either Enable or Disable. We will enable Protected Mode for all four zones. So select all zones one by one and if the checkbox is not selected by default, select it. If it is already selected, do not change it and check the next zone.
  • Once you are done, click 'Apply' and then 'OK' button.

Specify the path of IEDriverServer.exe and instantiate InternetExplorerDriver()

System.setProperty("webdriver.ie.driver", "C:/IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();

That is the only change we need. All further lines of codes will be the same for all browsers like we saw for Firefox example above.

Sample code to do cross-browser execution

You should write code smartly so that you don't have to update the code to run it on different browsers. In real-world projects, we manage it externally, like environment vars, config file etc. And we write code accordingly.

For beginners, here is a sample code:

String strBrowserName = "ie";
WebDriver driver = null;
if(strBrowserName.toLowerCase().contains("firefox")){
 System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
 driver = new FirefoxDriver();
}
else if(strBrowserName.toLowerCase().contains("chrome")){
 System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
 driver = new ChromeDriver();
}
else if(strBrowserName.toLowerCase().contains("ie")){
 System.setProperty("webdriver.ie.driver", "C:/IEDriverServer.exe");
 driver = new InternetExplorerDriver();
}
driver.get("https://cosmocode.io/automation-practice");
WebElement inpFirstName = driver.findElement(By.id("firstname"));
inpFirstName.sendKeys("TestFirstName");
driver.quit();

This was all about doing cross-browser execution. In Next Tutorial, we will learn how to take page screenshot using WebDriver.