Enterprise Java

How To Upgrade From Selenium 3 To Selenium 4?

Selenium 4, the latest version of the Selenium framework, has become the talk of the town since its announcement in 2018. The excitement around Selenium 4 is evident since it is packed with a host of new features and enhancements than its predecessor. Though there is no official announcement on the ‘final’ release of Selenium 4, Selenium users and test automation enthusiasts have already started exploring and experimenting by downloading the Alpha version of Selenium 4.

If you are thinking of upgrading from Selenium 3 to Selenium 4, this Selenium 4 tutorial will help you take the version leap! For folks waiting on the sidelines to try Selenium 4, the comparison between Selenium 3 vs. Selenium 4 could excite you to try Selenium 4 much sooner than you ever expected☺.

Though Selenium has various language bindings like Java, C#, Python, JavaScript, Ruby, and PHP, this Selenium 4 tutorial would focus on installing Java bindings.

TABLE OF CONTENT

Selenium 3 vs. Selenium 4

Wondering ‘What uniqueness does Selenium 4 have compared to Selenium 3?’ Well, to answer that question, we’ll do a quick walk-through of the newness offered by Selenium 4 in this section of Selenium 3 vs. Selenium 4 tutorial:

Selenium WebDriver W3C Standardization

In Selenium 3, the JSON Wire Protocol was the primary communication mode between the test code and web browser. Major browser drivers like ChromeDriver, GeckoDriver, etc., follow the W3C standard. This resulted in the encoding and decoding of requests as per the W3C protocol.

Under the hood, Selenium 4 uses the WebDriver W3C protocol. This eliminates the overhead of encoding and decoding that was necessary with JSON Wire Protocol in Selenium 3. This major architectural change will result in less flaky and more stable cross browser tests (i.e., tests across different versions and types of browsers) with Selenium 4. You can refer to our detailed coverage on Selenium W3C WebDriver in Selenium 4 to gain more insights.

Selenium 4 IDE

The IDE in Selenium 4 is much more than a rudimentary playback and record testing tool. Along with Firefox, it is available for the Chrome browser (as a Chrome extension).

The SIDE Runner tool in Selenium 4 IDE lets you run Selenium tests parallel on local Selenium Grid and cloud-based Selenium Grid. The ‘export’ feature enables you to export the recorded tests in Selenium supported languages like Python, C#, Java, and more. Selenium 4 IDE is a renewed and super-useful offering available only in Selenium 4.

Optimized Selenium Grid

If you’re a Selenium 3 user, you must be aware of how painful it’s to start the Hub and Node jars each time you want to perform automation testing on the Grid. Distributed test execution is all set to change with the optimized Selenium Grid in Selenium 4.

In Selenium Grid 4, Hub and Node are packed in a single jar file. Selenium Grid 4 has a more scalable and traceable infrastructure that supports four processes – Router, Session Map, Distributor, and Node. Improved GUI and built-in support for Docker are some of the additional perks you get in Selenium Grid 4.

Chrome DevTools

In Selenium 4, there is native support for Chrome DevTools Protocol (CDP) through the DevTools interface. The Chrome DevTools interface’s APIs would make issue diagnosis and on-the-fly editing of pages much easier.

The native support of CDP will help emulate geolocation and network conditions in Selenium 4 with more ease. With Selenium 4, you could test the web product built for a global audience by emulating geolocation in the code. At the same time, you could also check how the product performs against varying network conditions (e.g., 3G, 4G, etc.).

Relative Locators

In Selenium 3, you have to use a series of findelement commands on the appropriate WebElement to locate its vicinity elements. There was no shortcut to finding an element that is above/below/right to/etc. of a particular WebElement.

Relative Locators (above, below, toRightOf, toLeftOf, and near) that are newly introduced in Selenium 4 Alpha help locate web elements’ relative’ to a particular element in the DOM. Our detailed coverage of relative locators in Selenium 4 could help you get started with this feature exclusively available in Selenium 4.

Apart from these big enhancements in Selenium 4, it is packed with other new features:

  • TakeElementScreenshot API lets you capture a screenshot of a particular WebElement on the page.
  • Introduction of newWindow API helps in the simplified creation of a new Window (WindowType.WINDOW) or Tab (WindowType.TAB).
  • Optimized and accelerated debugging process.

