What’s New In Selenium 4
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.
The Official Release of Selenium 4
Selenium 4 announced the initial alpha release (4.0.0.alpha1) in May of 2019. With consistent updates, now it is in the beta phase with the latest driver (4.0.0.beta4) released in June of 2021. The official stable release of Selenium 4 has not been announced as of yet. However, the wider consensus of the developer community is that Selenium 4 is ready for a release candidate, with a stable release coming shortly after.
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.
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, it's 4.0.0.beta4 release.
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.
Upgrade Selenium from 3 to 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.
If you are using a build tool like Maven, simply change the dependencies section to reflect the new Selenium 4 driver, as shown below.
Improvements in Core Components
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;
- 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.
Feature Improvements in Selenium 4
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.
This method is used to click on a specific web element and it will replace the moveToElement(onElement).click()
This is used to click on an element and hold the click without releasing. This method will replace the moveToElement(onElement).clickAndHold()
This will be the default method for right click operations and replace moveToElement(onElement).contextClick()
This will perform a double click operation on an element and replace the moveToElement(element).doubleClick() method
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.
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
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.
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.
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
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
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.
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.