Selenium 4 New Features

A beta version of Selenium 4 became available for developers to try in February 2021. In October 2021, Selenium announced the first stable release of Selenium 4...

A beta version of Selenium 4 became available for developers to try in February 2021. In October 2021, Selenium announced the first stable release of Selenium 4. Some of the new features offered in Selenium 4 include

Relative Locators in Selenium 4

Selenium has introduced five relative locators with Selenium 4

  • above ( )
  • below ( )
  • toLeftOf( )
  • toRightOf( )
  • near ( )

we can now use relative locators to identify a web element in relation to other web elements.

New Methods in Action Class

Action class in Selenium provides methods to automate mouse and keyboard user interactions with WebElements of the web applications.

  • doubleClick( ): This method is used to perform a double click on a WebElement. this method replaces moveToElement(WebElement).doubleClick( ) in Selenium 4
  • contextClick ( ): This method is used to perform a right click on a WebElement. This method replaces moveToElement(WebElement).contextClick( ) in Selenium 4
  • clickAndHold ( ): This method is used to perform a click action on a WebElement without the mouse. This replaces moveToElement(WebElement).clickAndHold() in Selenium 4
  • release ( ): This method is used for releasing the depressed left mouse button. This method already exists in earlier versions of Selenium. In Selenium 4, this method is moved from org.openqa.interactions.ButtonReleaseActionclass to the Action class
  • release(WebElement): This method is used for releasing the depressed left mouse button. this method replaces moveToElement(WebElement).release( ) in Selenium 4

Changes in FluentWait Class

  • Selenium 4 has replaced the TimeUnit with Duration. The Duration class can be imported from the java.time.package and has methods to represent time duration in nano, milli, minutes, hours, days, and so on.
  • In Selenium 4, implicitlyWait takes only one parameter

New Methods for Browser Operations

  • newWindow ( ): We can create and open a brand new or tabbed window in the same session without creating a new driver object to manage multiple applications in the same test using the newWindow( ) method. To switch between these windows, you must fetch the window ID just like we did in the previous Selenium WebDriver.

Deprecation of DesiredCapabilities

DesiredCapabilities were primarily used in the test scripts to define the test environment (Browser name, version, operating system, etc.) for execution on the Selenium grid. In Selenium 4 capabilities, objects are replaced with Options. This means testers must use browser-specific capabilities like ChromeOptions, SafariOptions, etc.

DesiredCapabilities will still work if you suppress the warnings. However, changing to the new method is the most robust and future-proof solution.

Selenium Grid 4

  • Grid in Selenium 4 has a user-friendly graphical user interface.
  • The new Selenium Grid in Selenium 4 comes with Docker support. This will enable developers or testers to spin up the containers.
  • Users have flexible options to deploy the Selenium Grid on Kubernetes-based infrastructure for better scaling.
  • The configuration files used for spinning up the grid instance can be written in TOML, making it easier for humans to understand.

Chrome DevTool Protocol

Selenium 4 provides 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.

WebElement Screenshots

In Selenium 3, users can only take a screenshot of a web page, not a particular element, But with Selenium 4, we can take a screenshot at

  • the element level (such as a button or a paragraph or a text)
  • section level
  • and full page level.

To use this feature, first, you find the element, then take a screenshot of it by calling the getScreenshotAs( ) method of WebElement.

Related Articles