As far as Selenium 3 vs. Selenium 4 comparison is concerned, the overall experience with Selenium 4 (Alpha) is much more superior to Selenium 3. This is one of the primary motivating factors to download Selenium 4 (Alpha) and get your hands on the awesome features offered by Selenium 4.

Upgrade from Selenium 3 to Selenium 4

As of writing this article, Selenium 4.0.0-alpha-7 was the latest Selenium 4 (Alpha) version available for download on the Selenium website. Selenium 4 for different language bindings is shown below:

Language

Selenium 4 (Alpha) Download Link

Java

https://selenium-release.storage.googleapis.com/4.0-alpha-7/selenium-java-4.0.0-alpha-7.zip

Python

https://pypi.org/project/selenium/4.0.0.a7/

C#

https://www.nuget.org/api/v2/package/Selenium.WebDriver/4.0.0-alpha07

Ruby

https://rubygems.org/gems/selenium-webdriver/versions/4.0.0.alpha7

Selenium with Maven – Upgrade from Selenium 3 to Selenium 4

If you are using Selenium with Java, you will likely use Selenium with Maven to manage the dependencies associated with your Selenium project. For Selenium with Maven users, changing the Selenium version to 4 in pom.xml is all you need to upgrade from Selenium 3 to Selenium 4.

Maven Central repository is the ideal location to look for dependencies or libraries for all the versions. As shown above, we downloaded the Maven Dependency for Selenium 4 Java and added the same in pom.xml

Along with Selenium 4 Java, we also added the Selenium 4 packages for selenium-chrome-driver and selenium-remote-driver in the POM file (pom.xml).

Selenium with Gradle – Upgrade from Selenium 3 to Selenium 4

Gradle is a popular build tool that is used for Java-based applications. The build scripts in Gradle are written in Groovy or Kotlin DSL. If you are using Gradle for your Selenium Java project, the necessary dependencies must be configured in the build.gradle file, which is placed at the root level (of the project).

The necessary dependencies to be downloaded have to be added in build.gradle. Like Maven, the Maven Central Repository should be used for finding the required dependencies for the project.

Here is the build file (build.gradle) for a Gradle project where we have used the 4.0.0-alpha-7 version of the Selenium Java binding. Since we are using the TestNG framework in the implementation, we have added the other dependencies in the build.gradle.

plugins {
    id 'java'
}
 
group 'org.demo'
version '1.0-SNAPSHOT'
 
sourceCompatibility = 1.8
 
repositories {
    mavenCentral()
}
 
dependencies {
   compile group: 'org.seleniumhq.selenium', name: 'selenium-java',
            version: '4.0.0-alpha-7'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver',
            version: '4.0.0-alpha-7'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-remote-driver',
            version: '4.0.0-alpha-7'            
    compile group: 'org.testng', name: 'testng',
            version: '6.14.3'
}
 
test {
    useTestNG()
}

Test Setup – Demonstration of upgrading from Selenium 3 to Selenium 4

To demonstrate how to upgrade from Selenium 3 to Selenium 4, we take a cross browser testing example where the search for “LambdaTest” is performed on Google. The test is performed on the Chrome browser.

Test Scenario

  1. Go to Google
  2. Search for LambdaTest
  3. Click on the first search result
  4. Assert if the page title does not match the expected page title

Implementation

Here is the overall project structure in IntelliJ IDEA:

Downloading Selenium 4 using Maven

In a Maven project, the pom.xml file consists of the required details about the configuration, dependencies, and more. Along with Selenium 4 Java, we have also added the Selenium 4 Chrome Driver and Selenium 4 remote Driver dependencies in pom.xml.

As we are using the TestNG framework, the details of the test scenarios are added in testng.xml.

Setting up Gradle

The same project is used for demonstrating upgrading from Selenium 3 to Selenium 4 using Gradle. Dependencies, plugins, and the build scripts should be available in the build.gradle file, which is placed at the root level of the project.

For a quick recap on Gradle, you can refer to the detailed coverage of Gradle (with JUnit) in our earlier blog here. The dependencies of Selenium 4 Java, Selenium Chrome Driver (4.0.0-alpha-7), Selenium Remote Driver (4.0.0-alpha-7), and TestNG (6.14.3) are added in the build.gradle.

