Blog Details Shape

What’s New in Selenium 4 - Advanced Key Features

Pratik Patel
By
Pratik Patel
  • Oct 22, 2024
  • Clock
    8 min read
What’s New in Selenium 4 - Advanced Key Features
Contents
Join 1,241 readers who are obsessed with testing.
Consult the author or an expert on this topic.

Testing web applications is more difficult today than ever before, as testing on the web is time-consuming. Complexity increases with technology, and businesses and developers need efficient tools to deal with it. This is where Selenium became one of the most widely used test automation tools and has been crucial in reducing testing efforts as well as improving user experience. 

Selenium is an open-source tool developed to automate web browsers. This gives testers the ability to use the scripts in popular languages such as Java, Python, Perl, and JavaScript and brings seamless end-to-end testing to web applications. With Selenium, besides testing, you’re also shortening the process, improving quality and accuracy.

Selenium helps you perform functional testing on websites across several browsers to test web applications to find out if they work consistently. The cross-browser testing feature allows the site to work on browsers such as Chrome, Firefox, Edge, and Safari and verifies that the website loads properly on these browsers. 

Have you noticed the difference between Selenium 3 and Selenium 4? Alphabin, focus on automation and web testing using Selenium 4 and the latest best tools. But our expertise always puts us a step ahead of dynamic trends, adopting the advanced features of Selenium 4 to help us provide an accurate and effective testing solution.

{{cta-image}}

What’s New in Selenium 4?

Selenium 4 is here, and it’s effective and better than ever! It’s the latest version of the Selenium framework, with lots of exciting upgrades that make web browser automation even more powerful. If you know Selenium 3, you’re ready for an updated one. Selenium 4 has better performance, smoother testing across the board, and allows you to test across many different browsers. 

Selenium 4 brings W3C WebDriver protocol for desired enhanced browser compatibility, new features of the relative locatorsChrome DevTools Protocol (CDP), and a new and improved Selenium Grid with Docker Container support. It also includes automatic WebDriver management, improved tab handling, and an updated Selenium IDE. Functionalities such as DesiredCapabilities are now considered obsolete for the ease of automation.

Key Features of Selenium 4

Selenium 4 was released, and it comes with exciting and new features like an enhanced Selenium grid, updated Selenium IDE, relative locator, and many more that make Selenium 4 the most efficient and accurate for web application automation. Now let’s take a closer look at features.

Key features of Selenium 4

1. Enhanced Selenium Grid

Selenium Grid has been redesigned and enhanced in Selenium 4 for better scaling, performance, and consumption to execute the test. It enables the users to run concurrent tests for (browser, device), reducing testing time.

  • Docker and Kubernetes Integration: With the support of out-of-the-box Docker and Kubernetes, Selenium Grid is now capable of running its instances in isolated containers, thus providing a leveled-up and elastic environment for testing. This allows resource utilization to be properly coordinated and for tests to run more smoothly across multiple platforms.
  • IPv6 and HTTPS Support: Now with IPv6 becoming an added feature with Selenium Grid, we can now use IPv6 networks for messaging. An HTTPS protocol can now be used for communication with the grid, which makes better security possible.
  • TOML Configuration Files: The allocation procedure for Selenium Grid has also been enhanced to use simple-to-read and change TOML files. It also makes it easier for those with large-scale testing environments to organize themselves onto the grid.
  • Compatibility with Cloud Platforms: Selenium 4 is compatible with several cloud-based solutions, so distributed teams can grow their testing capacity rapidly while avoiding concerns about physical resources.

2. Upgraded Selenium IDE

This Selenium IDE has been further developed, and it has become much easier to use and offers more options. It enables users to be able to record, edit, and debug tests in a very efficient manner, most specifically when it comes to those users who may not have a good understanding of using scripts.

  • Updated GUI: The new release of Selenium IDE includes changes in the graphical user interface (GUI), which has been improved for enhanced usability. An organized interface makes it easier to create and run a set of test cases.
  • Enhanced Element Locator Strategy: Selenium 4 has come with Relative Locators, where users can find elements with respect to some other elements on the page. This makes it easier to locate an element on the screen, so when dealing with dynamic or complex layouts, it is easy to find the right element.
  • Improved Control Flow Mechanism: It is now possible to include such advanced commands as conditionals and loops to the control flow of IDE and create much more complex tests with no external script programming.

