Selenium is the most used automation tool in the market. The Selenium is constantly improving and changing as per market standards. Selenium 4 released with many useful features created hope in selenium testers.  Those who were thinking that selenium has no future, the selenium 4 answered this by updating selenium with rich features, adopting functionality for modern web development testing.

There are many useful features in Selenium 4, we will look into that one by one.

  • Selenium 4 adopted the W3C standard protocol
  • Enhanced support for Selenium Grid
  • Selenium 4 IDE for record and playback
  • Relative Locators
  • Access to Chrome Developer Tool / Chrome DevTools Protocol (CDP)
  • Network Interception
  • Updated New Window/Tab Management options
  • Rich Actions class
  • Better Screenshot options
  • Improved Documentation

Selenium 4 W3C Protocol

The Protocol is very important to communicate between Webdriver and Browser, earlier this communication has made possible through JSON wire protocol, Selenium started implementing W3C protocol before Selenium 4 but with Selenium 4 the selenium fully supports W3C protocol standards. W3C Protocol makes cross-browser testing more efficient and stable. All the modern browsers support the W3C protocol. Though this is the major change with selenium 4, for users impact is less it made his job easier. Selenium provided detailed documentation on upgrading Selenium 4

Selenium IDE

Selenium IDE existed earlier as well, but later it was removed due to Firefox add-on standardization.

Selenium 4 re-introducing Selenium IDE. The Selenium IDE makes beginners' jobs easier, with Selenium IDE you can record the test and playback, No coding knowledge is required for this. The selenium IDE is provided as a browser add-on, it is available for all major browsers like Chrome, Firefox, etc.

Some of the Notable Features of Selenium 4 IDE are

  • The Selenium 4 has a complete revamp of the User Interface.
  • Multiple locator strategy, the Selenium 4 IDE records multiple locators so if one locator fails, it tries with another locator. Multiple locator strategy makes test scripts stable.
  • The test case can be re-used, just like how you do re-use of common code in utility the same way you can re-use in Selenium IDE.
  • Control Flow supported. Using the if, while, and times command you can make tests more stable and stronger.
  • Extendable, Selenium 4 IDE commands can be extended. You can define your command. It helps you to get the most out of it.
  • Debugging Support, The Selenium 4 IDE comes with debugging support, you can set a break point on IDE and run the tests, as soon as it hits the breakpoint, the execution stops and you can step over the command.
  • Control Execution Speed, Selenium 4 IDE allows you to control the execution speed, you can set execution speed fast, slow, medium, etc.
  • Logs are shown in the Log section which helps to understand and analyze the problem.
  • The project can be saved as .side extension and reuse for later.

Enhanced Support for Selenium Grid

Selenium Grid is used for scaling and distributing tests on different browsers and operating system combinations. Earlier, the Selenium Grid setup was complicated and it was time-consuming. Now, Selenium has extended its support for Docker containers, so rather than using a Virtual machine you can just set up docker and run your tests. Selenium Grid 4 is a fresh implementation.

Selenium Grid 4 comes with 3 advanced features

  • Observability: The Selenium Grid 4 observability features mainly focus on logs, metrics, traces. This helps to understand the internals of the system and debugging as well.
  • Grid End Points: The Grid endpoint feature mainly focuses on the Grid itself like availability, status,  sessions, and slots.
  • GraphQL Query Support: GraphQL Query helps you to query with existing data. This makes API queries more powerful, users can ask and get what they want.

The Selenium Grid 4 helps to utilize the DevOps concept fully.

Relative Locators

Selenium 4 provides a new feature called Relative locator, The Relative locator helps to find the element above, below, toLeftOf, toRightOf, and near.

You can simply call methods like .above, .below etc to get the relative locators.

Example : Using .above() in Selenium 4

WebElement passwordField = driver.findElement(By.id("password"));

WebElement emailAddressField = driver.findElement(with(By.tagName("input"))

.above(passwordField));

The relative locators make the test more stable and provide alternative options to locate the element.  These relative locators work according to Visual Location of other elements, that means how it elements are appeared visually on the browser considering other DOM element.

Support for Chrome Debugging Tool

The most awaited functionality is Chrome DevTools Tool Protocol. The Selenium 4 supports Chrome DevTools protocol, this functionality helps to access the Chrome Developer Tools and use them in your tests programmatically, with the Chrome DevTools Protocol one can control the browser itself as it provides the feature like device emulation, capturing logs, etc. Selenium has built the wrapper class called DevTools to support all the chrome devtools protocol functionalities. Selenium 4 also allows you to access the Chrome DevTools Protocol directly using method executeCdpCommand()

Example : Using send() command Selenium DevTools package

ChromeDriver driver = new ChromeDriver();

DevTools devTools = driver.getDevTools();

devTools.createSession();

devTools.send(Emulation.setGeolocationOverride(Optional.of(52.5043),

                                               Optional.of(13.4501),

                                               Optional.of(1)));

driver.get("https://my-location.org/");

driver.quit();

What is the difference between send() and executeCdpCommand ()?

The Selenium 4 introduced two major commands send() and executeCdpCommand(). Both are used for interacting with Dev Tools.