plugins {
    id 'java'
}
 
group 'org.demo'
version '1.0-SNAPSHOT'
 
sourceCompatibility = 1.8
 
repositories {
    mavenCentral()
}
 
dependencies {
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java',
            version: '4.0.0-alpha-7'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver',
            version: '4.0.0-alpha-7'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-remote-driver',
            version: '4.0.0-alpha-7'            
    compile group: 'org.testng', name: 'testng',
            version: '6.14.3'
}
 
test {
    useTestNG()
}

Execution

To execute the test scenario, right-click on testng.xml and select “run …\testng.xml”.

The execution snapshot below shows that the Chrome WebDriver was instantiated, and the test scenario was executed successfully.

For Gradle, we use the command line option (gradle test) to trigger the test in the project. As the test scenario (test_search_demo) is located in the class file DemoTest, we run the test using the following command (after navigating to the root folder of the project):

gradle test --tests DemoTest.test_search_demo

As seen in the execution snapshot, the test was executed successfully.

Upgrade From Selenium 3 To Selenium 4 Using The cloud-based Selenium Grid

The advantages offered by Selenium 4 can be best exploited on a Selenium Grid where tests can execute in parallel on the appropriate nodes. The Selenium 4 Grid provides a host of new features that ease and accelerate Selenium web automation testing.

A cloud-based Selenium Grid like LambdaTest offers much-needed scalability, reliability, and security, difficult to attain with a local Selenium Grid. Selenium testing on the cloud helps achieve better browser coverage, test coverage, and test coverage, as a number of tests can be executed in parallel on the cloud-based Selenium Grid.

We port the test demonstrated earlier so that it executes on LambdaTest’s Selenium 4 Grid. To get started, we generate the desired capabilities for Java language binding using LambdaTest Capabilities Generator.

As a part of the setup, the method implemented under the @BeforeTest annotation will use the RemoteWebDriver instead of the local Chrome Driver. Shown below is the updated DemoTest.java where we use the LambdaTest Selenium Grid for running the test:

The execution information is available in the Automation Dashboard of LambdaTest. As seen below, the test was executed successfully on the LambdaTest Selenium Grid:

That’s All Folks

Selenium 4 offers several new features like relative locators, Chrome DevTools, improved Selenium Grid, and more, making it a worthy upgrade over Selenium 3. In this Selenium 4 tutorial, we had a detailed look at how to upgrade from Selenium 3 to Selenium 4 for Java language bindings. If you are using Maven for the project, upgrading to Selenium 4 is about fetching the Selenium 4 from the Maven Repository and adding the same in pom.xml.

Apart from Java, it is easy to upgrade to Selenium 4 for Selenium supported languages like Python, C#, PHP, Ruby, and JavaScript. As far as Selenium 3 vs. Selenium 4 is concerned, Selenium 4 (which is still in the Alpha stage) has a huge upper-edge over its predecessor.

If you are using Selenium 4, have you tried Selenium with Maven? Do share your experience in the comments section. Also, let us know which is your ‘go-to feature’ in Selenium 4.

Happy Automation Testing☺

Frequently Asked Questions

What Is New In Selenium?

One of the most notable changes in the Selenium is the adoption of the W3C standard Web Driver Protocol, facilitating the interaction between browser drivers and Selenium Web Driver. Code export is now available for all the official language bindings with a better control flow mechanism to write better tests using “while” & “if” conditions, and more.

What Is The Difference Between Selenium 2 And Selenium 3?

Selenium 2 is the combination of WebdDriver+SeleniumRC based on Selenium core, whereas Selenium 3 supports Selenium RC indirectly through a back-end Webdriver without Selenium core.

What Is The Current Version Of Selenium?

The latest version of Selenium is Selenium 4.0. 0 Alpha 5, which was released in March 2020.

Published on Java Code Geeks with permission by LambdaTest, partner at our JCG program. See the original article here: How To Upgrade From Selenium 3 To Selenium 4?

Opinions expressed by Java Code Geeks contributors are their own.

Himanshu Sheth

Himanshu Seth is an engineer with 15+ years of experience. He has worked with multi-national companies as well as startups. He is also an avid blogger on technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button