Technology

What is Selenium 4? The latest in Selenium Automation Testing for browsers

What is Selenium 4? The latest in Selenium Automation Testing for browsers

For someone who is related to software testing or even development, the name “Selenium” would ring a bell at once. Released in 2009, Selenium is today arguably one of the most famous testing tools and the most famous when it comes to web app testing. Changing the way web app automation testing used to work, Selenium Webdriver is today in its fourth version of the stable release and has brought some significant (and some most waited) changes to the tool.

In this post, we will go through these changes while reflecting back on the previous versions to know how they will affect us. If you are new to Selenium or have just heard and not used it, a small brief introduction below might help you out.

What is Selenium 4?

Selenium is a test automation tool used in the field of browser automation. While the term “Selenium” previously meant automating browser related tasks, today that term has shifted towards “Selenium Webdriver”. Selenium has become a bigger project that contains smaller sub-projects (such as WebDriver) that work in browser automation but accomplish different tasks.

For example, if you are looking to involve a server in between browser and test cases to monitor HTTP requests more carefully, you would want to go with Selenium Remote Control instead of Webdriver. 

Selenium is simple to use, is open-source and comes with support for most programming languages. Since it has been around for a while now, Selenium has developed a very vast community that helps you in case of any issue. So it makes our life as a tester much easier. If you are looking to learn Selenium more fundamentally, a course on the same would be a better choice.

What’s new in Selenium 4?

Selenium 4 got released in October 2021 and there have been significant improvements in features and performance than Selenium 3 which got released 5 years back. In this post, we have divided the Selenium 4 changes into two parts – improvements & enhancements and another section on how to upgrade to Selenium 4. Both of these sections will help you shift from Selenium 3 to Selenium 4 smoothly.

Let’s start with new exciting features:

Selenium Grid

Selenium has been working on improving its grid and they have made it available with their fourth release. The Selenium Grid 4 (Grid in Selenium 4) can be run on a distributed architecture to be used with the Kubernetes and has been made more secure than its previous versions.

For Docker users, Selenium Grid 4 brings out handling Docker containers on the local machine. The user can also pull the required Docker images from the Grid making the maintenance part extremely easy. Grid 4 also makes it extremely easy if the user wants to visualize the Grid using the user interface that is built now on top of GraphQL.

Introduction of relative locators

Selenium developers understand the complexity behind complex locator based codes that not only make the code hard to read but also hard to maintain. Aiming toward a highly readable code, Selenium 4 has introduced relative locators in its library. A relative locator, as its name suggests, is a locator that can be described relative to another element. The best part of the relative locator is that it uses the English language to describe the relation. So, a user can use terms such as “to the right of this” and “above that element” etc.

The following example uses relative locators introduced in Selenium 4:

field1 = driver.find_element(By.ID, “example”)

field2 = driver.find_element(locate_with(By.TAG_NAME,  “input”).above(field1))

Chromium-based browser enhancements

Selenium 4 has brought enhancements to APIs used in Chromium-based browsers. Since these are enhancements, previously written tests need not be modified according to Selenium 4.

These enhancements include basic and digest authentication improvements, network interception, waiting for DOM and more.

One major modification has been the introduction of Bi-Directional APIs which are commonly referred to as BiDi APIs. These APIs do not limit the user to a strict request-response model giving freedom for writing automation tests based on events. The user will also be able to stream events from the user agents.

Please note that a lot of resources online encourage the use of Chrome Debugging Protocols (CDP) in Chrome Devtools, especially with Selenium 4. The Selenium community does not recommend its usage and asks the testers to use BiDi APIs instead. Therefore, we will skip that feature here.

Introduction to new commands

Selenium 4 has come up with improvements in the field of selenium commands as well additions of newer ones to make life easier. For example, to create a Selenium server, the command goes like this:

java -jar selenium-server-4.0.0.jar standalone

Which is long and it’s hard to remember the command. So if you are using Linux or macOS you can take advantage of this jar being executable from Selenium 4. First, make the jar executable:

chmod 755 selenium-server-4.0.0.jar

Then, rename this to anything else that you can remember like “selenium”. From that point on, instead of typing the whole java -jar command, you can just type selenium and it will mean the same.  

Enhancement of Wait syntax

Selenium 4 introduces a new syntax for Timeout() with modifications in the arguments passed to the function. Earlier the following was the wait syntax:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

This has changed to:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

Deprecating setLegacy

The setLegacy function was used earlier when dealing with Firefox related test automation code in Selenium. The code would be as follows:

FirefoxOptions options = new FirefoxOptions();

options.setLegacy(true);

Since Firefox’s recent versions (after 47) do not support this, Selenium 4 has deprecated setLegacy function and has asked to use GeckoDriver only.

There have also been a few minor deprecations that are specific to the language (Python and C#) but that would not be a major concern for the tester.

How to upgrade to Selenium 4 and major pain points

As a selenium user, I hope you must have found the new enhancements and additions quite helpful in test automation. Selenium 4 has been kept as soluble with Selenium 3 as much possible so that users don’t need to change too much of their old code.

This section is designed to help you upgrade to Selenium 4 easily and explore the areas where you can expect a bit of pain in the process.

Test code, capabilities and actions

Selenium 4 official announcement states the removal of the legacy protocol with the replacement of the W3C web driver making it standardized for usage. This can cause issues if your capabilities are not compliant with the W3C standards. The website states the following capabilities to be checked by the teaser:

  • browserName
  • browserVersion (replaces version)
  • platformName (replaces platform)
  • acceptInsecureCerts
  • pageLoadStrategy
  • proxy
  • timeouts
  • unhandledPromptBehavior

If the list does not contain the capability you are concerned with, then you need to add vendor prefix to it. For example, the following code adds build and name capabilities according to selenium 4:

Map<String, Object> cloudOptions = new HashMap<>();

cloudOptions.put(“build”, myTestBuild);

cloudOptions.put(“name”, myTestName);

browserOptions.setCapability(“cloud:options”, cloudOptions);

Another thing to keep in mind while checking up your code for Selenium 4 is the FindsBy interface which has been removed from this version. 

So this code:

driver.findElementByClassName(“className”);

Should now be written as this code:

driver.findElement(By.className(“className”));

Upgrading project dependencies

The next step is to upgrade the project dependencies accordingly. The project dependencies are changed in the file that declares them (such as pom.xml). However, it also depends on the type of project you are building and the language in use. Selenium 4 provides new dependencies for Java (Maven and Gradle), Python, C#, Ruby and JavaScript. Please refer to the official page for the code.

A better way to upgrade to Selenium 4

As seen in this post, till this point, Selenium 4 does come with attractive features but may need some work to do before completely modifying our old code and starting writing with the new one. Such a maintenance process happens with every version release which can waste our time and resources.

A better method to upgrade to Selenium 4 is to use a cloud-based infrastructure that provides all the latest technologies over the cloud. You don’t need to re-run the grids or install anything. Everything comes ready to use for you. HeadSpin is one such platform that makes test automation of the browsers extremely easy over the cloud. It provides Selenium along with all the latest browsers to test your website on. With a robust infrastructure, you get everything in one place without worrying about installation or maintaining your Selenium 3 codebase.

You can try this platform by signing up for free on the official website.

Conclusion

Selenium 4 is a better, optimized and stable version of the Selenium umbrella software. It is stable and is up-to-date with technologies that are actually used today rather been stuck to legacy syntaxes. To aware our users of the new enhancements and improvements, we crafted this post for them.

We hope this post will help you upgrade to Selenium 4 and give insights into the newer version of the software that is reliable, trustworthy and has stood the test of time.

To Top

Pin It on Pinterest

Share This