How to upgrade from Selenium 3 to Selenium 4

Understand the differences between Selenium 3 and 4 and upgrade. Test on real devices with BrowserStack Automate.

Get Started free
How-to-upgrade-from-Selenium-3-to-Selenium-4
Home Guide How to upgrade from Selenium 3 to Selenium 4

How to upgrade from Selenium 3 to Selenium 4

Selenium 4 has game-changing features like W3C WebDriver standardization, enhanced support for modern browsers, built-in support for relative locators, and improved debugging capabilities.

These updates address critical pain points in automation testing, making workflows more efficient and reliable.Given the widespread adoption of Selenium 4, understanding the differences from Selenium 3 is crucial for teams still using the older version.

This guide explores the key upgrades and provides a step-by-step process to help you transition smoothly and fully harness the benefits of Selenium 4.

Why Upgrade from Selenium 3 to Selenium 4?

The advanced features of Selenium 4 and compatibility with real devices make it a must-have for streamlined and reliable testing workflows.

Below are some key reasons why should upgrade from Selenium 3 to 4:

  • W3C WebDriver Standardization: Selenium 4 fully adopts W3C standards, eliminating the JSON Wire Protocol for communication between browsers and test scripts. This results in more stable cross-browser tests and eliminates compatibility issues.
  • Enhanced Selenium Grid: The new Grid simplifies setup with Docker support, compatibility with Kubernetes, IPv6, and HTTPS, and improved scalability. It includes an updated user-friendly interface and smoother integration with cloud services like AWS and Azure.
  • Revamped Selenium IDE: Selenium 4 introduces cross-browser support, better control flow, enhanced element locators, and the ability to export test cases in multiple programming languages.
  • Relative Locators: Locate web elements based on their position relative to others for more intuitive and readable test scripts.
  • Chrome DevTools Integration: Leverage debugging and network simulation features for faster bug fixes, geolocation testing, and simulating poor network conditions.
  • Simplified Window/Tab Management: Open and manage new windows or tabs using the newWindow API without creating new WebDriver instances.
  • Deprecation of Desired Capabilities: Replaced by Options objects, providing a clearer and more structured way to define browser-specific configurations.
  • Updated Actions Class: New methods simplify mouse and keyboard actions, improving automation of user interactions.
  • Improved Documentation: Comprehensive resources with a streamlined UI make it easier to navigate and learn Selenium’s features, especially for beginners.

Selenium 3 vs Selenium 4

Below are some key differences between Selenium 3 and Selenium 4:

1. Selenium WebDriver W3C Standardization

In Selenium 3, the JSON Wire Protocol facilitated communication between test code and the browser in question – which involved the unnecessary extra task of encoding and decoding API requests via the W3C protocol. The JSON Wire Protocol will be deprecated in Selenium 4.

In Selenium 4, the WebDriver utilizes the W3C standardization. Since most browser drives (chromedriver, geckodriver) do the same, it will let WebDriver communicate directly with the browser, doing away with the need for JSON Wire Protocol entirely. This will likely result in more stable cross browser tests via Selenium than in its previous architectures.

Since the WebDriver implementation for Opera and PhantomJS is no longer being developed, native support for these browsers has been removed in Selenium 4.

2. Enhanced Selenium 4 IDE

In Selenium 3 and older Selenium versions, the Selenium IDE was only available as a Firefox extension with limited features.

In Selenium 4, the IDE has become available for widely used browsers such as Chrome (as a Chrome extension). It is also expected to become available as a plugin or web extension for MS Edge soon. The IDE in Selenium 4 is equipped with a SIDE tool (Selenium IDE Runner) which allows the execution of a .side project on a Node.js platform.

The SIDE Runner is useful for running cross browser tests in the form of parallel testing on a cloud Selenium Grid such as the one provided by BrowserStack.

Additionally, the IDE in Selenium 4 offers an Export feature that lets testers export recorded tests as code in languages supported by Selenium – C#, Python, Java, JavaScript, .NET, etc.

3. Optimized Selenium Grid

