Wednesday, December 1, 2021

Selenium 3 vs Selenium 4

 


Selenium has four components as below:

 

 



Earlier Selenium interacted with browsers. The request and response of communication are through JSON Wire protocol. The JSON Wire protocol encodes and decodes all the communications between Selenium and the Browsers.

 



 


 


Selenium 4.0
- initially announced in 2018. we had got alpha and beta versions.
- officially released the first stable version of Selenium 4 On October 13, 2021, and brought lots of upgrades with this release

 

What’s New in Selenium 4

-          Selenium is now W3C compliant

-          Relative locators: Elements can be located based on the location of the neighbouring elements.

-          Better Window/Tab management

-          Improved Selenium Grid

-          Upgraded Selenium IDE

-          New APIs for CDP (Chrome DevTools Protocol)

-          Deprecation of Desired Capabilities. New Options class introduced.

-          Modifications in the Actions class

Selenium is now W3C compliant

W3C: The World Wide Web Consortium

-          JSON wire protocol was used to communicate between the Selenium WebDriver APIs and the browser native APIs.

-          With W3C compliance, communication happens directly without required encoding and decoding.

-          Any software following W3C standard protocol can be integrated with Selenium with no compatibility issues.

Relative locators:

-          Functions to locate nearby elements in specific directions.

-          Existing locator strategy

o   id

o   name

o   linkText

o   partialLinkText

o   className

o   tagName

o   XPath

o   cssSelector

-          New relative locators

o   above

o   below

o   toLeftOf

o   toRightOf

o   near

Better Window/Tab management:

-          work with multiple windows or tabs in the same session.

-          Can now open multiple windows/tabs without creating a new driver object.

o   Open a new window and switch to the window

§  Driver.switchTo().newWindow(WindowType.WINDOW);

o   Open a new tab and switch to the tab

§  Driver.switchTo().newWindow(WindowType.TAB);

Improved Selenium Grid:

-          Selenium Grid helps in Distributed Test Execution.

-          Enables test execution on different combinations of browsers, OS, and machines

-          Enables parallel execution.

Selenium Grid is now redesigned

-          Docker support

-          Enables to spin up the containers. (No need to set up VMs)

-          Enables to deploy the grid on Kubernetes for better scaling

-          Easier management – No need to set up and start hubs and nodes separately.

3 ways to run Selenium Grid

Standalone mode

The new Selenium Server Jar contains all the functionalities needed to run a grid

 java -jar selenium-server-4.0.0.jar standalone

 The Grid automatically identifies that the WebDrivers for Chrome and Firefox are present on the system.

Hub and Node

The classical way of using the Grid for Selenium test automation consists of two major components – Hub and Nodes.

 Start hub java -jar selenium-server-4.0.0.jar hub

Register node java -jar selenium-server-4.0.0.jar node –detect-drivers

Distributed

Grid 4 can be started in a fully distributed manner, with each piece running in its process.

 Selenium Grid 4 consists of FOUR processes:

Router

Distributor

Session

Node

 

 

 Upgraded Selenium IDE:

-          Record and Playback tool

-          Available as an add-on – Firefox, Chrome, MS Edge

o   Improved GUI

o   SIDE runner – Selenium IDE runner for CMD execution, grid and node projects

o   Better element locator strategy

o   More stable and reliable

New APIs for CDP (Chrome DevTools Protocol):

Chrome DevTools – set of tools built directly into Chromium-based browsers like Chrome, Opera and Microsoft Edge to help developers debug and investigate websites

-          Inspect elements in the DOM

-          Edit elements and CSS on the fly

-          Check and monitor the site’s performance

-          Mock faster/slower networks speeds

-          Mock geolocations of the user

-          Execute and debug JavaScript

-          View console logs

Selenium 4 comes with native support for Chrome DevTools APIs

-          Capture and monitor network traffic

-          Simulate poor network conditions

-          Perform geolocation testing

-          Change device mode to do responsive design testing

-          new ChromiumDriver class, which includes two methods to access Chrome DevTools:

o   getDevTools()

o   executeCdpCommand()


Deprecation of Desired Capabilities:

Desired Capabilities were primarily used in the test scripts to define the test environment (browser name, version, operating system) for execution on the Selenium Grid.

 Capabilities objects are replaced with the Options class.

Firefox – FirefoxOptions

Chrome – ChromeOptions

Internet Explorer – InternetExplorerOptions

Microsoft Edge – EdgeOptions

Safari – SafariOptions

 

For example:

 ChromeOptions options = new ChromeOptions();

options.setAcceptInSecureCerts(true);

options.setCapability(“build”, “Testing Chrome Options Selenium 4”);

options.setCapability(“name”, “Testing Chrome Options Selenium 4”);

options.setCapability(“platformName”, “Windows 10”);

options.setCapability(“browserName”, “chrome”);

options.setCapability(“browserVersion”, “latest”);

 

Modifications in the Actions class:

Actions class is used to simulate input actions from mouse and keyboard on specific web elements (e.g., Left click, Right-click, Double click, etc.)

 

Old

New

Click on a web element

moveToElement(element).click()

click(element)

Click on element without releasing the click

moveToElement(element).clickAndHold()

clickAndHold(element)

Right click

moveToElement(element).contextClick()

contextClick(element)

 release() initially a part of org.openqa.selenium.interactions.ButtonReleaseAction class. Is now part of Actions class.

 

 

Selenium 3 vs Selenium 4