For example, using if-else conditions in Selenium IDE to check if a button is visible before clicking it, code written in JavaScript:

if (isElementPresent("css=button#submit")) {
    click("css=button#submit");
} else {
    echo("Button not found");
}
Copied!

3. Relative Locators in Selenium 4

Relative Locators are one of the great things available in Selenium 4. Such locators make it possible to reference elements with relation to other elements (such as above, below, close to, next to the left of, or right of). This feature simplifies the task of finding elements in a complex layout that tends to break through standard locators. 

Example: Locating a button that is below a specific heading using relative locators, code written in Java:

WebElement heading = driver.findElement(By.tagName("h1"));
WebElement button = driver.findElement(RelativeLocator.withTagName("button").below(heading));
button.click();
Copied!

4. Chrome DevTools Protocol (CDP) Integration

With Selenium 4, the Chrome DevTools Protocol has been integrated into it, which means that you have direct access to the amazing features that Chrome’s developer tools have to offer. This integration provides testers with greater control over the browser and offers new capabilities such as:

  • Network Interception: It’s useful since testers can simulate network conditions, monitor requests, and control the browser's network activities, that is, to test the performance of applications under different network conditions.
  • Performance Metrics: By helping test performance and optimize, the CDP integration allows testers to pull vital performance metrics like page load times and memory usage.

For example, simulating network throttling to test how a webpage performs under a slow network, code written in Java:

DevTools devTools = ((ChromeDriver) driver).getDevTools();
devTools.createSession();
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTools.send(Network.emulateNetworkConditions(false, 100, 5000, 2000, Optional.of(ConnectionType.CELLULAR3G)));
driver.get("https://alphabin.co");
Copied!

5. Tab Management in Selenium 4

In Selenium 3, it was difficult to manage multiple windows/tabs, which required testing to create a new WebDriver object and switch up windows or tabs using a unique WindowHandle. 

But, in Selenium 4 with the newWindow API, this process gets simpler. Now, testers can easily create and switch into a new window or tab within the same test flow or session without having to load a new WebDriver object. 

For example, here is a main function code using the newWindow API in Selenium 4, code written in Java:

public static void main(String[] args) {
    // Set the path for the ChromeDriver
    System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");

    // Create a new instance of ChromeDriver (WebDriver)
    WebDriver driver = new ChromeDriver();

    // Navigate to the initial URL
    driver.get("https://www.example.com");

    // Open a new tab and switch to it
    driver.switchTo().newWindow(WindowType.TAB).get("https://www.google.com");

    // Open a new window and switch to it
    driver.switchTo().newWindow(WindowType.WINDOW).get("https://www.bing.com");

    // Perform any necessary actions here

    // Close the driver at the end
    driver.quit();
}
Copied!

6. Improved Documentation

Selenium 4 made the documentation part easier for testers to get started. The new version boasts a completely revamped UI in which you can smoothly move through all the different sections. The details of every tool and API within the Selenium suite are covered in it, saving you from searching others for your resources. 

This enhanced documentation allows testers to easily find everything needed to start (and continue!) with Selenium testing, from beginner guides to advanced features, resulting in a smoother learning curve and faster, more efficient test automation.

What has Changed and Deprecated in Selenium 4?

There are several key updates in Selenium 4, which help browsers automate. These advancements are positive ones, but they bring about changes and the deprecation of older elements that are replaced by more efficient elements. If you do not understand these deprecations, a major part of the Selenium 4 ecosystem will not be compatible and optimized for your current Selenium test automation scripts. Let’s take a closer look at what has changed and what has been deprecated.

What has changed and deprecated in Selenium 4?

Deprecation of Desired Capabilities

In past versions of Selenium, the test environment—including browser name, version, and operating system—for Selenium Grid execution was often specified using Desired Capabilities. 

However, it has been improved with Selenium 4. Instead of Desired Capabilities, testers define what browser stability level to use through Options objects. Set your desired test configurations using an Options object and pass it directly to the WebDriver constructor. This helps with browser configuration, making the options simpler and more organized.

