How to run Selenium tests on Chrome using ChromeDriver?
By Jash Unadkat, Community Contributor - March 20, 2023
Google Chrome dominates the browser market with a massive 64% global market share. With several useful features, it is one of the most preferred browsers in the world. Given its importance and usage, testing all websites on Chrome becomes crucial.
This article will explain how to perform Selenium tests on a Chrome browser using ChromeDriver. But before that, let’s understand what ChromeDriver is and how users can configure it on their systems.
What is a Selenium ChromeDriver?
A ChromeDriver is a standalone server or a separate executable used by Selenium WebDriver to control Chrome. Running Selenium test scripts on the Google Chrome browser is impossible without ChromeDriver. One can easily initialize the object of ChromeDriver using the following command:
WebDriver driver = new ChromeDriver
How to configure ChromeDriver?
Step 1 – Download ChromeDriver
First, download the ChromeDriver. Users need to download ChromeDriver for their respective Operating systems from this link. Refer to the image to get a better sense of ChromeDriver downloads.
Step 2 – Retrieve the chromedriver.exe File
Once the zip file is downloaded for the operating system, unzip it to retrieve the chromedriver.exe executable file. Copy this file to a specific location of your choice.
Step 3 – Set environment variables
Now copy the path where the ChromeDriver file is saved to set the system properties in environment variables. Follow the steps below to set the path in the environment variables:
1. Right-click on My Computer and click on Properties
2. Click on the Change settings option and then click on the Advanced tab
3. Now select the Environmental variables from the Advanced tab
4. Now, from the available options under system variables, select the Path option and click on Edit
5. At the end of the string, enter a semicolon ‘;’ and paste the path of your ChromeDriver file that you copied earlier, and click OK.
Heard about SafariDriver? Learn how to how to run Selenium Tests on Safari using SafariDriver.
Steps for macOS users:
- Go to the terminal and type the command: sudo nano /etc/paths
- Enter the password
- At the bottom of the file, add the path of your ChromeDriver
- Type Y to save
- To exit press Control + C
Steps to run Selenium Tests on Chrome Browser
Let’s discuss how to run Selenium scripts on a Chrome browser using ChromeDriver.
- Install the Eclipse Integrated Development Environment (IDE) and import all the Selenium dependencies into the project directory. (Users can use other IDEs, too as per their choice)
- Set the properties by specifying the type of driver to be used along with its path describing where it is stored.
- Initialize the object of the ChromeDriver. This helps in launching the Chrome browser.
- To navigate to a particular URL, use the driver.get() method. One can also locate certain web elements using specific locators. To know more about locating elements in Selenium, refer to this detailed guide on Locators in Selenium.
Pro Tip: Want to dive deeper into Selenium implementation on BrowserStack with free interactive courses and lab exercises? Visit Test University
Now let’s consider a test case example wherein we want to perform 3 simple steps:
- Open Chrome Browser
- Go to www.google.com
- Enter Youtube in the search text box
Did you know: There are more than 98 Chrome Browser versions to test on. Don’t miss out on testing across!
Refer to the code snippet below to get a better sense of executing the steps mentioned above:
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; public class ChromeExample { public static void main(String[] args) { //Setting system properties of ChromeDriver System.setProperty("webdriver.chrome.driver", "C://Selenium-java browserstack//chromedriver_win32//chromedriver.exe"); //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("YouTube"); //name locator for google search button WebElement searchIcon = driver.findElement(By.name("btnK")); searchIcon.click(); } }
When the above program is executed, the ChromeDriver launches the Chrome browser, opens the Google website, and enters the value Youtube in the search box. This is how one can run Selenium tests in Chrome using ChromeDriver.
Run Selenium Tests using ChromeDriver
- Although it’s vital to test web-apps for Chrome, it’s also important for a web app to be optimized for other browsers like Firefox, Safari, Edge, etc.
- Teams can leverage BrowserStack, which enable QAs to perform cross browser testing for their web apps across thousand of real browsers and browser versions like Safari, Opera, Edge, etc.
- All real browsers are installed on real desktop and mobile devices, thus providing the optimal environment for software testing in real user conditions.
Frequently Asked Questions (FAQs)
1. How to use Selenium WebDriver ChromeDriver?
The following are the steps to use Selenium WebDriver ChromeDriver:
- Download and install the latest version of ChromeDriver.
- Install the Selenium WebDriver client for your programming language (e.g., Python, Java, Ruby).
- Import the necessary packages in your code to use Selenium WebDriver and ChromeDriver.
- Create a new instance of ChromeDriver in your code.
- Use the instance to automate actions in the Chrome browser (e.g., navigating to a URL, filling out a form).
2. Are there any prerequisites for using Selenium WebDriver ChromeDriver?
Yes, you need to have a basic understanding of a programming language and its syntax. You must also have installed the latest versions of Chrome browser, ChromeDriver, and Selenium WebDriver client for your chosen programming language.
3. Which Selenium WebDriver do we need to execute tests in the Chrome browser?
- To execute tests in the Chrome browser using Selenium WebDriver, you need to use ChromeDriver.
- ChromeDriver is a separate executable used by Selenium WebDriver to control the Chrome browser.
- Download the latest version of ChromeDriver and control the Chrome browser during your tests.
- Use the version of ChromeDriver that matches the version of your Chrome browser.