testsigma
Topics
left-mobile-bg

Selenium ChromeDriver & How to do Selenium Tests on Chrome?

June 10, 2024Adhithi
right-mobile-bg
How to run Selenium tests on Chrome using ChromeDriver
image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free

If you’re looking for a way to make your web automation testing more enjoyable, you’ve come to the right place. I’ve got just the thing for you: Selenium ChromeDriver! It’s the perfect way to take your web automation tests to the next level – with a few clicks of a mouse; you can run Selenium tests on Chrome quickly! So, buckle up, and let’s get started.

A web browser is a window that gives you access to the Internet. It is the software application needed to visit a local or global website, which has a tremendous amount of utility, without a doubt. With a market share of roughly 65–67%, Google Chrome is the industry leader in this area and one of the most popular web browsers.

Given its widespread use and global coverage, evaluating web applications and websites on the Chrome browser is 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.The Google ChromeDriver is an open-source executable toolkit for Chrome that helps connect the browser to Selenium WebDriver. You can run Selenium tests on Chrome once you have configured ChromeDriver.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?

Selenium is a portable open-source automation testing framework for web applications. It supports various browsers, including Firefox, Chrome, Internet Explorer, and Safari. It can also simulate user interactions on a website, such as clicking links, filling out forms, and buying products. 

Selenium automates web browser interaction to test web applications. Selenium is often used in conjunction with other software testing frameworks, such as JUnit or TestNG, to create comprehensive automated tests for web applications.

Selenium can also be used to automatically test web applications for functionality and performance. In addition, Selenium can be used to scrape data from websites or generate reports automatically. In comparison to other testing tools, Selenium has a huge advantage in that there are no license fees because it is an open-source technology. 

Factors Contributing to Selenium’s Rising Popularity

The following are additional factors contributing to Selenium’s rising popularity:

  • You can write a test script in any one of the programming languages:  Python, Java, Ruby, C#, Perl, PHP, and .Net.
  • All three operating systems are supported for testing – Windows, Mac, and Linux.
  • A variety of browsers are available for testing, including Internet Explorer, Firefox, Google Chrome, Opera, and Safari.
  • Testing can be managed, and reports generated using tools such as TestNG and JUnit.
  • It supports continuous testing using Jenkins, Maven, and Docker.

While Selenium has many benefits, it also has some drawbacks. For example, Selenium can be slow to run tests, and it can be difficult to set up complex test scenarios. It remains, however, an important tool when it comes to automating web browsers and testing web applications.

What is a Selenium ChromeDriver?

ChromeDrivers are standalone servers or executables used by Selenium WebDriver to launch Google Chrome. ChromeDriver is a free, open-source web driver for automated testing of web applications across Google Chrome browsers. Selenium is an umbrella project for a variety of tools and libraries that enable and support automated web testing. ChromeDriver is one such tool that runs on Windows, macOS, and Linux operating systems. 

Google ChromeDriver implements the W3C WebDriver standard and provides a platform to write automated tests for Chrome browsers using any programming language that supports the standard. These tests can then be run using any of the many test runners available. Some popular test runners are JUnit, TestNG, and Mocha. 

To use ChromeDriver, you need to first download it from the Chromium website and then install it. Then you need to set up your test project to use ChromeDriver. This command can initialize ChromeDriver – WebDriver driver = new ChromeDriver. When using Chrome, you will need Chromedriver.exe (on Windows) or Chromedriver (on Linux and Mac) to run.

The code for doing this will vary depending on which programming language and test framework you use. Once everything is set up, you can start writing your tests. 

ChromeDriver to Test Web Applications

ChromeDriver provides several capabilities that can be used to test web applications –

  • Navigation – Capabilities to navigate to URLs, forward and back buttons, and refresh buttons.
  • User input – Capabilities to simulate user input with keyboard and mouse events.
  • JavaScript execution – Execute JavaScript in the context of the currently selected frame or window.
  • Take screenshots – Take a screenshot of the current page or an element on the page.
  • Cookie handling – Set and delete cookies visible on the current page.
  • Modifying network conditions – Modify network conditions such as offline mode, latency, bandwidth throttling, etc.

Why Use a ChromeDriver?

There are many reasons to use a ChromeDriver when working with web applications. To start with, Chrome is a widely used browser, so your users will likely be using it. Chrome is generally considered to be a stable and reliable browser, so you can be confident that your tests will run smoothly. Google ChromeDriver is faster and more stable than many of the other options on the market, offering a wide range of features, including built-in support for locators, cookies, and extension management. 

