Exciting New Features in Selenium 4: Enhancements for Efficient Web Automation

Selenium, the leading open-source framework for web testing, is set to unleash a powerful upgrade with the impending release of Selenium 4. It provides a set of tools and libraries for various programming languages, allowing test automation services providers to automate the testing and interaction of web applications. The primary component of Selenium is the WebDriver, which enables users to programmatically control a web browser and simulate user interactions.

Packed with new features and enhancements, Selenium 4 promises to make web automation even more robust and efficient. Some of the exciting features that Selenium 4 brings to the table are accompanied by illustrative examples.

Contact Us For Automation Testing Services

Table of Contents

Exciting New Features in Selenium 4

Relative Locators for Effortless Element Identification

One of the standout features in Selenium 4 is the introduction of Relative Locators. These locators simplify the process of identifying elements based on their proximity to other elements. The new strategies include near, above, below, toLeftOf, and toRightOf.

Consider the following example, where we locate a password input field situated 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

Selenium Grid receives a major overhaul in version 4, promising improved scalability and stability. The enhanced Selenium Grid facilitates parallel execution of tests across multiple nodes, making it an even more powerful tool for large-scale test automation.

Window Management API for Effortless Window Handling

Selenium 4 introduces a Windows Management API, providing a more efficient way to handle browser windows, tabs, and pop-ups. This API simplifies the complexities of window management, making it easier for automation engineers to interact with multiple browser instances.

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

Here, we have covered new features of Selenium 4.0, such as Relative Locators, Multiple Tab/Windows, and others. Selenium 4 not only builds upon the strengths of its predecessor but also introduces features that cater to the evolving needs of web automation testers. Whether it's the adoption of standardization, user-friendly enhancements, or better integration with browser tools, Selenium 4 stands as a robust framework that empowers testers to create and maintain efficient and scalable automated test suites.

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.