#TECH

What’s new in the Selenium 4.0 release?

Selenium 4.0 stable version was officially released on 13th October 2021. Now let’s take a look at what it will offer. Communication among client-servers by W3C Web driver standardization protocols, multiple enhancements in grid, easy navigation among tabs and window, full screen capture possibilities, new relative locators inspired by native english language and many more.    

Now the question is do we take it as a pinch of salt or celebrate? Let’s look at what has changed.   

Selenium 4 Features

World Wide Web Consortium (W3C) Standards for WebDriver 

In Selenium 3, communication between selenium web-driver APIs and browser native API was done by a phenomenon in which a client (user machine) on which web-driver is installed sends an object to the server in an JSON form. This JSON was used in encoded and decoded formats. 

Now in selenium 4, encoding and decoding of JSON objects is not required as it is based on W3C standard protocol. It follows a standard procedure which makes requests and responses to interact without any encryptions.

Grid Enhancements

Back in 2011, grid was developed in which various issues were associated like installation was complex and connection set up between node and hub was very trivial. With Selenium 4, a stable version is given in which the above mentioned issues are covered. Moreover, the user interface has become more user friendly as information like session capacity are visible on the go. Text executions are possible now on multiple nodes by a central point called hub. 

New Locators and methods

Five locators newly added in Selenium 4 above, below, toLeftOf, toRightOf, and near.

There are:

  • above(): locates element above the reference element
  • below(): locates element below the reference element
  • toLeftOf(): locates element left of the reference element
  • toRightOf(): locates element right of the reference element
  • near(): locates element near the reference element with distance of 50 px. Distance is added as an attribute.
  • Method “withTagName()” returns an instance of above relative locators. 

WebElement elementAbove;
elementAbove = driver.findElement(By.id("elementid"));
String elementBelow = driver.findElement(withTagName("tagname").below(elementAbove)).getText();
System.out.println("Element below elementAbove is "+ elementBelow);

 


Multiple Tabs & Windows

In the new release, there’s no need to create a new driver object in the same session to open a new tab or window.

  • Pass WindowType.TAB to newWindow() method will create a new tab
  • Pass WindowType.WINDOW to newWindow() method will create new window

Note: For switching between windows, pass the window ID to switchTo().window()

Tab:

driver.get(https://www.youtube.com/)
driver.switchTo.newWindow(WindowType.TAB);
driver.navigate().to(https://www.google.com/);

 

Window:

driver.get(https://www.youtube.com/)
driver.switchTo.newWindow(WindowType.WINDOW);
driver.navigate().to(https://www.google.com/);

Browsers Support Revoked for Opera and PhantomJS

PhantomJS was a headless browser which was used for headless browser interactions. Opera, a web-browser developed by company Opera which is currently called chromium. Implementations of Webdriver for browsers PhantomJS and Opera are deprecated, so in this release support on these browsers is no longer available now. But, users can use a walkaround for these browsers as well. For opera(chromium), use chrome browser and for phantomJS use headless mode on chrome or firefox.  

Selenium IDE

In 2017, IDE got deprecated. Now, it is introduced with multiple features. 

  • User Interface being more enhanced and the experience becomes user friendly.
  • Can export code for Java, Python, JS, .Net, Ruby.
  • For multi-browser parallel execution and playback functionality command line interface which is a selenium side runner is used which uses NodeJs. 
  • More detailed reports are shown with the count of the total, passed, and failed test cases with their execution time. 

Detailed Screenshot

In Selenium4, a new method getScreenshotAs() is introduced which helps in capturing a screenshot of the element. Basic process to capture remains the same, find the webelement on DOM and use the method to capture the screenshot. 

Also, another method getFullPageScreenshotAs() was used to capture only the screenshot of the visible current page. Previously, it worked on firefox driver.  Now, in selenium 4, it takes a screenshot of the entire page, not dependent on whether the page is visible or not. 

What is deprecated in Selenium 4?

1. FindsBy

FindsBy interface which is present in org.openqa.selenium.internal package implemented by the RemoteWebDriver class. The methods in it are findElement(By), findElements(By) where you can use By Class. 

2. Actions

New methods are now added as a replacement of classes under package org.openqa.selenium.interactions.

2.1. click

moveToElement(onElement).click() is replaced by click(WebElement). This helps in clicking on specific elements. 

2.2. clickAndHold

moveToElement(onElement).clickAndHold() is replaced by clickAndHold(WebElement). This helps in clicking on a specific element without a release. 

2.3. contextClick

moveToElement(onElement).contextClick() is replaced by contextClick(WebElement). This helps in right clicking on specific elements. 

2.4. doubleClick

moveToElement(element).doubleClick() is replaced by doubleClick(WebElement). This helps in double clicking on specific elements.

2.5. Release

release() method moved to Action class from org.openqa.selenium.interactions.ButtonReleaseAction. This helps in release of click and hold. 

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)
.pollingEvery(20, TimeUnit.MILLISECONDS)
.withTimeout(20, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
3.2. FluentWait in Selenium 4

TimeUnit is replaced with Duration

FluentWait wait = new FluentWait(driver)
.pollingEvery(Duration.ofMillis(200))
.ignoring(NoSuchElementException.class)
.withTimeout(Duration.ofSeconds(20));

4. Driver Constructors

Options replace the capabilities objects. Option Object is to be created for the driver class. 

4.1. FirefoxDriver

FirefoxDriver FirefoxOptions replaced FirefoxDriver Capabilities.
FirefoxOptions options = new FirefoxOptions();
options.setAcceptInsecureCerts(true);
FirefoxDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
4.2. ChromeDriver

ChromeDriver ChromeOptions replaced ChromeDriver Capabilities.

ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
4.3. InternetExplorerDriver

InternetExplorerDriver InternetExplorerOptions replaced InternetExplorerDriver Capabilities.

InternetExplorerOptions options = new InternetExplorerOptions();
options.setAcceptInsecureCerts(true);
InternetExplorerDriver driver = new InternetExplorerDriver(options);
driver.get("https://www.google.com");
4.4. SafariDriver

SafariDriver SafariOptions replaced SafariDriver Capabilities.

SafariOptions options = new SafariOptions();
options.setAcceptInsecureCerts(true);
SafariDriver driver = new SafariDriver(options);
driver.get("https://www.google.com");
4.5. EdgeDriver

EdgeDriver EdgeOptions replaced EdgeDriver Capabilities.

EdgeOptions options = new EdgeOptions();
options.setAcceptInsecureCerts(true);
EdgeDriver driver = new EdgeDriver(edgeOptions);
driver.get("https://www.google.com");

Conclusion

Multiple frameworks for automation are available for a tester to choose from but Selenium always seems to be a widely used framework. The new features are attractive for every QA engineer. In my opinion, the features that are added will act as a boom for QAs who believe in the left-shift paradigm. 

Selenium 4 will be quite promising as it helps in making the test process simpler.  One of the crucial enhancements are to influence containers by docker which was priorly performed by users to set up the machines and to scale-up, the grid can also be deployed on Kubernetes. 

For more information, you can read on https://www.selenium.dev/downloads/.

You might also like