Test Automation Using Selenium ChromeDriver
As per the browser market share, Google Chrome is said to be the most used cross-platform browser in the world. Every new chrome version comes up with an exciting feature that hikes the importance and usage of the chrome browser. Hence, it becomes essential to test our web application on such a high-rated browser.
Performing different test cases manually on different chrome versions can be hectic and challenging. To overcome this challenge, it is necessary to perform test automation on the chrome browser.
Selenium is an open-source project offering a variety of tools and libraries for web browser automation. It is primarily used to write scripts to automate the end-user interactions and to test site functionality in a much faster way.
Chrome officially provides an OS-dependent driver which establishes a connection between Selenium WebDriver and Google Chrome browser. Once the connection gets established, we are good to go with selenium tests on the chrome browser.
What Is Selenium ChromeDriver?
ChromeDriver is a standalone server that develops a communication medium with Selenium WebDriver Chromium Protocol. The two protocols that are being used by Selenium ChromeDriver to interact with the browser are JsonWireProtocol and W3C Protocol. These two protocols are responsible for translating the Selenium-based commands into the corresponding actions on the chrome browser.
The primary purpose of Selenium ChromeDriver is to launch the browser and perform the desired automated operations. The
ChromeDriver now comes up with different capabilities, for example: running tests in incognito mode, headless mode, disable extensions and pop-ups, etc.
The general syntax to setup Selenium ChromeDriver is:
In the above syntax, WebDriver is an interface that is being extended by ChromeDriver class, hence, all the methods which are declared in the WebDriver interface are implemented by the respective driver class.
To run our Selenium tests on Chrome browser, it is important to have a ChromeDriver executable file in our Selenium project. To download the respective ChromeDriver, we need to check the current chrome version installed in our testing machine. According to the chrome browser version, we need to download the compatible ChromeDriver. Here is the link to download the ChromeDriver for your Selenium project.
As per the below screenshot, the Chrome version installed in the testing machine is 88.0.4324.96, hence, the ChromeDriver to be installed must be of version 88.0.4324.96 as well.
To run our Selenium tests on chrome browser, it is important to have a ChromeDriver executable file in our Selenium project. To download the respective ChromeDriver, we need to check the current chrome version installed in our testing machine. According to the chrome browser version, we need to download the compatible ChromeDriver. Here is the link to download the ChromeDriverfor your Selenium project.
As per the below screenshot, the chrome version installed in the testing machine is 88.0.4324.96, hence, the ChromeDriver to be installed must be of version 88.0.4324.96 as well.
Note: Since the ChromeDriver is OS-dependent, you will need to make sure you download the chromedriver version that is OS-compatible.
Setting Up Maven Project To Run Our First Selenium Based Chrome Test
There are a few prerequisites that need to be taken care of before running your first Selenium test:
- Eclipse or Intellij IDE
To avoid downloading any dependency, it is a good practice to create a maven project which allows you to directly add all dependencies in a pom.xml file.
Now since we have downloaded the ChromeDriver, we just need to add two dependencies in our pom file.
1. Selenium Dependency
2. TestNG Dependency
For example, let’s try to launch a pCloudy login page via Google Search.
In the above example, we have demonstrated a TestNG framework where we have used BeforeTest annotation to launch and maximize the browser before the actual test begins. To launch the browser, we have used the
setProperty() method to specify the location of the ChromeDriver executable file that we downloaded according to the chrome version installed in the system.
The next command is the primary command that launches the web browser (syntax explained above). In the main test method, we have Google searched the pCloudy keyword and have directed to the pCloudy login page. And at the end, we have used AfterTest annotation to quit the chrome browser once the test method execution gets completed.
Important Note: Instead of specifying the ChromeDriver path in every Selenium project using the
setProperty() method, we can also save the ChromeDriver path in environment variables.
Use Of ChromeOptions Class
ChromeOptions class provides some advanced arguments to manipulate Selenium ChromeDriver properties. This class can also be used in conjunction with Desired Capabilities. Below are the lists of operations that are commonly used with ChromeOptions class:
- Start-maximized: Opens chrome browser in maximized mode
- Headless: Launch chrome in headless mode i.e. without GUI
- Incognito: Launch chrome browser in incognito mode
- Version: Displays/print chrome browser version
- Disable -extensions: Disable existing chrome extensions
- Disable-popup-blocking: Disable pop-ups being displayed on chrome
The General Syntax To Declare Chromeoptions Class
Now since we have understood the basic usage of ChromeOptions class, let’s try to understand this better with some of the above-defined operations:
Here we have used the same test method as taken in the previous example. The only difference that we have initiated here is the use of ChromeOptions class. With the above example, we are running our test script in incognito mode with a maximized browser window. The operations that we have declared i.e. incognito and start-maximized have added arguments to the ChromeOptions object. The ChromeOptions object is then passed in the default constructor of ChromeDriver.
Here is a list of a few other advanced methods provided by ChromeOptions Class to set up the Selenium ChromeDriver properties; let’s have a quick look at those as well:
1. To add a new extension
This method is to add an extension to the chrome browser while running your automation test. All extensions are stored in the system with the .crx extension.
2. To add a new binary path
This method is used to specify the binary file path. The binary file path can be of chrome binary or any other binary being used in automated tests.
3. To accept an untrusted certificate
This method allows the chrome browser to accept insecure website certificates.
Manage Chrome Binary With WebDriverManager
As discussed above, we always have to download relevant compatible ChromeDriver versions to run our automated scripts on the chrome browser. In a case where your chrome browser gets updated to a newer version, then downloading the new compatible ChromeDriver would be mandatory. These steps become cumbersome as the chrome versions keep on updating.
To overcome this phase, we have another open-source project named “WebDriverManager” that automates the management of different browser drivers.
Importing WebDriverManager in our project avoids the explicit downloading of browser drivers and thus avoids the use of the
setProperty() method to specify the browser driver path.
To import WebDriverManager in your maven-selenium project, you need to add its maven dependency in the pom.xml file:
Now since we have added WebDriverManager maven dependency, let’s look at the general syntax that is being used to instantiate a browser using WebDriverManager in Selenium.
With the above syntax, WebDriverManager does the magic for you:
- It checks the browser version installed in your machine (e.g. Chrome, Firefox).
- It matches the version of the driver (e.g. ChromeDriver, GeckoDriver).
- If an unknown version is found, it uses the latest version of the driver. It downloads the driver if it is not present on the WebDriverManager cache (~/.cache/selenium by default).
- It exports the Selenium required WebDriver Java environment variables.
Let’s have a look at a practical example using WebDriverManager:
For more details on WebDriverManager, please have a look at its official GitHub repository.
Google Chrome is known to be one of the most popular browsers in the market, and hence the need to automate the browser testing of your web app on the Chrome browser becomes absolutely crucial. Thankfully, using the ChromeDriver we can access the browser with ease to perform the Selenium test automation.
Credit: Source link