Every company nowadays is leveraging test automation to enhance their product’s quality. And if you are into web application development, there is no better way to ensure your product’s optimum quality and purpose other than using Selenium.
Selenium is one of the most popular open-source frameworks for test automation. It is used to validate your web app across different platforms and browsers.
The release of Selenium 4 was widely awaited by testers since it was first announced in 2018’s Selenium conference. Now that Selenium 4 is officially in the market, testers will find that a lot of changes are brought not into WebDriver but also in Selenium Grid and Selenium IDE. But how should you plan the migration or upgrade of your existing Selenium framework? Let’s discuss that in this post.
What’s New in Selenium 4?
Before getting on with the installation, let’s find out about the new features we have in Selenium 4.
- First of all, unlike the previous complex Selenium grid that came with Selenium 3, the new grid comes with Docket support. It’s flexible, easy to use, and easily scalable.
- The IDE is upgraded. It is more user-friendly with a new GUI. It is also available for all major browsers like Chrome or Firefox.
- You can easily locate elements with new relative locators like Below, Above, To right of and To left of.
- Documentation is also revamped. The site has a neat UI and makes it easy and fun for new test engineers to learn all the tips and tricks Selenium has to offer.
- Test automation engineers can now use Chrome development properties like Network, Application cache, Profiler etc. This is because Selenium 4 now supports Chrome DevTools Protocol.
- New methods like clickAndHold, click, contextClick, doubleClick are added in the actions class.
- Previously in Selenium 3, when test engineers needed to open a link in a new tab for performing a certain action, they had to create a new object and use WindowHandle method to switch operations. However, in Selenium 4, there is a new API called newWindow. This new API allows users to switch to a new tab without the need of a new WebDrver object.
Using Maven and Gradle
Are you already using Selenium with Maven? If so, you can upgrade Selenium 3 to 4 by just changing the version in <version> tag in the pom.xml file.
<dependencies> <!–https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java –>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
The same process goes if you are using Gradle. You need to change just the version in the dependencies tag that you will find in the build.gradle file.
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’
}
test {
useTestNG()
}
But what if you don’t have Maven or Gradle. Let’s find out how we can install both of them.
Installing Maven and Setting Up Tests
Maven is a popular dependency and build management tool used for Java-based applications. The advantage of using Maven is that it helps with managing the entire build lifecycle. You can install Maven by
- Having JDK installed in your system.
- Download maven and extract the zip.
- Add the bin directory of Maven to your system’s PATH environment variable.
- After that, you can just run mvn -v in a new command prompt window. You will get the version details.
Setting up tests is easy. Here, we are writing a sample test script for testing the search feature of Bing. You have to add the following code in a new Java class and place it under the src directory.
package Tests;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
class BingSearchTest{
final static String PROJECT_PATH = System.getProperty(“user.dir”);
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, PROJECT_PATH+ “/chromedriver”);
ChromeDriver driver = new ChromeDriver();
driver.get(“https://www.bing.com/”);
WebElement searchTxtBox = driver.findElement(By.name(“q”));
searchTxtBox.sendKeys(“Microsoft” + Keys.ENTER);
driver.quit();
}
}
Installing Gradle and Setting Up Tests
Apart from Maven, there is another popular build tool where you can write scripts in Kotlin or Groovy. You guessed it right! It’s Gradle. You can install it as a plugin in Eclipse. If you want to install it manually, you have to
- Download the zip.
- Create a new folder on your C drive and unzip the downloaded directory.
- Right-click on your PC’s properties, go to advanced => system settings, and then environmental variables.
- Edit the Path and add an entry to Gradle’s bin directory.
- Save the properties and you are done.
You can write tests for Gradle either from the command line or from the IDE itself. For testing your setup, you can add the same code that we used for Maven in a new java class just like maven and build the gradle file in the project root.
That’s it. Once you are done installing Maven or Gradle, you can upgrade your existing Selenium framework to version 4 with the step we noted above.
Installing Selenium via JavaScript
Apart from Java, you can also download and install Selenium using NPM. Let’s find out how.
Installing Node and NPM
NodeJS is a mandatory prerequisite. Download ans install it on your system. Check the version using node -v and npm -v in the command line interface.
Install WebDriver
You can install Selenium with the package manager of npm with the help of the following command.
npm install -g selenium-webdriver
Also, for running the tests, you need to have the drivers for IE, Firefox and Chrome installed in your system.
You can get the drivers from Selenium’s official downloads section. If you are using Windows, you will also need to add the path to the drives in the PATH variable.
Once you have everything installed, add the package.json file in the root directory of the project. This file will help you to install the plugins and dependencies needed to run the tests and build the project.
You can create package.json manually or by running the following command.
npm init
For installing Selenium 4, you can refer the following package.json file.
{
“name”: “google-sample-test”,
“version”: “1.0.0”,
“description”: “sample selenium 4 with javascript “,
“main”: “googleSearchTest.js”,
“scripts”: {
“test”: “echo \”Error: no test cases\” && exit 1″
},
“author”: “Shama Ugale”,
“license”: “ISC”,
“dependencies”: {
“selenium-webdriver”: “^4.0.0”
}
}
After updating the dependencies for Selenium 4 in package.json, run npm install to get all the dependencies installed for your project.
Did you remember the sample test that we mentioned earlier for testing Selenium with Maven? You need to write a similar code for testing your setup. Write the code under the file bingSearchTest.js.
const {Builder,Buy,Key} = require(“selenum-webdriver”);
async function searchTestOnBing(){
// create and open a firefox driver object
let driver = await new Builder.forBrowser(“firefox”).build();
// navigate to bing.com
await driver.get(“https://www.bing.com”);
// locate the bing search textbox and enter “Selenium webdriver”, hit Enter key
await driver.findElement(By.name(“q”).sendKeys(“Selenium webdriver”,Key.RETURN));
}
searchTestOnBing();
For running the test, run node bingSearchTest.
Any Way to Skip All the Installation Headaches?
Nowadays, we all are looking for ways to save manual time and focus on finding innovative solutions for our projects. Installation and configuration can sometimes prove to be hectic and time taking jobs. Well, you don’t need to worry anymore. With Lambdtest’s cloud-based Selenium grid, you can test your project on a Selenium grid hosted on the cloud. You can run your tests hassle-free on more than 3000 desktop and mobile browsers.
Coming to Selenium 4, you can use LambdaTest’s Desired Selenium Capabilities Generator.

Based on your mouse interactions on the user interface, the generator will give you the entire code of capabilities class. Also, you can get the code not only in Java or JavaScript but also in C#, Ruby, Python and PHP.
Conclusion
We hope you will find the blog post useful in understanding how upgrading to Selenium 4 will be helpful for your project. We have mentioned all the basic steps you can use to upgrade your project from Selenium 3 to 4. We have also discussed how you can use a cloud-based Selenium grid to skip the installation and directly start your work. So, start upgrading your framework and keep testing to enhance your application’s quality.