9e. WebDriver – Code samples for launching other browsers

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,

package com.blog.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class HelloWorld_IE {

	public static void main(String[] args){
		//System property set up
		System.setProperty("webdriver.ie.driver", "browser-   drivers\\IEDriverServer.exe");
		//Create a new instance for the class InternetExplorerDriver
		//that implements WebDriver interface
		WebDriver driver = new InternetExplorerDriver();
		//Assign the URL to be invoked to a String variable
		String baseUrl = "https://www.google.com";
		//Open baseUrl in IE browser window
		driver.get(baseUrl);
		//Print the message to console
		System.out.println("Hello World!");
		//Close the IE browser
		driver.quit();
	}
}

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.

System.setProperty("webdriver.ie.driver", "browser-drivers\\IEDriverServer.exe");

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,

WebDriver driver = new InternetExplorerDriver();

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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

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,

package com.blog.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class HelloWorld_Chrome {

	public static void main(String[] args) {
		// System property set up
		System.setProperty("webdriver.chrome.driver", "browser-drivers\\chromedriver.exe");
		// Create a new instance for the class ChromeDriver
		// that implements WebDriver interface
		WebDriver driver = new ChromeDriver();
		// Assign the URL to be invoked to a String variable
		String baseUrl = "https://www.google.com";
		// Open baseUrl in IE browser window
		driver.get(baseUrl);
		// Print the message to console
		System.out.println("Hello World!");
		// Close the IE browser
		driver.quit();
	}
}

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,

ConsoleOutput

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!

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.