MainMenu

Home Java Overview Maven Tutorials

Monday, 23 December 2024

Selenium 4 features


Features in Selenium 4:


1. W3C WebDriver Protocol

1. Selenium 4 adopts the W3C WebDriver standard, ensuring better interoperability and reliability across different browsers.
2. Direct communication with the browser eliminates the need for encoding and decoding between JSON Wire Protocol and W3C, improving performance.

2. Relative Locators:

New relative locators simplify element location based on their relationship with other elements on the page.
Methods include:
above()
below()
toLeftOf()
toRightOf()
near()

Example :
WebElement element = driver.findElement(RelativeLocator.with(By.tagName("button")).below(By.id("username")));

3. Enhanced Selenium Grid

Grid 4 offers better scalability and reliability.
Features include:
Support for Docker to deploy and manage nodes.
Improved observability with built-in logging and monitoring.
New architecture using Hub-Node communication via messaging queues.
Support for both standalone and distributed modes.

4. Improved Browser Support

Selenium 4 adds support for modern browsers and features:
Native Chromium-based Edge support.
Enhanced DevTools Protocol for Chrome and Edge.

5. DevTools Protocol Integration

Selenium 4 integrates the Chrome DevTools Protocol (CDP), enabling access to advanced browser controls.
Capabilities include:
Network interception and monitoring.
Simulating geolocation and device settings.
Capturing console logs and exceptions.

Example :

DevTools devTools = ((HasDevTools) driver).getDevTools(); devTools.createSession(); devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty())); devTools.addListener(Network.requestWillBeSent(), request -> { System.out.println("Request URL: " + request.getRequest().getUrl()); });


6. Better Documentation

Selenium 4 provides updated and detailed documentation with examples for all APIs and features.

7. New Window/Tab Management

Simplified API to open and switch between new browser tabs or windows.
1. Open a New Tab
driver.switchTo().newWindow(WindowType.TAB);
2. Open a New Window
driver.switchTo().newWindow(WindowType.WINDOW);


8. Improved Actions API

The Actions class now supports finer control and more complex gestures for interactions like:
Multi-pointer input (for mobile testing).
Multi-touch gestures

9. Deprecations and Cleanups

Older methods and features have been deprecated, streamlining the API for easier usage.
Example: DesiredCapabilities replaced with Options.

10. Enhanced Screenshots

Take screenshots of specific web elements or the entire page, improving test debugging.
Example :
WebElement element = driver.findElement(By.id("example"));
File screenshot = element.getScreenshotAs(OutputType.FILE);

11. WebDriverManager No Longer Required

Selenium 4 handles driver binaries automatically through the WebDriverManager class.