Table of contents
1.
Introduction
2.
Selenium ChromeDriver
2.1.
Configuring ChromeDriver on Windows
2.2.
Configuring ChromeDriver on Mac
3.
Implementation
3.1.
Program
3.2.
Output
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Running Selenium Tests on Chrome

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Google Chrome currently holds the most extensive user base among web browsers by a vast margin. All the handy features and ease of use make chrome immensely popular among users. That makes testing web applications and websites on Google Chrome critical for developers. 

The Selenium testing tool makes running tests on Google Chrome accessible and easy to implement. Selenium offers cross-browser functionality to test cases on different browsers, including Google Chrome. 

We will understand how we can run our test cases on Google Chrome using Selenium ChromDriver and Java programming through this article.

Selenium ChromeDriver

ChromeDriver allows you to run your Selenium tests on the Chrome browser by acting as a communication medium. ChromeDriver is a standalone server Selenium WebDriver uses to control chrome. Running Selenium test scripts on chrome is not possible without the ChromeDriver. You can use a ChromeDriver easily by instantiating the ChromeDriver object, assigning it to a WebDriver object, and using that object for browser based actions.

As the ChromeDriver is a crucial prerequisite for running Selenium tests on chrome, you need to get it on your system before going forward.

Firstly you will need to download the ChromeDriver executable file according to your system requirements. You can download ChromeDriver for your respective operating system from here. After unzipping the downloaded file, you will get the executable file(chromedriver.exe).

Configuring ChromeDriver on Windows

Now you will need to set the path of your chromedriver in the environment variable using the following steps on windows os.

  1. Right-click on My Computer in your system and select Properties.


     
  2. Now click on the Change settings option and then on the Advanced tab.
  3. Next, select the Environmental variables from the Advanced tab.


     
  4. Select the Path option and click on the Edit option from the options available under system variables.


     
  5. Finally, enter a semicolon ';' at the end of the string, paste your ChromeDriver file's copied path, and click OK.

Configuring ChromeDriver on Mac

The easiest way of making the executable available globally on the macOS or any Linux system is by copying it under any of the folders already in the PATH variable. You can achieve this by following the steps below.

  1. On the Terminal of your system, type echo $PATH to find out all the folders already in the Path variable.


     
  2. Now you can move your chromedriver executable file from the downloaded directory to one of the folders in the PATH variable. For example, usr/local/bin.

Implementation

Now that you have configured ChromeDriver for your system, you can write a test case and run it using Google Chrome.

We will use the Eclipse IDE to run our Java Selenium test case on chrome using the ChromeDriver. Our test case should open Google Chrome, go to www.google.com, and enter Coding Ninjas in the search box.

Firstly you will need to add Selenium as a dependency to your project. You can do this by adding the .jar file to your project directory or adding Selenium as a dependency on your Maven or Gradle build files. The latter is the recommended approach.

pom.xml(Maven)

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.1.3</version>
</dependency>

 

build.gradle(Gradle)

dependencies{
    compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-java’, version: 4.1.3
}

 

Now we can write the code for our test case. In the code, we will initialize the object of ChromeDriver using this command.

WebDriver driver = new ChromeDriver();
You can also try this code with Online Java Compiler
Run Code

 

We have also specified multiple custom options for running chrome and, lastly, to perform the principal task set earlier.

Program

package chromeselenium;


import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class ChromeTest {  
  
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {  
      
           // System Property for Chrome Driver   
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");  
        
        //adding options to use when running chrome.
        ChromeOptions options = new ChromeOptions();
        
        options.addArguments("start-maximized"); // open Browser in maximized mode
        options.addArguments("disable-infobars"); // disabling infobars
        options.addArguments("--disable-extensions"); // disabling extensions
        options.addArguments("--disable-gpu"); // applicable to windows os only
        options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
        options.addArguments("--no-sandbox"); // Bypass OS security model
          
        // Instantiate a ChromeDriver class.     
        //Creating an object of ChromeDriver
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();


        //Deleting all the cookies
        driver.manage().deleteAllCookies();


        //Specifiying pageLoadTimeout and Implicit wait
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


        //launching the specified URL
        driver.get("https://www.google.com/");  
        //Locating the elements using name locator for the text box
        driver.findElement(By.name("q")).sendKeys("CodingNinjas");


        //name locator for the google search button
        WebElement searchIcon = driver.findElement(By.name("btnK"));
        searchIcon.click();
  
    }  
  
} 
You can also try this code with Online Java Compiler
Run Code

 

On running the above code as a Java Application using Eclipse, a new Google Chrome window should open automatically where Coding Ninjas should be searched on Google Search.

Output

Frequently Asked Questions

  1. What is the difference between ChromeDriver and WebDriver?
    WebDriver is an open-source tool for automated testing of web applications across different browsers. The WebDriver gives you the capability to navigate web pages, user input, JavaScript execution, etc. Whereas the ChromeDriver is a standalone server that implements the W3C WebDriver standard. 
     
  2. What is WebElement Selenium?
    An HTML document has  HTML elements; each consists of a start tag and an end tag between which the content lies. A Selenium WebElement is essentially an HTML element on a website. You can use WebElement to search for HTML tags and work with them or test them.
     
  3. How do you assign a WebElement?
    You can assign a WebElement by searching for an element by name, id, XPath, or other identifiers. We have set the google search button as a WebElement to perform a search in our example.

    //name locator for the google search button
     WebElement searchIcon = driver.findElement(By.name("btnK"))
     searchIcon.click();
     
  4. Why is Java used for Selenium?
    Because of being such a prominent and old programming language, Java has an abundance of frameworks, plugins, APIs, and libraries supporting Java for test automation. That is why around 77% of Selenium Testers use Java which also makes knowledge sharing quick and easy.

Conclusion

This article extensively discussed running Selenium tests on Google Chrome using ChromeDriver and its implementation in Java using the Eclipse IDE. You can use the ChromeDriver to perform much more complex Selenium tests. The popularity and features of Google Chrome make ChromeDriver a vital tool for testing web applications. Moreover, Selenium provides cross-browser functionality allowing you to run your Selenium tests with ChromeDriver to execute all tests on the Chrome browser.

We hope that this blog has helped you enhance your knowledge regarding Selenium. If you would like to learn more, check out our articles on Selenium Interview QuestionsMethods in Selenium, and Basics of Java. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass