Running Test on Selenium Chrome Driver With Examples

Chrome browser implements the WebDriver protocol by using an executable file called ChromeDriver.exe. This executable file starts a server on your system and all your tests will communicate to this server in order to run your tests.

In this article, we will learn

  • How to download the latest version of Selenium ChromeDriver
  • How to setup Selenium ChromeDriver in multiple ways

Download Selenium ChromeDriver

First, we have to download the latest version of ChromeDriver, mainly because it supports the latest versions of Chrome, and it contains all the bug fixes. The following are the steps to download ChromeDriver.

 

Chrome Version

  • Step 2: Click on ChromeDriver 73.0.3683.20 link. You will be navigated to ChromeDriver download page which contains ChromeDriver for Linux, Mac and Windows operating systems.

Note: Here we are working on Windows Operating system, we need to download the corresponding Chrome driver of Windows version. If your Operating System is Linux or Mac then you need to download the corresponding Chrome driver.

Chrome-Index

Chrome Index

  • Step 3: Click on chromedriver_win32.zip to download ChromeDriver for Windows.
  • Step 4: Once the zip file is downloaded, you can unzip it to retrieve chromedriver.exe. Note the location where you extracted the ChromeDriver. Location will be later used to instantiate the driver.
  • Chrome-Driver

Chrome Driver

Launching Chrome Browser using Selenium WebDriver

Launching a Chrome driver is easy for launching as any other driver.
WebDriver = new ChromeDriver();

package com.journaldev.selenium.Chrome;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeDriver {
	public static void main(String[] args) {
		WebDriver driver= new ChromeDriver();
		driver.get("https://journaldev.com");
     }
} 

When you run above program we get an exception called java.lang.IllegalStateException. which tells The path to the driver executable must be set by webdriver.chrome.driver.

To overcome the above problem we need to download the ChromeDriver in order to work with selenium commands which we are writing on Chrome. Every browser as a driver. The driver for Chrome is the ChromeDriver. The selenium commands will be interpreted by ChromeDriver and it will be executed on Chrome.

Different ways to initialize ChromeDriver

There are 2 methods to initialize ChromeDriver

  • Use Webdriver.Chrome.Driver
  • Use Environment Variables

Method 1: Use Webdriver.chrome.driver system property

Code to set the System properties is

System.setProperty(“webdriver.chrome.driver”,“Path to chromedriver.exe”);

The complete program to launch the ChromeDriver will be like this:

package com.journaldev.selenium.Chrome;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeDriver {
	public static void main(String[] args) {
                System.setProperty("webdriver.chrome.driver","D:\Drivers\chromedriver.exe");
		WebDriver driver= new ChromeDriver();
		driver.get("https://journaldev.com");
                String PageTitle = driver.getTitle();
                System.out.println("Page Title is:" + PageTitle);
                driver.close();
     }
} 

When you run the above program you will notice that Journaldev.com is opened in the new Chrome window and it will print the website title in the console.

Method 2: Setting ChromeDriver Path in Windows Environment Variables

  • Step 1: Go to My Computer and Right click to get the context menu.
  • Chrome-Driver

 

MyComputer Properties

  • Step 2: Click on the Change Setting on the opened window.

Change Settings

Change Settings

  • Step 3: Click on Advance tab and click on Environment Variables.

System Properties

System Properties

  • Step 4: Select Path under System Variables and click on Edit.

Environment Variables

Environment Variables

  • Step 5: At the end of the string use semicolon and paste the path of the ChromeDriver. On my machine my ChromeDriver exe resides in D:Drivers

Path

Path

Note: Once the path is set you would not need to set the System property every time in the script. Your script will work without the System Property code.

The complete program to launch the ChromeDriver will be like this:


package com.journaldev.selenium.Chrome;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromefoxDriver;
public class ChromeDriver {
    public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
    driver.get("https://journaldev.com");
    String PageTitle = driver.getTitle();
    System.out.println("Page Title is:" + PageTitle);
    driver.close();
    }
 }

When you run the above program your script will work without the System Property code and you will notice that Journaldev.com is opened in the new Chrome window and it will print the website title in the console.

By admin

Leave a Reply

%d bloggers like this: