In this post, we’ll take you through everything you need to know about Selenium 4. We’ll start with a basic introduction to Selenium, new features of Selenium 4, the Difference between Selenium 3.0 and Selenium 4.0, and the following.
Selenium is one of the leading test automation tools in the market. Selenium is first launched in 2004 as Selenium Core. In 2007, Selenium IDE & Selenium WebDriver are added. Later on, Selenium tools were named Selenium 2(2011), Selenium 3(2016). Now Selenium is coming as Selenium 4.0.
Note: Still stable version is not released. The current version is Selenium 4 (Alpha 7), released on 10th November’20. We hope that all the features we are going to list here will be available in the next official Selenium 4 release. Until Selenium 4 is officially released, It’s not recommended to upgrade or migrate your existing Selenium Project
What is New in Selenium 4?
To use this there is no need for extensive programming knowledge. It supports rapid test development. It is like a record and playback tool.
Selenium Grid 4 allows us to run our test scripts across multiple machines or systems.
Difference between Selenium 3 and Selenium 4
Selenium 3 vs Selenium 4
JSON wire protocol was used to communicate between the Selenium Webdriver APIs and the browser native APIs. All the requests and responses communicated across the protocol were encoded & decoded.
It follows the W3C standard protocol (driver and the browser communication follows the standard procedure) due to this request and the response communicated across the protocol doesn’t require the encoding and decoding API.
Selenium 4 Features
On October 13, 2021, Selenium 4 was released as a stable version, following the Alpha and Beta releases of its predecessor. The Selenium 4 features introduced are far more promising than anyone could have imagined, which is exactly what we’ll discuss in this section.
#1. WebDriver changed to W3C Standardization
The major change in Selenium 4 is the standardization of the WebDriver API with respect to W3C standards to encourage compatibility across various software implementations. With this change, requests and responses communicated across the protocol don’t require the encoding and decoding API. Based on the W3C standards updation, any software that follows W3C standards can be integrated with Selenium 4 without any compatibility issues. Almost all browsers such as Chrome, Safari, and IE are already W3C standard compliant.
#2. Selenium Grid Optimization
Even though Selenium Grid was developed long back in 2011. With Selenium 4, Selenium Grid is coming up with improvised UI and stability by removing the issues occurred in the earlier version of Grid like installation, the connection between the hub and node.
It allows us to execute the test cases in parallel on multiple operating systems, multiple browsers, and in different versions.
The Grid UI is flexible, user friendly and information like session’s capacity, run time can be found in the UI.
Nodes can be multiple in a grid and these are used to execute test scripts on an individual systems.
Hub can be used as a central point from where we can allocate test scripts execution to different nodes in the network.
There is no need to set up to the start the hub & nodes individually once we start the server. The Grid automatically works as a hub and nodes.
It also supports tools like AWS, Azure, and helps in the process of DevOps.
Check this link for more information on Selenium Grid
#3. Relative Locators
Relative locators (aka Friendly locators) allow us to locate the WebElements by its position by concerning other web elements such as above, below, toLeftOf, toRightOf, and near.
In simple words, relative locators allow us to locate web elements based on their position with respect to other web elements.
There are five locators newly added in Selenium 4:
- above(): It is to locate a web element just above the specified element
- below(): It is to locate a web element just below the specified element
- toLeftOf(): It is to locate a web element present on the left of a specified element
- toRightOf(): It is to locate a web element present on the right of a specified element
- near(): It is to locate a web element at approx. 50 pixels away from a specified element. The distance can be passed as an argument to an overloaded method.
Note: Method “withTagName()” is added which returns an instance of RelativeLocator. Above relative locators support this method “withTagName()“
//Assume element1 is above element2
element1 = driver.findElement(By.id("1234"));
String element2 = driver.findElement(withTagName("ABC").below(element1)).getText();
System.out.println("Element below element1 is "+ element2);
//Assume element0 is above element1
element1 = driver.findElement(By.id("1234"));
String element0 = driver.findElement(withTagName("ABC").above(element1)).getText();
System.out.println("Element above element1 is "+ element0);
//Assume element1 is on the left side of element3
element1 = driver.findElement( withTagName("ABC").toLeftOf(element3)).getText();
System.out.println(" Left of element 3 is : "+ element1);
//Assume element3 is on the right side of element1
element3 = driver.findElement( withTagName("ABC").toRightOf(element1)).getText();
System.out.println(" Right of element 1 is : "+ element3);
Check out a detailed guide on Selenium Relative Locators
#4. Multiple Tabs & Windows
With Selenium 4, we can work with multiple tabs or windows. We can open a new tab or window in the same session without creating a new driver object.
- To create a new Tab by passing WindowType.TAB to newWindow() method
- To create a new window by passing WindowType.WINDOW to newWindow() method
Note: To switch between the windows, we have to fetch the window IDs and pass it to the switchTo().window()
#5. Browsers Support
Native support has been removed for Opera and PhantomJS. Since the WebDriver implementations for these browsers are no longer under active development.
- If you are an Opera user then you can use Chrome.
- If you are a PhantomJS user then you can use Chrome or Firefox in a headless mode.
#6. Selenium IDE
Selenium IDE is a tool used to record and playback. It allows us to interact with browsers. The previous version of Selenium IDE was deprecated in the year 2017. The latest version of Selenium IDE is coming up with man more advanced capabilities.
- With better UI & user experience
- CLI runner (Selenium-side-runner) which runs on NodeJs allows us to perform playback and parallel execution on multiple browsers.
- It also provides reports with information like a total number of test cases executed with execution time, no. of test cases passed, and no. of test cases failed.
- It allows users to run on browsers like Firefox, Google Chrome, IE, etc.,
#7. WebElement Screenshot
Selenium 4 allows screenshots to be taken at different UI levels like element, section and full page. We will discuss why this feature is necessary and how it can be achieved in Selenium 4, along with sample code exxamples.
Selenium 3 allows users only to capture a webpage and no provision to capture screenshot of a specific web element. Selenium 4 allows us to take a screenshot of a particular web element.
WebElement screenshot =driver.Findelement (By.xpath("YourXpath"));
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
File dstFile = new File("Image.png");
#8. Chrome DevTools
Selenium 4 has native support for Chrome DevTools protocol through the “DevTools” interface. With this, we can get Chrome Development Properties such as Application Cache, Fetch, Performance, Network, Profiler, Resource Timing, Security, and Target CDP domains, etc.,
Chrome DevTools is a set of web developer tools. These have been built directly to the Chrome Browser. It allows us to edit the web pages and diagnose problems quickly. This way we can make better websites and deliver it faster.
We will see Chrome DevTools in detail later.
Logging and request tracing processes have been improvised to fasten the debugging process which resolves the script issues easily.
Selenium 4 comes out with official and detailed documentation on Selenium IDE, Selenium WebDriver, and Selenium Grid.
Documentation of Selenium WebDriver is available and you can find it here.
Download Selenium 4
You can find Selenium 4 Alpha in the Maven repository. You can download it from Selenium official website
Download Selenium 4 (Alpha 7) Maven Dependencies from here
How To Upgrade From Selenium 3 To Selenium 4?
If you would like to upgrade from Selenium 3 to Selenium 4 then you must check our step by step Selenium 4 Installation Guide to Install Selenium 4.
What is deprecated in Selenium 4
Some of the methods deprecated in Selenium 4 are as follows
FindsBy interface is a part of org.openqa.selenium.internal package implemented by the RemoteWebDriver class. The methods in that are findElement(By), findElements(By).
Note: These changes won’t affect end users. You can use By class with findElement(By) and findElements(By).
The Actions class is used to emulate user gestures like mouse movements, hoverings etc., In the Selenium 4, some new methods have added to the Actions class as a replacement of the classes under org.openqa.selenium.interactions package.
click(WebElement) is a method to click an element. It is added newly to Actions class in replacement of moveToElement(onElement).click()
clickAndHold(WebElement) is a method to click on an element without releasing. It is added to Actions class in repalcement of moveToElement(onElement).clickAndHold()
contextClick(WebElement) is a method to right click on an element. It is added to Actions class in replacment of moveToElement(onElement).contextClick().
doubleClick(WebElement) is a method to double click on an element. It is added to Actions class in replacment of moveToElement(element).doubleClick().
release() is a method to release the depressed left mouse button at the current mouse location. release() is moved to Actions class from org.openqa.selenium.interactions.ButtonReleaseAction class.
#3. Fluent Wait
The methods in the fluentwait such as withTimeout() and pollingEvery() have been modified.
#3.1. Fluentwait in Selenium 3
FluentWait wait = new FluentWait(driver)<br>.pollingEvery(20, TimeUnit.MILLISECONDS)<br>.withTimeout(20, TimeUnit.SECONDS)<br>.ignoring(NoSuchElementException.class);
#3.2. FluentWait in Selenium 4
Selenium 4 replaced the TimeUnit with Duration
FluentWait wait = new FluentWait(driver)<br>.pollingEvery(Duration.ofMillis(200))<br>.ignoring(NoSuchElementException.class)<br>.withTimeout(Duration.ofSeconds(20));
#4. Driver Constructors
Few driver constructors have been deprecated. Capabilities objects have been replaced with Options.
We have to create an Options object for the Driver class we use.
FirefoxDriver Capabilities is replaced by FirefoxDriver FirefoxOptions.
FirefoxOptions options = new FirefoxOptions();<br>options.setAcceptInsecureCerts(true);<br>FirefoxDriver driver = new FirefoxDriver(options);<br>driver.get("https://www.google.com");
ChromeDriver Capabilities is replaced by ChromeDriver ChromeOptions.
ChromeOptions options = new ChromeOptions();<br>options.setAcceptInsecureCerts(true);<br>ChromeDriver driver = new ChromeDriver(options);<br>driver.get("https://www.softwaretestingmaterial.com");
InternetExplorerDriver Capabilities is replaced by InternetExplorerDriver InternetExplorerOptions.
<br>InternetExplorerOptions options = new InternetExplorerOptions();<br>options.setAcceptInsecureCerts(true);<br>InternetExplorerDriver driver = new InternetExplorerDriver(options);<br>driver.get("https://www.softwaretestingmaterial.com");
SafariDriver Capabilities is replaced by SafariDriver SafariOptions.
SafariOptions options = new SafariOptions();<br>options.setAcceptInsecureCerts(true);<br>SafariDriver driver = new SafariDriver(options);<br>driver.get("https://www.softwaretestingmaterial.com");
EgdeDriver Capabilities is replaced by EdgeDriver EdgeOptions.
EdgeOptions options = new EdgeOptions();<br>options.setAcceptInsecureCerts(true);<br>EdgeDriver driver = new EdgeDriver(edgeOptions);<br>driver.get("https://www.softwaretestingmaterial.com");
In this tutorial, we have covered new features of Selenium 4.0 such as W3C standardization, brand new Selenium IDE, Relative Locators, Multiple Tab/Windows, and others. We hope Selenium 4.0 gives us the best experience and ability to do almost all the tasks which are unfulfilled in Selenium 3.
Share your thoughts on these changes to Selenium in the comments section below.
Must read: Selenium 4 Tutorial