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

  • 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();
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.

14 thoughts on “Interacting with the browser | Running test on different browsers”

  1. Hi,

    Has anyone tried to run Selenium automation on IE (any version) ? I have tried to do so, but what happens is that methods like click() not working as it should(some clicks are not executed). Also, after a few clicks (if it is done as it should) IE freeze and any activity is not possible any more. There is a strange behaviour that I noticed with UI. At some point UI looks like a mess (none of the links, forms is not where it should be). Is there any solution for this issues ?

    Thanks!
    Mario

    Reply
  2. When I used Selenium in 2012 we already had the click() issue on IE, its a very old and common bug and there is no fix for it. Some say you should use Actions some say disable/enable native events, others recommend sending a "Keys.RETURN" instead of clicking and others went to use javascript's click()…

    Reply
  3. Hi Mario… Is it possible for you to share the URL and the line of codes that is not able to perform some action on IE… Is the same code working if the application is opened on Firefox? As you mentioned that UI is looking messy for IE, it may be possible that the application is not getting rendered properly in IE. Even if you open our Website in IE the UI is somewhat different as the CSS and Javascript doesn't load for IE properly. Hence it is natural that you will face issues while executing it on IE. In general, there is no such issue with IE execution in Selenium if application behavior is same as other browsers.

    Reply
  4. These workarounds helps sometime to overcome these issues. However IE implementation for WebDriver has became mature now with new releases and generally we do not face these issues.

    Reply
  5. In Next Tutorial we will learn how to take page screenshot and execute JavaScript.

    Can I bring up one theme for next tutorials?:) It would be really great to see how using Selenium it's possible to create Test Report for executed tests.

    Reply
  6. And really you sometimes predict questions that might me asked, when I started reading this chapter I thought of if it's possible to run tests in each browser consequentially…:)

    Reply
  7. Hi Alexey… Good thought… Unfortunately Selenium has no role in creating test reports. For that we either need to write our own code in the programming language that we are using (e.g to write report in a excel/text file ) or we can use some testing frameworks like TestNG / JUnit to generate HTML reports… Cheers, we will cover it later on in this website.

    Reply
  8. Again quite impressive and informative material on selenium

    Things to notice

    –> For Paths we are using '/' instead of ''

    –> Paths are not case sensitive

    –> we can use 'ignorecase' instead of 'contains' so we can avoid making it lowercase

    –> Tutorial is helping in learning language too
    I Would like to know the answers for the below Questions

    q1) Why we dont need to include path for firefox driver ?
    did de included at the time of initial tutorials

    q2) what is the tool you use for screen capture and marking on them ?

    q3) I am getting below error while using IE

    https://www.dropbox.com/s/j0td15l0593alhy/Problem%20in%20Selenium%20in%20IE.png

    q4) when i am using chrome in the address bar it shows " data:, " whats the significance

    Regards
    Gaurav Khurana

    http://www.Udzial.com
    Udzial Means share

    Reply
  9. Good observations Gaurav. In Windows OS we can use both forward slash(/) or backward slash() to specify path.. In Mac/Unix, only forward slash is allowed.

    q1) Why we dont need to include path for firefox driver ?
    did we included at the time of initial tutorials?
    Ans – For firefox we don't need separate driver to download. It is by default supported by WebDriver. Remember, in "Download Selenium Dependencies " tutorial we downloaded just IE and chrome driver?

    q2) what is the tool you use for screen capture and marking on them ?
    Ans – Snipping Tool / MS Paint. Both comes with Windows.

    q3) I am getting below error while using IE

    https://www.dropbox.com/s/j0td15l0593alhy/Problem%20in%20Selenium%20in%20IE.png

    Ans -> whenever you are writing URL make sure to give double-slash after http:. You missed that. In screenshot it is http:/www.udzail.com only. It should be http://www.udzail.com

    q4) when i am using chrome in the address bar it shows " data:, " whats the significance
    Ans – With latest chrome versions it is coming. Even I am curious to know.

    Reply
  10. Please let me know if you’re looking for a author for your weblog. You have some really good articles and I feel I would be a good asset. If you ever want to take some of the load off, I’d really like to write some content for your blog in exchange for a link back to mine. Please send me an e-mail if interested. Regards!

    Reply

Leave a Reply to AnonymousCancel reply