Shifting from Selenium 3 to Selenium 4

Automation Testing Mar 7, 2022

Selenium is an extensively used framework for UI automated testing. Selenium has a marked presence in the automation testing world due to its open-source nature and numerous features that make the testing process faster when compared to other tools.

About this blog:

In this blog, we will highlight the key differences between Selenium 3 and Selenium 4 and call out specific advantages of shifting to the newest version of Selenium (which is version 4), through some concrete examples.


There is a new version of Selenium released which is Selenium 4 that has the most significant advantage for W3C compliance of WebDriver APIs, which drills down to less flaky and more stable cross-browser tests.
The prominent change in this version is the deprecation of JSON Wire Protocol which was used in earlier versions before Selenium4.


Selenium-3 used JSON Wire Protocol for communicating with the web browser from the local end. The request is sent from Selenium Client to the Web Browser where the automation takes place. Once the automation is complete, a response travels back to the Browser Driver, JSON Wire Protocol, and Selenium Client.
As the Selenium Client libraries (i.e., Client) use the JSON protocol, and the web browser uses the W3C protocol, API encoding, and decoding was needed in this entire process.


But Selenium-4 has direct communication between the client and server using the W3C (World Wide Web Consortium) protocol, offering lots of advantages compared to the Web Driver used in the versions before Selenium-4

In Selenium 4, WebDriver W3C Protocol replaces the older JSON Wire protocol.

It essentially means that we  no longer need to encode and decode the API request using the W3C protocol, and the tests can directly communicate with the web browser.


Advantages in Test Automation after the shift to Selenium4:

  • Flakiness in web automation can be considerably reduced if a standard protocol is used.
  • The automated Selenium testing will run more consistently across browsers as the browsers, and Selenium WebDriver is using the same protocol.
  • The user does not need to customize the test cases based on the browser-specific.
  • Few exceptions that are browser-specific like No such Element exception, ElementNotInteractableException will not occur as there is common communication using W3C protocol

There are other benefits as well in Selenium 4 version

1. Usage of WebDriver Manager:

We can get rid of compatibility issues between Browser And Drivers

In Selenium WebDriver version 3, we need to explicitly download the compatible browser’s driver executable binary file and set the JVM path or properties so that selenium commands can be executed.

After Selenium 4 release we need not download the driver executables for individual browsers, instead by simply adding one line code it downloads the latest version for a specified driver executable/s automatically

How to use WebDriverManager?

WebDriverManager is an open-source Java library that carries out the management (i.e., download, setup, and maintenance) of the drivers required by Selenium WebDriver

Pre-Condition:

The project should be Maven. In the pom.xml file, we need to add the below dependency to have WebDriverManager in our Project.

Once WebDriverManager is added into pom.xml. Update Maven Project and ensure that WebDriverManager Jar is added into Project Build Path

Output

From the above screenshot we can see that Chromedriver started successfully. We can see the details of starting the chrome driver instance in the first line of output. Here we have set the Chrome version to “98.0.4758.102”.

Advantages :

  • WebDriverManager automates the browser setup in the Selenium code.
  • By default,  it downloads the latest version of the browser binary and also the binary for the appropriate platform.
  • We have to add the dependency for WebDriverManager in case the code belongs to Maven or Gradle environment.
  • It supports various browsers like  Google Chrome, Firefox, IE, Edge, Opera, etc.
  • Apart from automatically setting browser binaries, WebDriverManager also allows us to set values for parameters like browser driver version (in case we want a specific version), platform version, proxy details,

2.Relative or Friendly locator

There are several methods to locate the element of the web page which help Selenium scripts to uniquely identify the WebElements

  • Name
  • ID
  • Link text
  • Partial Link Text
  • Tag Name
  • Class Name
  • CSS
  • Path

Additional to the above elements, Selenium 4 has introduced, relative locators. It is the advanced way of locating the strategy of web element.

Through Relative locators, we can locate specific Web Element by using easy terms

  • toLeftOf(): Element located to the left of specified element.
  • toRightOf(): Element located to the right of the specified element.
  • above(): Element located above with respect to the specified element.
  • below(): Element located below with respect to the specified element.
  • near(): Element is at most 50 pixels far away from the specified element. The pixel value can be modified.

3.Waits

Why waits are required in Automation Testing

Sometimes the automation script is faster than the browser loading time or objects render time on the browser - no such element different elements may load at a different time interval, so webdriver script and browser events may not be synchronous at all times

There are different types of waits

  • implicit
  • explicit
  • fluent

From Selenium 4 there is a syntactical change in the way in which the wait statements are used

Before Selenium 4 :

After:


4.Capture screenshot of specific web element

In Selenium 3 it was only possible to take the entire page screenshot, but now with Selenium 4 users are able to take a screenshot of a specific web element.
In Selenium 3 we had to take a screenshot of the screen and then you had to crop it using desired element’s coordinates.


With Selenium 4 one can take a screenshot with just a few lines of code

5. Multiple Tabs /windows

Scenario: Invoking multiple windows /tabs from Selenium using the same driver instance with Selenium 4

Step1: Verify the user is able to navigate to url1

Step 2: User has to pick data from url2 and fill the data in url1

In the above use case till Selenium 3 version user has to navigate to the url2, capture the data and in the same browser navigate to url1 and fill the data which will be time-consuming.

From Selenium 4 multitasking can be performed. In the above use case, Step1 can be opened in one browser which is the parent window and Step 2 parallely can be opened in other browser that is the child window, capture the name and switch back to the parent window with the same driver instance.

Advantage :

Selenium 4 provides better window/tab handling using a new API. We can now create a new tab/window and automatically switch to it with fewer lines of code and without creating a new WebDriver object.


6. Selenium 4 Chrome DevTools APIs

Selenium 4 has added native support for Chrome DevTools APIs. With these new APIs, our tests can now:

  • Capture and monitor the network traffic and performance
  • Mock geolocations for location-aware testing, localization, and internationalization
  • Change the device mode and exercise the responsiveness of the application

Conclusion:

Selenium 4 comes with a major architectural shift to provide a stable test automation platform. Relative Locator is an interesting locator strategy added in Selenium 4, using which testers can access web elements with fewer lines of code.The addition of Chrome DevTools API in Selenium 4 , to test the application by emulating geolocation and varying network connections. This is also coupled with all the new features, usability, and performance improvements which makes Selenium 4 much more capable and performant compared to its predecessor.

Join us

Scalability, reliability, and maintainability are the three pillars that govern what we build at Halodoc Tech. We are actively looking for engineers at all levels and if solving hard problems with challenging requirements is your forte, please reach out to us with your resumé at careers.india@halodoc.com.

About Halodoc

Halodoc is the number 1 all-around Healthcare application in Indonesia. Our mission is to simplify and bring quality healthcare across Indonesia, from Sabang to Merauke. We connect 20,000+ doctors with patients in need through our Tele-consultation service. We partner with 3500+ pharmacies in 100+ cities to bring medicine to your doorstep. We've also partnered with Indonesia's largest lab provider to provide lab home services, and to top it off we have recently launched a premium appointment service that partners with 500+ hospitals that allow patients to book a doctor appointment inside our application. We are extremely fortunate to be trusted by our investors, such as the Bill & Melinda Gates Foundation, Singtel, UOB Ventures, Allianz, GoJek, Astra, Temasek, and many more. We recently closed our Series C round and In total have raised around $180 million for our mission. Our team works tirelessly to make sure that we create the best healthcare solution personalized for all of our patient's needs, and are continuously on a path to simplify healthcare for Indonesia.

Preethi B

Mastering Quality Across the Full Stack