CosmoCode (Formerly TeachMeSelenium)

Interacting with the browser | Running test on different browsers

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 follwing 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();
chrome_driver_path = "/Users/username/Desktop/chromedriver"

driver = webdriver.Chrome(chrome_driver_path)

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();
driver.get("https://cosmocode.io/automation-practice")
driver.find_element_by_id("firstname").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();
gecko_driver_path = "/Users/username/Desktop/geckodriver"
driver = webdriver.Firefox(executable_path=gecko_driver_path)

Troubleshooting

I ran the code and it raised an error – geckodriver executable needs to be in PATH

Are you sure you have passed the correct path to gecko driver as mentioned in the previous steps? Please double-check it.

I am getting another error- Expected browser binary location, but unable to find binary in the default location

The webdriver is trying to find your firefox installation in its default directory. If it is installed in some other directory you need to mention it explicitly:

firefox_binary_path = "some/path/to/firefox/binary"
driver = webdriver.Firefox(executable_path=gecko_driver_path, firefox_binary=firefox_binary_path)

Execute a script in Internet Explorer execution.

Prerequisites for IE

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

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

That is the only change we need. All further lines of codes will be the same for all browsers like we say 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();
#variable for specifying browser name
browser_name = "ie"
driver = None
if browser_name is "firefox":
    driver_path = "C:/teachmeselenium/geckodriver.exe"
    driver = new webdriver.Firefox(executable_path=driver_path)
elif browser_name is "chrome":
    driver_path = "C:/teachmeselenium/chromedriver.exe"
    driver = new webdriver.Chrome(executable_path=driver_path)
elif browser_name is "ie":
    driver_path = "C:/teachmeselenium/IEDriverServer.exe"
    driver = new webdriver.Ie(executable_path=driver_path)

driver.get("https://cosmocode.io/automation-practice")
driver.find_element_by_id("firstname").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.

Exit mobile version