The ChromeDevTools interface provides a wealth of debugging and troubleshooting tools for ChromeDriver, which is continually updated with new features and improvements. Additionally, with a ChromeDriver, you can take advantage of features like headless mode and automatic updates. Overall, using a ChromeDriver is a great way to ensure that your tests run smoothly and reliably on the most popular web browser. As a result, it is always worth considering using ChromeDriver for your web application needs.

When it comes to web automation, Selenium is one of the most popular tools available. Selenium allows you to control a web browser using code, which can be used for tasks like automated testing or web scraping. Selenium is a powerful tool; however, Selenium can only control browsers that have been specifically designed for automation.

Although the Google Chrome browser does not have such an extension, it can be controlled by using a Selenium ChromeDriver. Selenium ChromeDriver ensures that Selenium will be able to control the Chrome browser and perform the automated task you need it to. Therefore, it is important to use a ChromeDriver for Selenium if you want to automate tasks in the Chrome browser.

Why is ChromeDriver Important for Selenium?

Google Chrome is one of the most popular internet browsers globally. Nearly 3.45 billion people use Chrome as their preferred browser. Naturally, if you are developing a web application, testing its compatibility with Chrome is a necessary step. 

And if your choice of testing platform is Selenium, ChromeDriver is an obligatory server of choice that facilitates a communication channel between Selenium and the Chrome browser. If, during the test execution stage, ChromeDriver is missing, Selenium will not be able to control the Chrome browser and check the web application’s compatibility with the browser.

In fact, Selenium comes with an in-built Selenium Manager that automatically downloads and manages the drivers needed for testing. You do not need to manually install the ChromeDriver.

Manual Way to Install and Setup Selenium ChromeDriver for Testing

However, if you need to install and set up Selenium ChromeDriver manually for automation testing, you should be aware of the process. Here’s how you can do it:

For Windows

Once you have downloaded the ChromeDriver for Selenium on your system, follow these steps to install it.

  • Open File Explorer and head over to the ChromeDriver folder that you had extracted.
  • Next, copy the ChromeDriver executable file path, i.e., ChromeDriver.exe.
  • Open the About section under the settings option and click on Advanced system settings, which are present under the System Properties window.
  • Select the Environment Variable option to display the user and system variables.
  • Under the system variable, look for the Path variable and click on edit. A new window will open.
  • Click on New in that window and paste the file path you copied in step 2.
  • Click on OK to save the changes you made.
  • The ChromeDriver is installed on your Windows System.

For Mac

  • After the download is complete, open the ZIP file and click on the Finder.
  • Select Command+Shift+G and enter /usr/local/bin to open the bin directory.
  • Copy the extracted ChromeDriver executable file into this directory.

For Linux

  • Extract the ChromeDriver ZIP file by using the unzip command followed by the ZIP file name. For example: unzip ChromeDriver_linux64.zip.
  • You can choose the location of storing the ChromeDriver executable, but generally, people save it in the /usr/local/bin directory..
  • In order to move the ChromeDriver executable to the above-mentioned location, use the command: sudo mv ChromeDriver /usr/local/bin.
  • Once the above step is complete and the file is moved, you need to make it executable.
  • Grant executable permissions using the command run sudo chmod +x /usr/local/bin/ChromeDriver.

Steps to Download ChromeDriver

To download the most recent version of ChromeDriver for Selenium, follow the instructions below – 

Step 1 – Open the official ChromeDriver page –here

Step 2 – The most recent stable ChromeDriver and beta release details are available on this page.

Steps to Download ChromeDriver - recent stable ChromeDriver and beta release

Step 3 – On the Downloads link, click on the appropriate ChromeDriver download for Selenium version according to the Chrome browser version that is installed on your computer.

step 3

Step 4 – This will take you to the ChromeDriver download page, which includes ChromeDriver for the Mac, Windows, and Linux operating systems. Presuming you clicked on the link for ChromeDriver 96, you’ll see something like this –

step 4

Step 5 – Download ChromeDriver for Windows by clicking on chromedriver_win32.zip (or the latest version compatible with your operating system)

Step 6 – Finally, unzip the zip file to retrieve chromedriver.exe

Now, let us look further and learn how to configure ChromeDriver for Selenium.

Steps to Download ChromeDriver for version 115.0 and above

The above steps are for individuals using Chrome browser version below or equal to 114.0.

For everyone else, follow the below steps:

  • Click on Stable and choose the option that suits your system and Chrome specifications.
  • Copy the URL and paste it in a new Window to trigger the download.
  • The ChromeDriver matching your system specifications will get downloaded in your system

