Selenium 4 is here!
Here’s a quick flashback on Selenium before we proceed with Selenium 4.
If you google, “Which is the most widely used automation testing tool?”, the results will show “Selenium”. Don’t believe me? Feel free to check it out, but first read through this 15 mins long article if you want to learn about the latest version of the tool.
As most of you may know, Selenium is a free (open-source) automation testing tool, used to validate web applications across different browsers and platforms. Multiple programming languages such as Java, C#, and Python can be used to create Selenium test scripts for automation.
When most people hear “Selenium”, they think of Selenium WebDriver. But, did you know there are two more significant tools along with WebDriver that makes the selenium project complete — Selenium Grid and Selenium IDE.
There is an Interesting story behind the name of this tool. During Selenium’s development, another automation testing tool, QTP, made by the company Mercury Interactive, was popular.
Since Selenium is a well-known antidote for Mercury poisoning, Jason Huggins, the creator of selenium, suggested the name Selenium and his team agreed. So that’s how we got the name for this tool. Isn’t it interesting?
This tool was born back in 2004. Over the years with different releases like WebDriver in 2006, Selenium RC, Selenium IDE, Selenium Grid, Selenium 2, Selenium 3and now with Selenium 4, it has gradually evolved to be more efficient and include more features. Let’s take a look at some of the key new features that Selenium 4 offers, along with its improvements over Selenium3.
Let’s look at the key new features in detail and understand how they can be leveraged for efficient automation.
- Relative Locators
This is the most exciting new functionality of this release and allows you to tell Selenium what element to interact with, based on its position relative to other web elements using a very human-readable syntax.
If you have automated complex GUIs, you must know how tedious and time consuming it is to come up with the locator-strategy for some of the elements. With relative locators, it’s very easy to locate elements based on the visual location relative to other elements.
For example, you want to locate the password field, but its element id is dynamic. In such a case, normally, you would need to come up with a relative xpath or use some other locator-strategy, but with relative locators, this can be done very easily.
Here is the code snippet:
WebElement emailAddressField = driver.findElement(By.id(“email”));
WebElement passwordField= driver.findElement(withTagName(“input”) .below(emailAddressField));
As you can see in the above example, we have used the method ‘below’ to locate the password field making it very easy since we do not need to resort to using any complex element locator. This will also save time and reduce the locators in the test repository. Similarly, to the ‘below’ method, we can use the other methods which are — ‘above’, ‘to_left_of’, ‘to_right_of’, and ‘near’.
This is a useful feature to have because it gives you the ability to find elements using a more human way of describing things.
- Ability to take element screenshot
In Selenium 3, you need to capture a screenshot of the entire web page but in Selenium 4, there is a new option to capture screenshots of a particular web element. Hence, there is no need to use third-party tools for capturing a screenshot of a web element.
This helps in debugging test case failures with more precision.
- Browser window and tab management capabilities
If you have automated an application which opens multiple windows using Selenium, you may know how it could get difficult to manage using getWindowHandle and getWindowHandles methods.
To overcome this, Selenium 4 provides a new API called newWindow().
This new API lets us create a new window or tab and switch to it automatically. Since a new window is created in the same session it avoids creating a new WebDriver session which was the cases in the earlier version.
Here is the example of the new API:
Open a new Window in Selenium 4
Open a new Tab in Selenium 4
- Selenium IDE with control flow mechanism and resilient tests
Selenium IDE for automation was never a first choice of testers before. Even though the Selenium IDE offered ease of usage, it had several issues such as no support for cross browser testing running parallel tests or controlling flow among others. This is why when development for the Selenium IDE stopped in 2017, a lot of people thought that Selenium IDE has died.
But with the latest version of Selenium IDE 3.17.0, all of this changed and now once again, its usage has gradually increased. The reason? The new version of Selenium IDE now supports browser testing, parallel tests, and more.
The major changes are now in the newer versions of Selenium IDE — it now supports control flow’. As you know, during automation test execution, conditional statements are very important. With the new control flow command, it’s possible in the new Selenium IDE. Following are some of the control flow commands that help with conditional branching and looping in Selenium IDE.
if, else if, else, end
do, repeat if, while, end
Apart from this, in the new Selenium IDE version, we get the ’Resilient Tests’ feature, which means that Selenium IDE now records multiple locators for each element that it interacts with. During playback, if the Selenium IDE is unable to find a particular locator, it will try with all the other locators on the web page until one of them is successful. This reduces test failure when there is a change in the GUI.
Another major change in Selenium IDE is, now we can reuse the test cases. This is very important because it reduces the efforts of recording the same steps again and again for each test case.
- Chrome DevTools Protocol
Selenium 4 offers one of the most important features of exposing DevTools APIs from chromium drivers. This helps us getting Chrome Development properties such as:
- Application Cache
- Resource Timing
- Target CDP domains
With DevTools APIs, one can trigger DevTools commands by using the test script. This is useful in accepting security warnings appearing for websites. If you want to get into its details, check out the class named DevTools. This class mentions all the required methods that a tester can use to interact with the developer options.
- W3C compliance of WebDriver
As I mentioned earlier the most significant change in Selenium 4 is W3C compliance of WebDriver APIs, but what does it mean actually? Let’s understand.
The earlier version of Selenium, i.e., Selenium 3, used the JSON wire protocol to communicate with the Web browser, which had an overhead of encoding and decoding the API requests using the W3C protocol.
As most of the browser’s drivers follow W3C protocols for communication and the Selenium 4 WebDriver APIs adopt the W3C standardization, it will directly communicate with the web browser. This means that browser communication will be faster and more reliable.
Due to this, test cases will be less flaky, and we will be able to do more stable cross-browser testing. After reading this you might have some questions like “When, where, and who can use this tool?”
The answers to these questions are quite simple. Use Selenium 4 if you want to:
- Quickly automate boring/repetitive tasks
- Get started with automating your test suite
- Find a simple way to report a bug by having your users create a Selenium IDE test of the issue.
- Automate testing even if your team are not programmers
As Selenium’s official website states: “Primarily it is for automating web applications for testing purposes but is certainly not limited to just that. Boring web-based administration tasks can (and should) also be automated as well.”
For automating such web-based tasks Selenium IDE is perfect as it does not require any programming skill.
- SIDE Runner for Cross-Browser Testing
With SIDE runner, Selenium IDE is now not only a record and playback tool but much more than that. SIDE is a command-line test runner in Selenium IDE. It allows you to execute your test cases in different browsers, you just need to install SIDE runner and get the necessary browser drivers if the tests must be executed locally.
Parallel Test execution is also possible now with SIDE runner scripts (.side files). Due to the SIDE scripts, it is possible to integrate the Selenium IDE tests with popular CI tools like Jenkins, Travis, and more.
- Optimized Selenium Grid with Docker Container Support
Selenium Grid 4 is a new implementation and does not share the codebase with the previous version. It also leverages various new technologies to facilitate scaling up along with local execution. The earlier version of Grid supported the standalone and HUB-Node modes for Selenium Grid. But in Grid 4 supports 4 different modes which are as follows.
- Hub and Node
As mentioned above, Grid 4 provides two more modes —distributed, and with dockets. This is the key new improvement in the Selenium Grid. Along with this, Grid 4 also works well with Kubernetes, AWS, GCP, and Azure.
And you can scale a grid to enormous sizes, only if you can afford the price of running an enormous grid on public infrastructure. But the point to note is that it’s designed to scale horizontally.
Apart from these key new features Selenium 4 has some major improvements, as mentioned below.
- Improved Docker Support for Selenium Grid.
- Improved Action classes in Selenium WebDriver for mimicking the user actions.
- Improved debugging in the Selenium IDE along with modular test cases which can be reused.
Parting Words and Words of Wisdom about Selenium:
This section is for those who reached here after reading the complete article and as well as for those who skipped it and jumped directly at the end to get the gist of this interesting read.
To sum it up, Selenium 4 has a lot of new features in the beta version released recently (15 Feb. 2021).
The new features include:
- Relative locators to reduce time and efforts over locator strategy
- Chrome DevTool APIs accessing browser properties like network, security, and more.
- W3C compliance WebDriver which will help in stable cross-browser testing.
- All new Selenium IDE to help automate boring and repetitive web-based tasks.
- SIDE Runner for cross-browser testing
- Selenium grid 4 with Docker container support to scale up the grid.
Selenium 4 has a lot of potentials to accelerate the testing activities, which was not possible in its earlier versions. Here are some words of wisdom, from Simon Stewart, creator of the open-source project Selenium WebDriver.
“The best piece of advice regarding your automation efforts is to remember the test pyramid.
If you have many small tests, a few integration tests, and maybe one or two end-to-end Selenium tests, you’re doing it right.”
So, let us know if you and your teams are doing it right. You can also contact our testing experts for a free consultation and more information.Talk to our Experts for more details