Selenium Tutorials

Running Selenium Test on Chrome Browser

In this article, you will learn 

  1. How to run Selenium Tests on Chrome Browser
  2. How to download the latest version of ChromeDriver
  3. How to setup ChromeDriver in different ways.

Chrome driver implements the WebDriver protocol using an executable file called ChromeDriver.exe. The executable file starts a server on your system and all your tests are responsible to run your tests to communicate to this server.

How to download latest version of ChromeDriver

To work with ChromeDriver, first we have to download the latest version of ChromeDriver. To download the latest version of ChromeDriver we need to follow the below steps:

Step1: Go to the official website of chrome driver (https://sites.google.com/a/chromium.org/chromedriver/) and download the latest version of ChromeDriver based on your operating system.

Step2: Click on Downloads link

Step3: Click on ChromeDriver 81.0.4044.69 link. You will be navigated to ChromeDriver Index Download page for Mac, Linux and Windows operating systems.

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

Step4: Click on chromedriver_win32.zip to download ChromeDriver for Windows Operating System.

Step5: Unzip the downloaded file to retrieve chromedriver.exe. To instantiate the driver, you need to note down the location where you extracted the ChromeDriver.

Chrome Browser Launch using Selenium WebDriver

Like any other driver launching Chrome driver launching is very easy.

WebDriver = new ChromeDriver();

Consider the below example to launch the ChromeDriver

package  com.testing.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://in.yahoo.com/?p=us“);

     }

}

When you run the above program we get an exception java.lang.IllegalStateException. Which gives “The executable driver must be set by webdriver.chrome.driver path.”

To overcome the above exception we need to download the ChromeDriver to work with selenium commands.  For every browser have a driver, the driver for Chrome is ChromeDriver.

Different Methods to initialize ChromeDriver

Below are the two methods to initialize ChromeDriver

 

  • By using WebDriver.Chrome.Driver System Property
  • By using Environment Variables

 

Method 1: Using WebDriver.Chrome.Driver System Property

Below is the code to set the System Property for ChromeDriver

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

Below is the complete program to launch the ChromeDriver

Package  com.testing.selenium.ChromeDriver;

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:\\Testing\\Drivers\\chromedriver.exe”);

  WebDriver driver= new ChromeDriver();

  driver.get(“https://in.yahoo.com/?p=us“);

                String PageTitle = driver.getTitle();

                System.out.println(“Page Title is:” + PageTitle);

                driver.close();

     }

}

When you run the above program it will open yahoo.com website in the new Chrome Browser and prints the website title.

Method 2: By using Environment Variables

Step 1: Go to My Computer -> Right click and click on Properties

Step 2: Click on Change settings

Step 3: Click on Advanced tab and Click on Environment Variables.

Step 4: Select Path under System Variables and Click Edit button.

Step 5: Use semicolon at the end of the Variable value and paste the ChromeDriver path. On my computer my ChromeDriver exe is at D:\Testing\Drivers

Note: You would not need to set the ChromeDriver System property every time in the script it is a one time job. Now your script will work without giving System Property code.

Below is the program to launch the ChromeDriver

 

package  com.testing.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://in.yahoo.com/?p=us“);

    String PageTitle = driver.getTitle();

    System.out.println(“Page Title is:” + PageTitle);

    driver.close();

    }

 }

When you run the above program and your script will run without the System Property code and it will open yahoo.com website in the new Chrome Browser and prints the website title.

 

 

Facebook Comments
Tags

Related Articles

Leave a Reply

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.

Back to top button
Close
Close