Running Selenium Test On Chrome Browser

Introduction

One of the major advantages of Selenium is that it is compatible with multiple browsers. The browser-specific browser drivers help in achieving this feature of Selenium. To sum up, different browser-specific drivers run the Selenium script, on different browsers. In this tutorial, we will learn how to execute the Selenium code on the Chrome browser.

Pre-requisites

We need to do the environment setup before we can write the automation code. Therefore, the pre-requisites to run the Selenium script on Chrome browser are:

In case you have missed any of the above set up, please refer to the individual tutorials available in our course for them. After completing these steps, we need to set up a Chrome driver which will form a link between Selenium code and Chrome browser.

Set up for the Chrome Browser

Chrome Browser is the most widely accepted browser which is designed by Google. It is used on the web and mobile devices both.

If your script does not find the driver when you run it, it would throw an exception as shown in the image below:

Note: If you have not yet executed the script, you can do so by right-clicking on the java class > Run As > Java Application.

The error clearly says that path to driver executable should be set. Hence, to execute the Selenium script on Chrome, we need to set the path to chrome driver executable. We will perform the following steps in this tutorial:

  • Download Chrome driver
  • Set the Path variable or set the system property in the script
  • Execute the code
Download Chrome driver  
  1. First, check the version of the Chrome browser installed in your machine. You can do so by Help > About Google Chrome. It will show the current version installed.

2. Next step is, to download the Chrome browser driver. For that, we need to go to the download section of chromium official site https://sites.google.com/a/chromium.org/chromedriver/.

3. Select the version of Chrome Driver as per the chrome browser in your machine and click on it. It will navigate to a webpage with corresponding executables for different platforms.

4. For windows, select chromedriver_win32.zip and click on it. It will download a zip file. Save this zip file to an appropriate location.

Note: Please select the chrome driver version which matches your Chrome browser version. Otherwise, the script will not be able to establish the browser session and will throw an exception. For this reason, decide the suitable version of the chrome driver considering the official chrome driver site https://sites.google.com/a/chromium.org/chromedriver/downloads/version-selection.

5. Extract the content of the saved zip file in a folder and you will get chrome driver executable.

Driver location setup

Now after we have downloaded the Chrome driver executable successfully, we can tell the Selenium script where to locate it. There are two methods of doing it.

  1. Set the Path variable to point to the driver location in the system. I have stored Chrome driver at C:\softwares\drivers in my system. So I can set Path = C:\softwares\drivers and my Selenium script will be able to locate it without any code changes in the script. This method is demonstrated in detail in First Selenium Test case tutorial. Please refer to this tutorial if you wish to follow this first method.
  2. Set the driver location in the Selenium script using System.setProperty() method. You can also do the code changes in the Selenium test script to point to the location of the driver executable. This method is demonstrated below:
Code Changes

For the second method, we will set a system property “webdriver.chrome.driver” which points to the location of chrome driver executable.

//Set the system property for Chrome driver
System.setProperty(“webdriver.chrome.driver”,”C:\\softwares\\drivers\\chromedriver.exe”);

package seleniumAutomationTests;

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

public class MyFirstTest {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
    //location of the chrome driver exe
    System.setProperty("webdriver.chrome.driver","C:\\softwares\\drivers\\chromedriver.exe");
    
    //declare driver instance
    WebDriver driver = new ChromeDriver();
    
    //load the webpage of application under test
    driver.get("https://www.facebook.com/");
        
    //get the title of the page in a string variable
    String pageTitle = driver.getTitle();
    
    //print the page title on console
    System.out.println(pageTitle);
    
    //close the browser
    driver.close();

  }

}
Execution

To execute the script, right-click on the java class name and go to Run As > Java Application. With this, the script will start its execution. It will

  • Locate the driver at the location set by the System.setProperty method.
  • Start the Chrome browser using the chrome driver
  • Launch the application under test
  • Fetch the value of page title of the web page and store it in a string.
  • Print the page title on the console
  • Close the web page and complete the execution of the script.

Checking the output 

When we start the execution of our script, the application under test will start in a new chrome browser window. Then, it will perform the commands provided in the script. Thus, in our case fetch the page title and driver.close() method will close the browser window.

We can observe the output of the script on the console window at the bottom of the Eclipse IDE. Next, it will print the page title here.

 

Common exceptions encountered:

  1. IllegalStateException: It is thrown when the path to driver executable is not set. Resolve it either by providing the location of chrome driver executable using System.setProperty or by setting the Path environment variable.
  2. java.net.SocketException: It is thrown when there is a version mismatch between the Chrome browser in the system and chrome driver exe. It can be resolved by getting the latest Chrome driver exe if the Chrome browser has been upgraded. Or else, you can degrade the Chrome version in the system to resolve the error.

Conclusion

In this tutorial, we learned about how to set up the chrome driver for executing Selenium code on the chrome browser. Next, we will see how the same Selenium code can be executed on other browsers like Firefox and IE to achieve a multi-browser feature of WebDriver.

Translate »