Open In App

How to run Selenium Running Test on Chrome using WebDriver

Last Updated : 18 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Selenium is a popular open-source tool for automating web browser interactions. It works with different web browsers like Chrome, Firefox, and more, and different programming languages like Python or Java to write tests.

What is a Selenium ChromeDriver?

ChromeDriver is a separate executable that Selenium WebDriver uses to launch and control Chrome. It implements the WebDriver protocol, which is a set of RESTful HTTP endpoints used to communicate between the test script and the browser.

Prerequisites

1. Install Java on your system.

2. After installing Java on your system or already installing, verify the installation by opening a command prompt and typing:

java -version

3. Install Eclipse IDE on your system. Eclipse is a popular IDE for developing Java applications.

4. Install Selenium WebDriver for Java on your system. Selenium WebDriver is a library that allows you to control web browsers programmatically.

Running Test on Chrome

Step 1: Install ChromeDriver

  • ChromeDriver is a driver that allows Selenium WebDriver to communicate with the Chrome browser. Download the latest version of ChromeDriver from here.

Install ChromeDriver

  • Select the link which is compatible with your operating system. Download and extract the (e.g., chomredriver_win32.zip).

Select version

  • After downloading the zip file, extract the zip file and copy the path of the chromedriver.exe
    (e.g., D:\chromedriver_win32\chromedriver.exe).

Extract zip file

Step 2: Create a new Java project in Eclipse IDE

  • In Eclipse, go to File > New > Java Project.
    Create new Java project
  • Next, a pop-window appears Give a name to your project, such as (SeleniumTest), and click Finish.

Give name to project

Step 3: Add Selenium WebDriver jar files to your Project Classpath

  • In Eclipse, right-click on your project name and select Properties.
  • In the Properties window, select Java Build Path from the left panel and click on the Libraries tab from the right panel.
  • Click on the Add External JARs button and browse to the location where you saved the Selenium WebDriver files, select all the jar files (inside the lib folder also), and click Open. Click Appy and close the Properties window.

Add External JARs

Step 4: Create a new Java class in your Project

  • In Eclipse, right-click on the src folder under your project name and select New > Class. Give a name to your class, such as TestChrome, and click Finish.

Create new Java class

Example

Below is the Java program to retrieve title of the website:

Java




// Java program to retrieve title of the website
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
  
public class TestChrome {
    public static void main(String[] args)
    {
        // Set the path of chromedriver.exe file
        System.setProperty(
            "webdriver.chrome.driver",
            "D:/chromedriver_win32/chromedriver.exe");
  
        // Create an instance of ChromeDriver class
        WebDriver driver = new ChromeDriver();
  
        // Navigate to a web page
        driver.get("https://www.geeksforgeeks.org/");
  
        // Get the title of the web page
        String title = driver.getTitle();
  
        // Print the title
        System.out.println("Page Title: " + title);
  
        // Close the browser
        driver.quit();
    }
}


Explanation:

  • The above program opens a Chrome web browser using Selenium WebDriver, navigates to the GeeksforGeeks homepage (https://www.geeksforgeeks.org/).
  • It retrieves the title of the page, and prints the title to the console.
  • Then, it closes the browser and ends the WebDriver session.

Output:

ggeks-selium



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads