How to upgrade from Selenium 3 to Selenium 4
Shreya Bose, Technical Content Writer at BrowserStack - February 19, 2023
Since its announcement in 2018, Selenium 4 has been a point of excitement in the development and testing community. Naturally, devs and testers want to move to the newer version, because it offers a litany of new features and updates that provide very real advantages.
For individuals looking to download and use the Alpha version of Selenium 4, a quick rundown of what this transition entails is necessary. The best place to start this discussion is a comparison between Selenium 3 vs Selenium 4. Let’s have a look at what has changed, and what Selenium users moving to version 4 have to do differently.
Selenium 3 vs Selenium 4
- 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.
- 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.
- 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.
- Support for Chrome DevTools
Unlike Selenium 3, Selenium 4 provides native support for Chrome DevTools Protocol through its DevTools interface. It allows testers to utilize Chrome Development Properties like Fetch, Network, Profiler, Performance, Application Cache, etc. APIs offered by Chrome DevTools help QAs and devs identify anomalies faster and edit code without interrupting its execution (on-the-fly).
These APIs also help replicate myriad geographical locations and network strengths in Selenium 4 which enables QAs to run geolocation testing and test websites in throttled network conditions (2G, 3G, 4G, Edge) geolocation testing.
Do you know how to perform network throttling to test websites on Chrome?
- 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.
Did you know the difference between findElement and findElements command?
- DesiredCapabilities
Selenium 3 used DesiredCapabilities to help QAs define basic test requirements such as operating systems, browser combinations, browser versions, etc. within Selenium test scripts.
In Selenium 4, DesiredCapabilities have been replaced by Options. To use the Driver class, QAs have to create an Options class, set test requirements, and pass the object to the Driver constructor.
Driver constructors that accepted Capabilities objects have been replaced with those that accept Options.
# ChromeDriver(Capabilities) replaced by ChromeDriver(ChromeOptions).
# SafariDriver(Capabilities) replaced by SafariDriver(SafariOptions).
# EgdeDriver(Capabilities) replaced by EdgeDriver(EdgeOptions).
# FirefoxDriver(Capabilities) replaced by FirefoxDriver(FirefoxOptions).
# InternetExplorerDriver(Capabilities) replaced by InternetExplorerDriver(InternetExplorerOptions).
- 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.
- FluentWait
In Selenium 4, two methods from FluentWait – withTimeout() and pollingEvery() now accept a single parameter – java.time.Duration. Previously, in Selenium 3, they accepted two separate parameters int and TimeUnit.
For the full list of deprecations in Selenium 4, visit the official Selenium site.
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.
To accommodate the new version, BrowserStack has made necessary changes that would allow QAs to use Selenium 4 on its cloud Selenium grid.
Explore BrowserStack support for Selenium 4.
No matter the Selenium version, automation testing must be run on real browsers and devices to yield accurate results. Start running tests on 3000+ 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.