It has been almost three years since Selenium 3.0 released in October 2016. And after three years, in April 2019, Selenium 4 alpha version has been introduced. Subsequently, in this tutorial, we will have a quick look at what Selenium 4.0 has to offer.
Note: We will refer Selenium 4.0 Alpha as simply Selenium 4.0.
What is Selenium 4.0?
Selenium 4.0 is the latest version of Selenium libraries.
We can take a look at the latest version of these libraries in Maven central.
As seen in https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-remote-driver/4.0.0-alpha-1 above, 4.0.0-alpha-1 introduced in April 2019.
It is still an alpha version, and its stable version is yet to be released. However, currently, the latest alpha version available is Selenium4.0.0-alpha-3, as can be seen on the Selenium HQ page, https://selenium.dev/.
What can we expect in Selenium 4.0?
Here are some of the features promised for Selenium 4.0 in all three products of the Selenium family:
- Selenium IDE
- Selenium Grid
- Improved Observability
- Improved Documentation
Selenium IDE: New and refreshed Selenium IDE
- Selenium IDE is a tool for record and playback. Additionally, it will have advanced and rich features in Selenium 4.0.
- Moreover, the limitation of a lack of ability for parallel execution will overcome in the new version of IDE. In other words, a new version is coming with improved browser support. Additionally, parallel execution will be possible along with the passed/failed status of test cases and execution time.
- New IDE is W3C compatible and completely dependent on WebDriver.
- Moreover, the new version will have a new Node JSbased CLI runner.
WebDriver API’s W3C standardization:
In test automation, the use of WebDriver API is not just confined to be part of Selenium; it is consumed in various other automation tools as well. For Example, the Appium tool, used for Mobile testing leverages Web Driver API. Similarly, it is used in iOS drivers also. One of the most significant changes is that Selenium 4 is WebDriver API standardization as per the W3C standards. Effectively, now, WebDriver API will be compatible with implementing across multiple platforms. However, with internal architecture-related changes in APIs, automation scripts developed using Selenium 2 and 3 will not be impacted.
Introduction of Friendly/Relative Locators:
One of the challenging tasks in writing test automation script is the identification of an element on the web page. For this, Selenium already provides multiple explicit locators like name, id, XPath, etc. Selenium 4 is coming with one more new locator, i.e., Relative Locator. It provides a way to locate element by its position concerning other elements as follows:
- above: To locate web element appearing above the specified element
- below: To locate web element appearing below the specified element
- toLeftOf: To locate web element appearing to the left of the specified element
- toRightOf: To locate web element appearing to the right of the specified element
- near: To locate web element appearing to near of the specified element. Here near means the maximum of 50 pixels away from the specific element
For Example, in any login page, usually following fields are displayed:
In this web page, if you have to locate the password text field, RelativeLocator class method can be used as follows :
WebElement usernameField = driver.findElement(By.id(“username”));
WebElement passwordField = driver.findElement(RelativeLocator.withTagName(“input”).below(usernameField));
Devtools in chromium:
Selenium 4 also offers one of the important features of exposing devtools APIs from chromium derived drivers. Chrome devtools are a set of web developer tools that are built directly into Google Chrome. These tools help edit pages on the fly, for troubleshooting debugging. Chrome Dev Tools are supported out-of-box in Selenium 4. As seen in the screenshot of library org.openqa.selenium.devtools below, following packages, will available in Selenium script :
- Application Cache
- Target etc
With devtools APIs availability, one can trigger DevTools commands by the test script like this :
DevTools devTools = ((ChromeDriver)driver). getDevTools();
This access to devtools API provides some of the important capabilities in selenium script to control the browser:
- Listen Chrome Console logs by using addListener()method
- Accepting security warnings appearing for web site
- Modification of user-agent request header
- Perform security operations
Network and performance analyzers:
As seen in packages provided by the development tool, one of the great packages available is Network. It provides class methods which offer the following capabilities:
- Intercepting requests
- Emulating Network Conditions by changing ConntectionType
- Enabling network tracking
Also, performance package classes give the provision to analyze runtime performance by providing methods for collecting and reporting duration metrics.
- Support for the Chromium-based Edge browser: Since version v188.8.131.52-alpha-2, selenium web driver supports Chromium-based Edge browser, i.e., automation script can run on Chromium-based Edge browser also.
- Full page screenshot in Firefox: Since version v184.108.40.206-alpha-2, the addition of full-page screenshot support for the Firefox browser happened. [Prajakta: Though this one can see it in the changelog, I could not find any detailed information on this. Most of the information I searched on is about Ashot utility, but not related to Selenium4 feature.]
- Element level screenshots: v220.127.116.11-alpha-1 provided one of the essential features of element level screenshots, i.e., Element screenshotting is now possible with Selenium 4.[Prajakta: Here, I could not find more information or sample. Detected some samples using getScreenshotAs() method which is already available in Selenium 3]
Selenium Grid: Better suited to the modern world
- Current Selenium Grid 2 has been developed long back, i.e., in 2011. So, Selenium 4 is coming with a fresh architecture with Selenium Grid version 4.It promises to be Modern, Scalable, Observable
- Let’s understand briefly what Selenium Grid is. Selenium Grid consists of 2 components:
- Node: It is the machine where test script execution takes place
- Hub: This is the server machine controlling other machines in the network. Selenium Grid allocates the execution of test cases to a specific node, and it has only one hub.
- Earlier configuration and installation used to be challenging due to various issue which used to occur during installation or configuration, also connecting the hub with the node machine. But, now, with Selenium Grid version 4, Selenium grid setup has become hassle-free as on the starting of Selenium Grid, it functions as both modes, i.e., Node and Hub.
- New Grid version also allows parallel test case execution on multiple browsers, systems as well as operating systems without much hassle.
- As Selenium 4 grid promises to be modern, it provides support to advanced tools useful in the DevOps process like Docker, Kubernetes, etc.
- The new Selenium 4 Grid server also permits the user to output logs in a single line format in JSON.
- Nicer Grid UI: Selenium Grid 4.0 version grid UI is improved to be more user-friendly.
Selenium 4.0 provides improved observability by providing details information of hooks, tracing of requests initiated, etc. Thus makes debugging easier,
It has been quite a long time since Selenium documentation updated with Selenium 2.0 release somewhere in the year 2012. Since then, there is no update of the documents or tutorials. With Selenium 4.0, SeleniumHQ will be delivering the long-awaited promise for test automation engineers, i.e., updating of Selenium documents.
How to get hands-on Selenium 4.0 WebDriver API?
Does it generate interest to have a look at this new make-over version of Selenium? Well, the release of a stable version of Selenium 4.0 has not yet happened, one can download an alpha version from the selenium site. And the latest version of the alpha release is Selenium4.0.0-alpha-3.
One can download the Alpha version of Selenium 4.0 from https://selenium.dev/downloads/ page
To try out this new version, We should follow the below steps need to try out this new version :
- Download Selenium 4.0.0-alpha-3. jar from https://selenium.dev/downloads/ [Note, that SeleniumHQ.org page is currently updated find with improved UI if entered seleniumhq.org, it gets redirected to https://selenium.dev/]
- Save the jar in some folder like C:/seleniumJars
- Create Java project and ‘C:/seleniumJars’ in the build path
- You can try this in the existing selenium project. Just be replacing Selenium 3.0 jar path by this 4.0 jar path.
- Once, the project gets refreshes, it’s time to start the latest Selenium WebDriver API
How to verify Selenium 4.0 WebDriver API is available, or installed successfully?
Just add the following line in any of the Selenium scripts of your project:
import static org.openqa.selenium.support.locators.RelativeLocator.withTagName;
This line should not throw any compilation error.
In case, upgraded WebDriver API is still not correctly available in the project; we will get the following error:
In the case of the above error, it needs to check that if the project build path is pointing to 4.0 jar and then refresh the project.
So, this is all about an introduction of the upcoming release of Selenium, i.e., Selenium 4.0
To conclude, in this tutorial, we have covered the latest Selenium new features in Selenium IDE, WebDriver API, and Selenium Grid. Moreover, we have also seen the installation of the Selenium 4.0 Web Driver. It covers an introduction to the new version of Selenium, which is going to released soon. During WebDriver’s new features, we have briefly covered a new locator called Relative Locator. We will be covering Selenium Relative Locators in more detail in the next tutorial.