Here are the Options objects you'll use for different browsers:

  • Chrome: ChromeOptions
  • Firefox: FirefoxOptions
  • Microsoft Edge: EdgeOptions
  • Safari: SafariOptions

Example

For Firefox using

System.setProperty("webdriver.gecko.driver", "path_to_geckodriver");

        // Create FirefoxOptions object
        FirefoxOptions options = new FirefoxOptions();
        
        // Set desired capabilities using FirefoxOptions
        options.addArguments("-private");  // Start Firefox in private mode
        options.setHeadless(true);         // Run Firefox in headless mode

        // Pass FirefoxOptions object to the WebDriver constructor
        WebDriver driver = new FirefoxDriver(options);

        // Navigate to a URL
        driver.get("https://www.google.com");
Copied!

Deprecation of the FindsBy Interface

In Selenium 3, FindsBy interfaces from the org.openqa.selenium.internal package, which included findElement(By) and findElements(By), were used to locate web elements. In Selenium 4, these methods are deprecated, offering more efficient and streamlined ways to handle element interactions, aligned with modern web standards. 

Example

Here's an example that demonstrates the change in element-finding methods using a modern approach.

System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
        WebDriver driver = new ChromeDriver();

        // Same usage, but improved internally in Selenium 4 for better performance
        WebElement element = driver.findElement(By.id("example-id"));
        System.out.println(element.getText());
Copied!

Changes in Action Class

The Selenium 4 actions class for simulating keyboard and mouse actions on web elements has improved for simplicity and efficiency. Here's a summary of the key changes: 

  • Click(WebElement) now replaces the older method moveToElement(onElement).click(), making it more straightforward to perform a click action directly on a web element.
  • ClickAndHold(WebElement) has simplified the process, replacing moveToElement(onElement).clickAndHold() to hold down the mouse on a specific element.
  • ContextClick(WebElement) replaces moveToElement(onElement).contextClick(), allowing a right-click action directly on a web element without needing the extra move-to step.
  • DoubleClick (WebElement) now directly replaces moveToElement(element).doubleClick(), making it easier to perform a double-click on elements.
  • Release() method, which was previously part of the ButtonReleaseAction class, has now been moved to the main Action class in Selenium 4, simplifying the overall structure and making it easier to manage actions.

Example

Here's an example demonstrating the updated usage of the Action class in Selenium 4

System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
        WebDriver driver = new ChromeDriver();

        // Navigate to the webpage
        driver.get("https://www.example.com");

        // Create an instance of the Actions class
        Actions actions = new Actions(driver);

        // Find the WebElement you want to interact with
        WebElement element = driver.findElement(By.id("element-id"));

        // Click on the element using the updated click() method
        actions.click(element).perform();

        // Click and hold the element using the updated clickAndHold() method
        actions.clickAndHold(element).perform();

        // Perform a right-click (context click) on the element using contextClick()
        actions.contextClick(element).perform();

        // Perform a double-click on the element using doubleClick()
        actions.doubleClick(element).perform();

        // Release the click using the updated release() method
        actions.release().perform();
Copied!

Modifications to the FluentWait Class

FluentWait class methods in Selenium 4 have their changes made to withTimeout() and pollingEvery(). Both methods now accept only one parameter: Duration. You can stop setting multiple arguments when calling the pollingEvery() method, and you’ll only pass in the Duration, which can be specified in units of seconds, milliseconds, nanoseconds, hours, days, etc. Similarly, withTimeout() too takes a single Duration for specifying the maximum wait time.

Example

Here's an example demonstrating how to use the updated withTimeout() and pollingEvery() methods in Selenium 4's FluentWait class with the new Duration parameter:

System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
        WebDriver driver = new ChromeDriver();

        // Navigate to a webpage
        driver.get("https://www.example.com");

        // Create FluentWait instance with the new Duration parameter
        Wait<WebDriver> wait = new FluentWait<>(driver)
                // Set the maximum wait time to 30 seconds using withTimeout(Duration)
                .withTimeout(Duration.ofSeconds(30))
                // Set the polling interval to 5 seconds using pollingEvery(Duration)
                .pollingEvery(Duration.ofSeconds(5))
                // Ignore exceptions like NoSuchElementException during polling
                .ignoring(NoSuchElementException.class);

        // Use FluentWait to wait for an element to be present on the page
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("element-id")));
Copied!

