How to Upgrade to Selenium 4

Advanced Topics — Published September 25, 2020

In this series on Selenium 4, we are providing use cases and code samples to explore the upcoming changes to the WebDriver API.

In the previous blog post, we discussed Migrating to Selenium 4 and today we’ll detail how to install Selenium 4 so that you can check out all of the new features!

While Selenium has various language bindings, Java and JavaScript are the most widely used, so this post will cover how to install these two bindings. 

Supercharge Selenium with Applitools Visual AI

Get Started

Java

How to Upgrade from Selenium 3 to Selenium 4

If you are already using Selenium with Maven, you can upgrade it to Selenium 4 by changing the Selenium version to 4. Refer to line 28 in the example below:

If you are already using Selenium with Gradle, you can upgrade it to Selenium 4 by changing the Selenium version to 4 as shown on line 17 in the example below:

How to Install Selenium 4

Before beginning, make sure Java is installed on your machine and the JAVA_HOME environment variable is set in your system path.

The Selenium Java binding can be set up in two different ways: via build tools, or manually. I’ll outline the steps for both. However, you only need to choose one approach.

Install Selenium via build tools

There are various Java build tools available to manage the build and dependencies; the most popular and widely used ones are Maven and Gradle.

Install and setup Maven

Maven is a build and dependency management tool for Java based application development which helps with complete build lifecycle management.

Maven is also available with the Java IDEs (IntelliJ, Eclipse, etc) as a plugin.

However, if you are not using the plugin, you can install Maven manually.

Download Selenium using Maven

Maven looks for a pom.xml file for information on project, configuration, dependencies and plugins etc.

Maven Central Repository is the place where all the dependencies/libraries for all versions can be found. We can search for a library and copy and paste the dependency into our pom.xml file to download them.

For example, Selenium 4 can be downloaded using maven by adding the dependencies in pom.xml as shown in the below sample:

Test Setup

You are now all set to write tests and run them through the IDE or through the command line. You can test the setup, by adding the following code snippet into a new Java class under the ‘src’ directory.

Install and setup Gradle

Gradle is another popular build tool used for java based applications these days. It’s open source and the build scripts can be written in Groovy or Kotlin DSL.

Gradle can be also installed as a plugin in Java IDEs (IntelliJ, Eclipse, etc).

However, if you are not using the plugin, you can install Gradle manually.

Download Selenium using Gradle

Gradle looks for a build.gradle file where all the dependencies, plugins and build scripts are written.

Maven Central Repository is the place where all the dependencies/libraries for all versions can be found for Gradle as well. We can search for a library and copy and paste the dependency from the Gradle tab into our build.gradle file to download them.

For example, Selenium 4 can be downloaded using gradle by adding the dependencies in build.gradle file as shown in the below sample.

Test Setup

You are now all set to write tests and run them through the IDE or through the command line. You can test the setup, by adding the following code snippet into a new Java class under the ‘src’ directory and build.gradle file in the project root directory.

Install Selenium manually

Download Selenium

The jar files for Selenium 4 Java bindings can be downloaded from the official Selenium downloads page.

It will be downloaded as a zip file, unzip it.

Selenium scripts can be executed on various platforms, namely Firefox, Chrome, Internet Explorer, Safari, Opera and Edge. To explore and install the drivers for each refer to documentation links under the “Browsers” section on Downloads page.

Add Selenium to IntelliJ

Within IntelliJ:

Go to the File Menu  Project Structure  Modules  Dependencies Click on ‘+’ Sign -→ Select JARs or directories → Select all the Selenium jar files and click on OK button.

Also, add the downloaded browser drivers under the project directory. In our example we will add chromedriver to run our tests on Chrome browser.

Test Setup

Create a new Java file under the ‘src’ directory and add this code:

JavaScript

You can download and manage the Selenium JavaScript bindings using npm.

Install NodeJs and NPM

As a prerequisite, NodeJs needs to be downloaded and installed on the machine. To verify the installation, use the below two commands

node -v
npm -v

Both should give the current version installed as an output.

Download and install Selenium Webdriver

Using the below command we will install Selenium with npm package manager on your machine.

npm install -g selenium-webdriver

Additionally, to run the tests on the browsers (Chrome, Firefox and IE etc), browser drivers need to be downloaded and set in the system path.

The drivers can be downloaded from the Downloads page and the links can be found under the “Browsers” section on the page. Windows users may add the driver path to the PATH variable under environment variables. Mac and Linux users need to add a driver path to the $PATH variable in the shell.

Once we have installed the Selenium libraries and set the path for the browsers, we need to add the package.json file to the project root directory. NPM requires this file to install the dependencies and plugins used to build the project along with running the tests.

package.json can be created either manually or running the below command which adds the file to the current directory it is run from.

 npm init

Below sample package.json file can be referred for installing Selenium 4 and running our tests. If you are already using Selenium Javascript bindings, to upgrade to Selenium 4, change the version as shown on line 12 in the below sample build file.

Once you have updated the Selenium 4 dependencies in the package.json file, run the below command to install all dependencies defined in your package.json file under dependencies section.

npm install

The test is written in “googleSearchTest.js” file. Below is the sample code, which opens a Firefox browser, navigates to google.com and searches for “Selenium 4” and hits ENTER.

To execute the test use the below command

node googleSearchTest

Summary

We went through the setup instructions for Selenium 4 for two most popular language bindings – Java and JavaScript with ready to use sample tests and build files.

Now that we are ready with the Selenium 4 setup on our machine, in the next post of this series, we will explore the newly added features offered in Selenium 4 and the benefits they provide with working code samples.

Are you ready?

Get started Schedule a demo