9e. WebDriver – Code samples for launching other browsers

This entry is part 31 of 36 in the series Selenium

What if you can only automate your tests in one browser using Selenium, i.e. Firefox but your web application supports Chrome and IE as well? This implies that testing is required on all three browsers! If this is the case, then Selenium is as useful as sticking your head in sand.

Oh, don’t panic! Our ruling champ, WebDriver is here for our rescue. And this is our topic for the day. Let’s dive in!

Step 1: First things first! Let us download the required executable files. Go to ‘www.seleniumhq.org/download’.

  • InternetExplorerDriver is available under ‘Internet Explorer Driver Server’ section.
  • ChromeDriver, Opera, SafariDriver are available under ‘Third Party Browser Drivers’ section.

In this post we will only discuss InternetExplorerDriver and ChromeDriver. Similar procedure is to be followed for other browsers set up as well.

Browser download

Step 2: Once the executable files are downloaded, extract and save them in desired path. I have created a folder (right click on the package -> New -> Folder) named “browser-drivers” within the project in eclipse IDE and copied the downloaded files for easy access.

Step 3: Now, let us create two new classes by “right clicking on the package -> New -> Class” and naming them as ‘HelloWorld_IE.java’ and ‘HelloWorld_Chrome.java’ respectively.

The package explorer pane now looks as below,

Eclipse window

Let us consider a very simple scenario as our main goal here is to see if we can launch IE and Chrome browsers,

  1. Open IE/Chrome browser as per the test case.
  2. Navigate to ‘https://www.google.com/’
  3. Display ‘Hello World’ message in the console.
  4. Close the browser.

Let us first see the code for ‘HelloWorld_IE.java’ class,

If noticed, this code looks very similar to what we saw in our previous post, “First test script – By launching Firefox”.

These other browsers require a system property set up as its direct launch is not supported.

It is required that the path to the driver executable, “IEDriverServer.exe”  must be set by the “webdriver.ie.driver” system property. Specify the path where the executable is saved in your system accordingly.

The other major change to be made in order to instantiate IE browser is,

Once this statement is typed, a squiggly line appears below ‘WebDriver’ and ‘InternetExplorerDriver()’. Upon hovering, eclipse will suggest all possible quick fixes. Click on the first fix that suggests to import respective packages.

The following code looks quite self explanatory from the comments provided. It just navigates to the google page once IE browser is launched, prints ‘Hello World’ message to the console and closes the browser window.

The cosole window upon executing this test is as below,

Eclipse console

The code for ‘HelloWorld_Chrome.java’ class,

There are only two noticable changes if this code is compare to ‘HelloWorld_IE.java’.

  1. System property set up
  2. ChromeDriver class instantiation

We can thus conclude that by specifying the respective system property and providing exact driver class instantiation, respective browsers can be launched and automated easily with the help of WebDriver.

Upon executing the code for Chrome browser, console window is as below,


Slowly and finally WebDriver is getting its time in the Sun I guess! You are all welcome to raise your voice in the comments section in case you face any issues in launching various browsers.

Until then, see you again in another post! Have a nice day!

Series Navigation<< 9d. WebDriver – Executing the Test9f. WebDriver – JUnit Environment Set-up >>

Leave A Comment

Your email address will not be published. Required fields are marked *