Difference Between Selenium 3 and Selenium 4

Here are the differences between the older version and newest version of Selenium

Features Selenium 3 Selenium 4
Protocol JSON Wire Protocol W3C WebDriver Standard
Browser Compatibility Browser interaction limited Enhanced browser interaction
Element Locator Relied on standard locators (ID, name, XPath, etc.) Introduction of relative locator
Tab Management Provide limited support for managing multiple browsers. Full tab management functionality
DevTools Integration No direct integration with Chrome DevTools. Integrated Chrome DevTools Protocol (CDP)

{{cta-image-second}}

Conclusion

In Selenium 4, they bring lots of changes that make testing the web much easier, with improved browser compatibility, relative locators, and integration with developer tools. Adaptability to the growing complexity of web applications is made possible by these advancements, which simplify automation tasks, enhance performance, and allow more control over testing. 

Alphabin is very good at automation testing using Selenium 4's strong features. We guarantee our software testing solution will be as seamless and efficient as it can be. We provide clients with faster, more reliable test execution and robust cross-browser testing, allowing businesses to deliver high-quality applications with confidence.

Something you should read...

Frequently Asked Questions

What is Selenium, and why is it widely used in automation testing?
FAQ ArrowFAQ Minus Arrow

Selenium is an open-source tool that automates web browsers, enabling testers to simulate user behavior on websites. It helps you perform functional testing on websites across several browsers to test web applications to find out if they work consistently. The cross-browser testing feature allows the site to work on browsers such as Chrome, Firefox, Edge, and Safari.

How is cross-browser testing achieved in Selenium?
FAQ ArrowFAQ Minus Arrow

Cross-browser testing in Selenium is achieved by configuring WebDriver for different browsers like Chrome, Firefox, Safari, and Edge. You instantiate the WebDriver for the required browser and run your test scripts across multiple browsers. This ensures your application functions consistently across different browsers and platforms.

What’s new features in Selenium 4 that helps improve testing?
FAQ ArrowFAQ Minus Arrow

Some key new features in Selenium 4 include:

  • Enhanced Selenium Grid: Supports Docker and Kubernetes for scalable testing.
  • Chrome DevTools Protocol Integration: Provides better control over browser performance and network activities.
  • Relative Locators: Allows finding elements relative to others on the page for easier navigation in complex layouts.
  • Improved Tab Management: Simplifies handling multiple tabs and windows within the same test flow.
What is the difference between Selenium 3 and 4?
FAQ ArrowFAQ Minus Arrow

Selenium 4 introduces an improved, W3C-compliant WebDriver protocol, eliminating the need for a browser-specific driver, unlike Selenium 3. It also includes features like relative locators, native Chrome DevTools support, and enhanced grid architecture. Selenium 4 simplifies cross-browser testing and offers better performance and debugging capabilities.

About the author

Pratik Patel

Pratik Patel

Pratik Patel is the founder and CEO of Alphabin, an AI-powered Software Testing company.

He has over 10 years of experience in building automation testing teams and leading complex projects, and has worked with startups and Fortune 500 companies to improve QA processes.

At Alphabin, Pratik leads a team that uses AI to revolutionize testing in various industries, including Healthcare, PropTech, E-commerce, Fintech, and Blockchain.

More about the author

Discover vulnerabilities in your  app with AlphaScanner 🔒

Try it free!Blog CTA Top ShapeBlog CTA Top Shape
Join 1,241 readers who are obsessed with testing.
Consult the author or an expert on this topic.
Join 1,241 readers who are obsessed with testing.
Consult the author or an expert on this topic.

Discover vulnerabilities in your app with AlphaScanner 🔒

Try it free!Blog CTA Top ShapeBlog CTA Top Shape
Pro Tip Image

Pro-tip

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

What’s New in Selenium 4 - Advanced Key Features