How to run Selenium tests on Chrome using ChromeDriver?
This is the perfect way to make the web automation tests execute faster. So let’s get started to know how we can achieve it.
Google Chrome is the industry leader in executing selenium automation tests because it is one of the most popular web browsers used globally. With its widespread use and global coverage, evaluating web applications and websites on the Chrome browser is very critical and essential. Since Selenium offers cross-browser functionality, it is a preferred tool for test automation. Developers can run Selenium tests on Chrome using the Selenium ChromeDriver. You can run Selenium tests on Chrome once the ChromeDriver is configured.
In this article, we will focus mainly on using ChromeDriver for Selenium. You will learn how to install, configure, and download ChromeDriver for Windows. Let’s look at how to run Selenium tests on Chrome.
What is Selenium and Selenium ChromeDriver?
Selenium is an open-source automation testing framework to test web applications using web browser interactions. It supports various browsers like Chrome, Firefox, Internet Explorer and safari.
We use Chrome Driver with Selenium in order to execute tests on the Chrome browser. Chrome Drivers are executable drivers used by Selenium WebDriver to launch Google Chrome. Chrome Driver is also an open source and we can run the tests on Chrome Browsers by configuring it as per our requirement.
How to download and configure ChromeDriver for Selenium?
Chrome Driver implements W3C WebDriver standards and protocols. To use Chrome Driver, we must first download it from the Chromium website, install it on the system, and set up the project.
To configure Chrome Driver in the code, we use the below line of code:
WebDriver driver = new ChromeDriver()
Chrome Browser implements the WebDriver Interface. We need Chromedriver.exe to start the server on the system that runs the test scripts in Selenium.
Configuring Selenium ChromeDriver using Environment Variables?
We can declare system level variables for Chrome Driver so that whenever a webdriver instance is created in Selenium script, it automatically detects the path of the Chrome Driver and runs on it.
Step 1. Once the ChromeDriver executable file is extracted and moved to the folder location, copy that location and set its path in the System’s environment variables.
Step 2. open “System Properties” pop up. Click on the “Environment Variables” button. In System Variables section, click on Path variable and then click on the Edit button.
ChromeDriver
Step 3: On the Edit environment variable pop-up, click on the “New” button and add the ChromeDriver executable location.
Step4:Click on OK to save the configuration.
Step 5. When we create an instance of ChromeDriver in the script, it will automatically detect the ChromeDriver path from the system variables and run the tests on the Chrome browser.
Configuring Selenium ChromeDriver using System.setProperty method?
There is another way to configure ChromeDriver in the test script that is configuring using code explicitly using System.setProperty() method
This method accepts key value pair where
key is “webdriver.chrome.driver” and value is the path of the ChromeDriver executable file that is chromedriver.exe
System.setProperty(“webdriver.chrome.driver”,”path of the chromedriver.exe”)
Running Selenium tests on Chrome
Steps to run Selenium Tests on Chrome Browser
In any IDE, create a Java Project and Add Selenium Java Jars in the java build path.
Create a Java class and write the below code to launch Chrome browser.
System.setProperty("webdriver.chrome.driver","C:\\Users\\ADMIN\\Documents\\chromedriver.exe");
// Instantiate a ChromeDriver class.
WebDriver driver = new ChromeDriver();
// Maximize the browser
driver.manage().window().maximize();
// Launch Website
driver.get("https://www.google.com/");
Delete the System.setProperty() line from the above code if ChromeDriver is configured in the environment variable.
Code Walkthrough:
Set the system property “webdriver.chrome.driver” to the path of your ChromeDriver.exe file and instantiate a ChromeDriver class: System.setProperty(“webdriver.chrome.driver”,”chromedriver path”);
Maximize the window: driver.manage().window().maximize();
To open the URL: driver.get(“URL link”)
Configuring Selenium WebDriver with WebDriver Manager
WebDriverManager provides an API using the class WebDriverManager (package io.github.bonigarcia.wdm). This class provides a group of static methods to create managers, i.e., objects devoted to providing automated driver management and other features.
The primary use of WebDriverManager is the automation of driver management. To use this feature, you need to select a given manager in the WebDriverManager API (e.g., chromedriver() for Chrome) and invoke the method setup().
Let us see how to invoke the browser using WebDriver Manager:
Add the below dependency to the pom.xml
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.9.2</version>
</dependency>
Below line of code invokes the chrome browser using WebDriverManager.
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
Using WebDriver Manager , no need to download the chrome driver exe file manually or setup , just with one line of code, we can invoke the browser.
Conclusion
Selenium and Chrome browsers are the most common combinations for testing web applications across the globe. Selenium automates the testing of web applications, and with its support for the Google Chrome browser, ChromeDriver has become one of the most popular drivers. Even though we can use ChromeDriver without any additional configuration, we have understood how to get the most out of ChromeDriver by configuring it.
Source: This article was originally published at https://testgrid.io/blog/chromedriver-for-selenium/.
Subscribe to my newsletter
Read articles from Steve Wortham directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Steve Wortham
Steve Wortham
Experienced Software QA Manager passionate about delivering top-quality software, writing intuitive content, and fostering connections with industry peers.