In Selenium 3 and older, the Hub and Node (s) need to be initiated individually when performing automation testing. It supported the following processes – Router, Session Map, Distributor.

In Selenium 4, the Hub and Node are consolidated into a single jar file. Once the server starts, it activates both Hub and Node. The processes supported here are – Router, Session Map, Distributor, Node. It also comes with an enhanced GUI and support for Docker.

The Selenium 4 Grid also supports IPV6 addressed, and facilitates user interaction with the Grid via HTTPS protocol. It also supports DevOps-friendly tools such as AWS and Azure.

4. Support for Chrome DevTools

Unlike Selenium 3, Selenium 4 offers native support for Chrome DevTools Protocol through its DevTools interface. This allows testers to use Chrome Development Properties like Fetch, Network, and Profiler to identify issues quickly and edit code on-the-fly. It also enables geolocation and network condition testing (2G, 3G, 4G, Edge) for more accurate website testing.

5. Relative Locators

Selenium 3 required testers to used findElement commands to locate web elements in the vicinity of another element, such as to the right/left/above/below of an element.
Selenium 4 has implemented the ability to do exactly this. It has introduced Relative Locators that allow testers to find web elements relative to another element in the DOM.

6. DesiredCapabilities

In Selenium 3, DesiredCapabilities were used to define test requirements like OS, browser, and version. In Selenium 4, DesiredCapabilities are replaced by Options. QAs now create an Options object, set the requirements, and pass it to the Driver constructor.

7. Actions Class

A number of new methods have been added to the Actions class to replace classes previously contained within package org.openqa.selenium.interactions:

  • click(WebElement) is added to Actions class to replace moveToElement(onElement).click(). It is used to click on a certain web element.
  • clickAndHold(WebElement) added to replace moveToElement(onElement).clickAndHold(). It is used to click on an element without releasing the click.
  • contextClick(WebElement) added to replace moveToElement(onElement).contextClick(). It is used to right click on an element.
  • doubleClick(WebElement) added to replace moveToElement(element).doubleClick(). It is used to double click on an element.
  • release() was initially a part of org.openqa.selenium.interactions.ButtonReleaseAction class. Now it has been moved to Actions class. It is used to release the depressed left mouse click at the current cursor location.

8. FluentWait

In Selenium 4, two methods from FluentWaitwithTimeout() and pollingEvery() now accept a single parameter – java.time.Duration. Previously, in Selenium 3, they accepted two separate parameters int and TimeUnit.

How to upgrade to Selenium 4

The Selenium upgrade to 4 can be undertaken in multiple ways:

Selenium with Maven

Simply change the Selenium version from 3 to 4 in pom.xml.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>selenium4</groupId>
<artifactId>selenium4</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

</dependencies>

</project>

Selenium with Gradle

Change Selenium version to 4 as shown below:

plugins {
id 'java'
}

group 'SeleniumGradleSample'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
compile group: 'org.testng', name: 'testng', version: '6.14.3'
// https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-6'
}

test {
useTestNG()
}

Install Fresh Instance of Selenium 4

Ensure that Java is installed on the local machine. Additionally, set the JAVA_HOME environment variable in the system path. This can be done via build tools (Maven or Gradle, as detailed above) or manually.

BrowserStack Automate Banner

Test on Real Devices with BrowserStack Automate

To accommodate the new version, BrowserStack has made necessary changes that would allow QAs to use Selenium 4 on its cloud Selenium grid.

No matter the Selenium version, automation testing must be run on real browsers and devices to yield accurate results. Start running tests on 3500+ real browsers and devices on BrowserStack’s real device cloud. Run parallel tests to get faster results without compromising on accuracy. Detect the bugs and offer a high-end UX/UI to the users by automated testing in real user conditions with BrowserStack Automate.

Talk to an Expert

Conclusion

Use this guide to transition smoothly and fully harness the benefits of Selenium 4. The features of Selenium 4 have proven to quite promising and can make the test process easier. However, it is important to perform the tests on real devices for accurate results and leverage platforms like BrowserStack.

Tags
Selenium Selenium Webdriver