What’s New In Selenium 4
September 7, 2021

What’s New In Selenium 4

Open Source Automation

Selenium is a widely used test automation tool for web applications. Due to its robust feature set and proven reliability, it is a staple tool in any development pipeline. It’s no surprise, then, that the eagerly anticipated Selenium 4 release is one of the major milestones of Selenium. Currently available in a beta version (as of August 2021), Selenium 4 aims to introduce an extensive array of new features and improvements.

In this article, we will have a look at the improvements made to the core components in Selenium 4 as well as the new features it introduces.

Back to top

When Was the Official Selenium 4 Release Date?

Selenium 4 announced the initial alpha release (4.0.0.alpha1) in May of 2019. Selenium 4 was officially released on October 13, 2021. 

Selenium 4 Usage

The installation process of Selenium 4 is the same as Selenium 3. We need to install Selenium and then obtain the necessary drivers for the target browsers.

📕 Related Resource: Learn more about How to Run a Selenium Test with TestNG

Installing Selenium 4

The installation process of Selenium 4 is the same as Selenium 3. First, install Selenium 4 using pip, since we are using Python. Make sure to specify the required Selenium version in the installation command. In this instance, its 4.0.0.beta4 release.

pip install selenium==4.0.0.b4

 

Selenium 4 installation

Then, simply download the drivers for the required browsers and add them to the system PATH or simply point to the driver in the code. The following code sample shows how to point to the chrome driver without adding it to the system PATH.

from selenium.webdriver import Chrome
 
# Point to the Driver
driver = Chrome(executable_path='G:\\chromedriver\\chromedriver.exe')
driver.get("https://www.google.com")

 

Upgrade to Selenium 4

The upgrade process will depend on the programming language and the build tool you use. When using Python, if you have an existing Selenium 3 installation, you can simply upgrade it to Selenium 4 via pip. 

pip install --upgrade selenium==4.0.0.b4

 

Selenium 4 upgrade

If you are using a build tool like Maven, simply change the dependencies section to reflect the new Selenium 4 driver, as shown below.

        org.seleniumhq.selenium
        selenium-java
        
        4.0.0-beta-4

 

Back to top

Selenium 4 Core Component Improvements

Selenium 4 WebDriver Improvements

The primary change in the WebDriver is the WebDriver APIs have been made completely W3C compliant. Therefore, encoding and decoding API requests is no longer needed when interacting with different browsers. Instead, this standardization has enabled the WebDriver to communicate with the targeted browsers directly.

This W3C standardization of the WebDriver has led to the depreciation of JSON wire protocol. But don’t worry: it will not impact the existing users, since most browser drivers have adapted W3C standardized protocols. This ensures a smoother transition from Selenium 3 to 4. Furthermore, this architectural improvement has made Selenium 4 a much more stable and efficient test automation tool.

Selenium 4 IDE Improvements

The Selenium IDE is used to record user interactions with web pages, allowing developers to create test cases by recording workflows and user behavior. Selenium 4 aims to provide a much more robust IDE with support for multiple browsers and platforms, from Chrome and Firefox to even direct integration with Microsoft Edge.

Some improved features of the Selenium 4 IDE are as follows:

  • Flow control improvements allowing users to incorporate better conditional statements and loops in test cases.
  • Improved code export functionality. Recording can be easily exported in the desired language as code blocks.
  • Improved element location functionality. The IDE is now able to locate hard-to-find elements within web pages, which in turn lead to more stable and targeted tests.

Selenium 4 Grid Improvements

Selenium Grid enables users to run tests across various browsers and devices parallelly, using the hub and node model. In Selenium 4, the Grid has been improved with Docker support and the ability to easily be deployed on Kubernetes clusters. This containerized approach allows users to easily distribute the test workload along with multiple containers without depending on physical or virtualized machines.

Selenium 4 Grid will offer three deployment methods;

  • Standalone
  • Hub and Node method
  • Containerized method

Additionally, some other improvements such as standardization of configuration files to utilize TOML, usability improvement in the GUI, and full IPv6 support were made to the Grid in Selenium 4, aiming to increase its usefulness further.

Back to top

Selenium 4 Feature Improvements

Standardized for W3C Compliance

As discussed earlier, the Selenium WebDriver has adopted the standardized W3C protocol for communications with browsers. Since major browser drivers like ChromeDriver and geckodriver already follow the W3C standard, moving to W3C as the default protocol has enabled Selenium to communicate with browsers directly.