send(): The selenium has written a wrapper to chrome devtools protocol, internally it makes the call to chrome devtools protocol commands, org.openqa.selenium.devtools package provides this option. Selenium has done this considering the future of this feature. Selenium might consider this for other browsers as well in the future. So the wrapper send() should be your first choice when you interact with dev tools.

executeCdpCommand(): The executeCdpCommand() method is available directly from ChromiumDriver, It doesn’t use any selenium wrapper.Only if you want to bypass selenium implementation of CDP commands then you should consider this. Since you are calling The executeCdpCommand() method it requires more work while sending parameters.

Enhanced  Window/Tab Management in Selenium 4

We must admit this, the modern automation framework is still struggling to provide the feature like automating New Window/ Tab scenarios, The Selenium has gone one step advanced, and providing new window API makes it simpler. Selenium 4 provides a new API called NewWindow which creates a new tab, new window, and Once it's created, Selenium automatically switches to  New Tab/Window.

Example: Using newWindow() in Selenium 4

driver.get("https://www.google.com/");

driver.switchTo().newWindow(WindowType.TAB);

driver.navigate().to("https://rahulshettyacademy.com/#/index");

WindowType.WINDOW: Creates New Window

WindowType.TAB: Creates a new tab within the same window.

Improved Documents

Though Selenium was popular, most of us were rely on external documents and tutorials to learn and implement Selenium Tests. Selenium has improved documentation, the APIs, Commands are documented with their syntax, for example on all Selenium-supported languages.

The official website of selenium provides complete documentation of Selenium 4. The new website helps everyone, it eliminates a lot of confusion that pops up while writing test automation scripts.

Rich Actions Class

The keyboard and mouse actions are performed using the Actions class. Complicated scenarios like drag and drop, click and hold, click on an element with Key combinations are some examples of Actions class usage. Selenium 4 has made some changes to the Actions class. In earlier versions, we were performing MoveToElement action before click, clickAndWait, etc. but with Selenium 4 you can simply use Click() method of actions class and it will MoveToElement and Click

Example: Actions usage in Selenium 4

WebElement searchBtn = driver.findElement(By.linkText("Sign in"));

Actions actionProvider = new Actions(driver);

// Perform click-and-hold action on the element

actionProvider.clickAndHold(searchBtn).build().perform();

The mouse actions such as click(), clickAndHold(), contextClick(), doubleClick(), release() can be utilized in a same way mentioned above. No need to use moveToElement(onElement).clickAndHold().

Screenshot Options in Selenium 4

The selenium 4 provides various ways to take a screenshot like

Element Screenshot: Selenium 4 has introduced a new method called getScreenshotAs(), with this you can search for a particular element and take a screenshot of it.

Example: Element Screenshot in Selenium 4

WebElement element = driver.findElement(By.cssSelector("h1"));

File scrFile = element.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("./image.png"));

Full-Page Screenshot: This functionality in Selenium 4 enables one to take a full-page screenshot. The getScreenshotAs() method is used with the driver instance to take a full-page screenshot.

Example: FullScreen screenshot in Selenium 4

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("./image.png"));

Print Page option

The Print Page Option is available in Headless Chrome, Selenium framework provides PrintPage options, this prints the current page into PDF.

Example: Print current page to pdf in Selenium 4

import org.openqa.selenium.print.PrintOptions;

driver.get("https://www.selenium.dev");

printer = (PrintsPage) driver;

PrintOptions printOptions = new PrintOptions();

printOptions.setPageRanges("1-2");

Pdf pdf = printer.print(printOptions);

String content = pdf.getContent();

Network Interception.

Since Selenium 4 has enabled support for CDP, users can take advantage of this to capture performance metrics, limit the network bandwidth, block the URLs, etc. All modern automation testing framework provides this feature, The selenium is introducing now with Selenium 4. The Network Interception is an advanced feature, you can get control over all the requests. For example, if you want to block all the CSS files you can do so.

Example : Network interception in Selenium 4

ChromeDriver driver = new ChromeDriver();

DevTools devTools = driver.getDevTools();

devTools.createSession();

devTools.send(Performance.enable(Optional.empty()));

List<Metric> metricList = devTools.send(Performance.getMetrics());

driver.get("https://google.com");

The Selenium is a long-lived automation framework, it has evolved a lot. Many players in the market entered and disappeared, the Selenium 4 release is a clear message to all of its users it’s not going to disappear any time soon. The selenium team also manage to add all the major features which are very much required for Modern web development. The selenium 4 release stopped some users from switching to modern frameworks.

The Rahul Shetty Academy provides world-class training cost-effectively, The training is planned considering beginners to advanced users. The Rahul Shetty Academy helps people to get updated with the latest technology and trends. The complete learning guide for testing courses even with many advanced tools are available in Rahul Shetty Academy.

Author: Ganesh Hegde | LinkedIn

Ganesh Hegde is an experienced Quality Assurance Engineer/SDET, he has experience in various tools such as Selenium, Cypress, Protractor, Playwright, TestCafe, WebdriverIO. He is also passionate about Technical Content Writing and Blogging.


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}