Note: This Chrome version is only for testing purpose. It opens with a similar notification on the top.

Rest of the steps to install the driver remains same as above.

Steps to Run Automation Test With Selenium ChromeDriver

In this section, we will see how to execute automation tests using Selenium ChromeDriver now that it is installed and ready for use in your system.

We will write two test scripts, one in Python and another in Java, to open the Simply Travel website of Testsigma and click the login button on the right side. The purpose is to identify a UI element and perform the click action.

Simply Travel is a demo website built by Testsigma to run the tests and understand testing concepts.

Sample test script in Python

Prerequisites:

  • Python
  • Selenium library for Python
  • Chromedriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# Set up the Chrome WebDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

try:
    # Open the Simply Travel website
    driver.get("https://simply-travel.testsigma.com")

    # Maximize the browser window
    driver.maximize_window()

    # Find the login button by its XPath and click it
    login_button = driver.find_element(By.XPATH, "//a[contains(text(), 'login')]")
    login_button.click()

    # Wait for a few seconds to observe the result
    driver.implicitly_wait(5)
    
finally:
    # Close the browser
    driver.quit()

Sample test script in Java

Prerequisites: set up Selenium with your preferred build tool Maven or Gradle.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class TestSimplyTravel {
    public static void main(String[] args) {

        // Set up the Chrome WebDriver
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        
        try {
            // Open the Simply Travel website
            driver.get("https://simply-travel.testsigma.com");
            
            // Maximize the browser window
            driver.manage().window().maximize();
            
            // Find the login button by its XPath and click it
            WebElement loginButton = driver.findElement(By.xpath("//a[contains(text(), 'login')]"));
            loginButton.click();
            
            // Wait for a few seconds to observe the result
            Thread.sleep(5000);
            
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

How to Configure ChromeDriver?

Let us learn how to run Selenium in Chrome using ChromeDriver – 

Step 1 – After you have found and extracted the chromedriver.exe executable file, choose a location for this file and copy it there.

Step 2 – Now, copy the location of the ChromeDriver file’s storage location to set the system’s environment variables.

Step 3 – Click on My Computer and select Properties.

Configure ChromeDriver step 3

Step 4 – After selecting the Change settings option, select the Advanced tab.

Step 5 – Choose Environmental variables now by clicking the Advanced tab.

Configure ChromeDriver step 5

Step 6 – Select the Path option from the list of system variable options, then click Edit.

Configure ChromeDriver step 6

Step 7 – Put a semicolon (‘;’) after the string, paste the path of the ChromeDriver file you previously copied, and then click OK.

Step 8 -In your Python script, import the Selenium web driver module and specify the path to ChromeDriver:

    from selenium import webdriver

    driver = webdriver.Chrome(‘/Users/username/chromedriver’)

Run your script as usual. ChromeDriver will automatically launch the Google Chrome browser and execute your Selenium commands.

How to Configure ChromeDriver in MacOS?    

Chromedriver can be easily configured in macOS by following these steps:

Using your package manager, such as brew or npm, to install the driver is the simplest approach to installing ChromeDriver.

Step 1 – Use the Homebrew package manager in your terminal window to install ChromeDriver with brew install cask ChromeDriver.

Step 2 – Check the version returned by chromedriver –version after it has been installed. It won’t work if there is an error.

(Similar instructions, such as npm install chromedriver, are available for other package managers.)

Step 3 – Use Docker to run Chrome and ChromeDriver in a container. Download the merged container, launch it, and then direct your code to the appropriate location.

Step 4 – Include it in your Selenium setup code and source control check it in just like any other configuration information. If you follow this path, you can add further drivers, such as GeckoDriver (also known as Firefox).

Step 5 – Download the driver, then add the directory where it is to your System PATH.

Advanced Techniques for Selenium ChromeDriver

Besides conducting primary operations, such as finding an element and performing the click function, some advanced techniques are also available to use.

  • Handling Dynamic Elements: Sometimes web applications contain elements that load dynamically and need a wait instruction. Using WebDriverWait, you can wait for elements to appear before interacting with them.
  • Executing JavaScript: To perform actions not directly supported by Selenium, like scrolling or interacting with elements that are not visible, execute JavaScript.
  • Taking Screenshots: Screenshots are important for debugging and documentation after testing is done. Selenium provides a straightforward way to capture screenshots.
  • Headless Browser Testing: Running tests in headless mode can speed up execution and is useful for CI/CD pipelines.
  • Handling Multiple Windows and Frames: Web applications might open new windows or use frames. Selenium can switch between them as needed.
  • Parallel Test Execution: Running tests in parallel is typically achieved using test frameworks like TestNG (for Java) or pytest (for Python).

Steps to Run Selenium Tests on Chrome Browser

Now let’s discuss how to use ChromeDriver to run Selenium scripts on a Chrome Browser.

  • Import all the Selenium dependencies into the Selenium project directory using the Eclipse Integrated Development Environment (IDE). However, users can also use other IDEs according to their preferences.
  • Specify the type of driver that will be used, along with its path describing where it can be found.
  • Set the ChromeDriver object to its initial state. Using this method, you can launch the Chrome browser.
  • Use the driver.get() method to navigate to a particular URL. Specific locators are also available for locating certain web elements.

When you specify the path to chromedriver.exe and create an object of the ChromeDriver class, it will look like this –

Steps to Run Selenium Tests

Output –

output

Wrapping Up

Selenium and Chrome browsers are the most common combinations for testing web applications. When you want to use Selenium to automatically test your web applications, you will need to use a driver. ChromeDriver is one of the most popular drivers, as it offers support for the Google Chrome browser. While it is possible to use ChromeDriver without any additional configuration, we just discussed in detail to show you how to get the most out of ChromeDriver by configuring it in any operating system. 

By default, ChromeDriver will try to launch the latest version of Google Chrome. However, you can specify a different version by setting the ‘CHROME_DRIVER_VERSION’ environment variable. You can now start using Selenium to automate your web browsers. 

Testsigma is an open-source, independent test automation platform ready to support your requirements and demands. With free maintenance testing and unlimited flexibility, you can create reliable, individualized, and trustworthy automated tests without needing to spend time on setup, integration, or programming. 

Frequently Asked Questions

Do we need to install ChromeDriver for Selenium?

Selenium WebDriver uses ChromeDriver to manage Chrome. It is kept up-to-date by the WebDriver contributors and the Chromium team.

The ChromeDriver’s primary function is to start Google Chrome. Without it, it is impossible to automate any website and run Selenium test scripts in Google Chrome. To perform test cases on the Google Chrome browser, you mostly require ChromeDriver.

What is the use of ChromeDriver in Selenium?

A WebDriver implementation called ChromeDriver enables programmers to automate testing for Google Chrome. Without needing to set up a separate testing environment, developers may launch and test their web applications against Chrome by utilizing Google ChromeDriver. 

What is ChromeDriver class in Selenium?

When working with Selenium, you will often hear about the ChromeDriver class. This class is responsible for launching and managing Chrome browser instances. While ChromeDriver does not provide any built-in methods for interacting with the browser, it does expose a number of capabilities that can be used to automate tasks.

For example, you can use ChromeDriver to launch a new browser instance, navigate to a web page, and take a screenshot. You can also use it to execute JavaScript code or retrieve information about the current page. Ultimately, the ChromeDriver class provides a powerful way to automate interactions with the Chrome browser.

What is the difference between WebDriver and ChromeDriver?

WebDriver and ChromeDriver are two popular web-based automation tools. Both can be used to automate web browser interactions, but they differ in how they operate. WebDriver is a tool that is used to automatically control web browsers.

In contrast, ChromeDriver is a tool specifically designed to automate Google Chrome. As a result, it offers better support for features that are specific to Chrome, such as extensions and bookmarks. However, WebDriver can be used with any web browser, making it a more versatile tool. Ultimately, the decision of which tool to use depends on the specific needs of the user.

Which driver is best for Selenium?

Selenium works best with ChromeDriver. Additionally, ChromeDriver offers support for functions like cookies and file uploads that are unavailable in other WebDriver implementations. As a result, Selenium WebDriver uses ChromeDriver, a separate application, to manage Chrome. With assistance from WebDriver contributors, it is maintained by the Chrome team.

Can ChromeDriver work without Chrome?

Generally proven, ChromeDriver cannot work without Chrome. However, it can run on any portable Chrome version.

imageimage
Subscribe to get all our latest blogs, updates delivered directly to your inbox.

RELATED BLOGS


Scriptless Test Automation | What , Why it Matters & Examples
TESTSIGMA ENGINEERING TEAM
TEST AUTOMATION
Top 6 Game Testing Tools You Need to Know
TESTSIGMA ENGINEERING TEAM
TEST AUTOMATION
Power of POC in Testing: Your Exclusive Guide to Success
VIJAYARAGHAVAN VASUDEVAN
AUTOMATION TESTINGTEST AUTOMATION