Moreover, this shift to the W3C protocol has increased the reliability and speed of the Selenium components. With this W3C protocol becoming the default protocol in Selenium 4, the JSON Wire protocol is deprecated and native support for Opera and PhantomJS has been removed. Since the Opera browser is now based on Chromium, users who want to test their implementation on Opera can utilize the chromedriver. Besides, PhantomJS users can test on FireFox or Chrome in headless as an alternative to PhantomJS.

Updated Actions API

Selenium 4 has updated the Actions class (API) that simulates user inputs from mouse, touch, and keyboards such as clicks, double clicks, etc. Following is the list of newly introduced methods to the Actions class.

click(WebElement)

This method is used to click on a specific web element and it will replace the moveToElement(onElement).click()

clickAndHolde(WebElement)

This is used to click on an element and hold the click without releasing. This method will replace the  moveToElement(onElement).clickAndHold()

contextClick(WebElement)

This will be the default method for right click operations and replace moveToElement(onElement).contextClick()

doubleClick(WebElement)

This will perform a double click operation on an element and replace the moveToElement(element).doubleClick() method

release()

The release() method can be used to release a left mouse click at the current cursor location. With the updated version of Selenium, It has been moved to the primary Actions class from the org.openqa.selenium.interactions.ButtonReleaseAction class.

Relative Locators

Selenium 4 introduces a convenient way of locating web elements on a page with relative locators. These relative locators can be called using friendly names such as;

  • to left of
  • to right of
  • above
  • below
  • near

This intuitive naming allows users to locate web page elements based on their visual location with respect to other elements. For a simple example, If there are two buttons in a form (submit and cancel) located near each other, we can simply call the "To right of" method to locate the other button.

Example

from selenium.webdriver.support.relative_locator import with_tag_name
 
submitButton = driver.find_element(By.ID, "submit")
cancelButton = driver.find_element(with_tag_name("button").
                                   to_right_of(submitButton))

 

 

Multiple Window and Tabs Management

The "newWindow" method of Selenium 4 allows users to simply create new windows or switch between tabs without creating separate WebDriver objects and performing switch operations using the WindowHandle method.

Examples:

# Open a New Tab
driver.switch_to.new_window('tab')
 
# Open a New Window
driver.switch_to.new_window('window')

 

 

Chrome DevTool Protocol

Selenium 4 offers native support for the Chrome DevTools protocol, allowing users to utilize native chrome development tools and properties like profilers, network monitoring, caching, etc.

These native integrations enable users to use DevTools API to perform various tasks such as network performance simulations and geolocation testing while quickly addressing any bugs.

Example - Geolocation Emulation:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
 
def geo_location_test():
    driver = webdriver.Chrome(executable_path='G:\\chromedriver\\chromedriver.exe')
    # Create Geo Location
    map_coordinates = dict({
        "latitude": 38.134557,
        "longitude": -122.709826,
        "accuracy": 100
        })
    # Emulate Location
    driver.execute_cdp_cmd("Emulation.setGeolocationOverride", map_coordinates)
    # Navigate to the Website
    driver.get("https://www.google.com") 

 

 

Replacing Desired Capabilities with Options

In Selenium 4, Desired Capabilities that were used to define the testing environment such as browser name, version, os are replaced by an options object.

These options are browser-specific, and users will have to create an options object with the test environment requirements and pass it to the WebDriver. The list below indicates the options object for major web browsers.

  • Chrome – ChromeOptions
  • Microsoft Edge – EdgeOptions
  • Firefox – FirefoxOptions
  • Safari – SafariOptions
  • Internet Explorer (IE) – InternetExplorerOptions

Example - Chrome Options:

from selenium.webdriver.chrome.options import Options
 
browser_options = Options()
browser_options.headless = True
driver = webdriver.Chrome(executable_path='G:\chromedriver\chromedriver.exe',options=browser_options)  

 

 

Improved Documentation

Another usability improvement of Selenium 4 is enhanced documentation. The official documentation of Selenium 4 has been improved with clear explanations, code snippets for various languages, and an easily accessible and searchable documentation section.

This documentation covers all aspects of Selenium, including the WebDriver, IDE, and Grid components. Moreover, this clear and exhaustive documentation helps testers easily understand how new features offered by Selenium 4 can be used in testing.

Back to top

Bottom Line

Selenium 4 comes with a major architectural shift with some long-awaited improvements to provide a feature-rich and stable test automation platform. The complete compliance with the W3C protocol ensures that Selenium is capable of handling any kind of browser automation for the foreseeable future. When this is coupled with all the new features, usability, and performance improvements, users can confidently use Selenium in their projects without the fear of the tool becoming obsolete.

To get started with running your Selenium functional tests in BlazeMeter for advanced reporting and cloud-based load testing, sign up for free.

Start Testing Now

 

Related Resources

 

Back to top