What's New in Selenium 4: Web Automation Upgrades

Table of Content

Selenium is the most widely used open-source web site testing tool, and it will receive a huge boost with the launch of Selenium 4. Selenium is a set of tools and libraries for various programming languages, which makes it easy for test automation services providers to automate web application testing and interaction. Selenium Automation Testing Services leverage these capabilities to ensure seamless testing experiences and enhanced efficiency. WebDriver is at the core of Selenium, which allows the users to program control of a web browser and simulate user actions.

Selenium 4 has new features and enhancements that can potentially enhance web automation with more power and efficiency. New and exciting features of Selenium 4 are explained with examples.

Exciting New Features in Selenium 4

Relative Locators for Effortless Element Identification

Relative Locators are among the most significant additions in Selenium 4. Relative locators simplify identifying elements based on where the elements are in relation to one another. New element locators include near, above, below, to the left, and to the right.

Below is an example where a password input field is rendered below the username input field:

WebElement usernameInput = driver.findElement(By.id("username"));
WebElement passwordInput = driver.findElement(RelativeLocator.withTagName("input").below(usernameInput));

This enhancement streamlines the code for locating elements about each other, promoting cleaner and more maintainable scripts.

Revamped Selenium Grid for Improved Scalability

Version 4 of Selenium Grid is a significant update that promises increased stability and scalability. The improved Selenium Grid is an even more potent tool for large-scale test automation since it allows tests to run in parallel across several nodes.

Window Management API for Effortless Window Handling

With the introduction of the Windows Management API in Selenium 4, browser windows, tabs, and pop-ups may be handled more effectively. Automation engineers may interact with many browser instances more easily thanks to this API, which streamlines the difficult window management process.

Here's an example of opening a new browser tab using the Window Management API:

This feature enhances the control and flexibility testers have over managing browser instances.

A new window is opened and switches to it

driver.switchTo().newWindow(WindowType.WINDOW);

Loads URL open in the newly opened window

driver.get("URL");

A new tab is opened and switches to it

driver.switchTo().newWindow(WindowType.TAB);

Loads URL in the newly opened Tab

driver.get("URL");

Chrome DevTools Protocol Integration for Advanced Browser Interactions

Selenium 4 integrates the Chrome DevTools Protocol directly into the WebDriver, offering advanced browser control and interaction capabilities. This integration opens new possibilities for debugging and performance analysis during test execution.

To enable the Chrome DevTools Protocol, you can use the following code:

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("w3c", false);
options.setExperimentalOption("devTools", List.of("console"));
WebDriver driver = new ChromeDriver(options);

This integration empowers testers to leverage the full potential of Chrome DevTools directly within their Selenium scripts.

Relative URL Support in Navigation for Seamless Page Transitions

Selenium 4 brings a significant improvement in navigation by supporting relative URLs. This allows testers to navigate to URLs relative to the current page, simplifying navigation workflows.

// Navigate to a relative URL
driver.navigate().to("/path/to/page");

This feature contributes to cleaner and more concise navigation code.

HTTP/2 Support for Enhanced Performance

Selenium 4 embraces the HTTP/2 protocol, a major advancement for web automation. The adoption of HTTP/2 enhances the performance of Selenium scripts, leading to faster and more efficient test executions.

Deprecation of Desired Capabilities

Desired Capabilities were primarily used in the test scripts to define the test environment (browser name, version, operating system) for execution in Selenium.

In Selenium 4, capabilities objects are replaced with Options. This means testers now need to create an Options object, set test requirements, and pass the object to the Driver constructor.

Listed below are the Options objects to be used for defining browser-specific capabilities:

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

Modifications in the Actions Class

Actions class in Selenium is primarily used to simulate input actions from the mouse and keyboard on specific web elements (For, e.g.: Left click, Right click, Double click, etc.)

In Selenium 4, several new methods have been added to the Actions class:

click(WebElement)

This method is added to the Actions class to replace the moveToElement(onElement).click(). It is used to click on a certain web element.

clickAndHold(WebElement)

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

contextClick(WebElement)

This method will replace moveToElement(onElement).contextClick(). It will perform the right-click operation.

doubleClick(WebElement)

This method is added to replace moveToElement(element).doubleClick(). It will perform a double-click on an element.

release()

This method (user for releasing the pressed mouse button) was initially a part of org.openqa.selenium.interactions.ButtonReleaseAction class. Now, with the updated version of Selenium, it has been moved to the Actions class.

Conclusion

Relative Locators, Multiple Tab/Windows, and other new Selenium 4.0 features have been discussed here. In addition to improving on the advantages of its predecessor, Selenium 4 adds functionality to meet the changing demands of web automation testers. Selenium 4 is a strong framework that enables testers to build and manage effective and scalable automated test suites, whether through standardization, user-friendly improvements, or improved integration with browser tools.

We hope Selenium 4.0 gives us the best experience and ability to do almost all the tasks that are unfulfilled in Selenium 3. Hire PixelQA, a leading software testing company, and see the difference in your software’s final outcome.

About Author

Jigna JadavJigna Jadav started her journey as a QA executive in manual testing and moved her career up to QA Team Leader. She aspires to train and mentor her juniors and enhance the automation infrastructure.