Selenium Change log 4.9.0: Enhanced Error Reporting, Shadow DOM Support, and More!
A.N.M Zakaria Shahed
ISTQB Certified CTFL & Agile tester | Test Automation | Team Lead | Ex BJIT
The latest version of Selenium, version 4.9.0, was released on April 28, 2023. This version includes several new features, improvements, and bug fixes. Some of the new features include:
Support for the latest versions of Firefox, Chrome, Edge, and Safari
One of the key updates in Selenium 4.9.0 is support for the latest versions of popular web browsers, including Firefox, Chrome, Edge, and Safari. Here are some details on how this feature works:
In addition, Selenium 4.9.0 also includes updates to the WebDriver API that ensure compatibility with the latest browser versions.
WebDriver driver = new FirefoxDriver();
driver.get("https://www.example.com");
In this example, we create a new FirefoxDriver instance, which uses the latest version of the Firefox browser. We then use the get() method to navigate to the https://www.example.com URL and test the web application.
Overall, support for the latest browser versions in Selenium 4.9.0 ensures that users can effectively test modern web applications and take advantage of the latest browser-features and security updates.
A new W3C WebDriver API implementation for Firefox and Safari.
The new W3C WebDriver API implementation for Firefox and Safari in Selenium 4.9.0 provides better compatibility and performance compared to the previous implementation. It conforms to the WebDriver standard developed by the World Wide Web Consortium (W3C), which is the industry standard for web automation.
Here are some details on how the new W3C WebDriver API implementation works:
Here's an example of how to create a Firefox browser instance using the new W3C WebDriver API:
FirefoxOptions options = new FirefoxOptions();
options.setCapability("w3c", true);
WebDriver driver = new FirefoxDriver(options);
Note the setCapability("w3c", true) line, which enables the new W3C WebDriver API implementation.
Similarly, here's an example of how to create a Safari browser instance using the new W3C WebDriver API:
SafariOptions options = new SafariOptions();
options.setCapability("w3c", true);
WebDriver driver = new SafariDriver(options);
Again, note the setCapability("w3c", true) line to enable the new implementation.
Overall, the new W3C WebDriver API implementation in Selenium 4.9.0 provides several benefits for web automation, including improved compatibility, performance, and error handling.
Improved performance and stability.
One of the major improvements in Selenium 4.9.0 is the improved performance and stability of the framework. Here are some details on how this improvement was achieved:
Here's an example of how to use the new dynamic wait feature:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));
In this example, the WebDriverWait object waits up to 10 seconds for the element with id myElement to become visible. If the element does not become visible within that time, a TimeoutException is thrown.
Overall, the improved performance and stability in Selenium 4.9.0 make it a more reliable and efficient framework for web automation.
Support for automatic updates of GeckoDriver and ChromeDriver.
One of the new features in Selenium 4.9.0 is the support for automatic updates of GeckoDriver and ChromeDriver. Here are some details on how this feature works:
Here's an example of how to enable automatic updates in Selenium 4.9.0:
ChromeOptions options = new ChromeOptions();
options.setCapability("enableAutoUpdate", true);
WebDriver driver = new ChromeDriver(options);
In this example, the enableAutoUpdate capability is set to true, which enables automatic updates of ChromeDriver whenever a new WebDriver session is started.
Similarly, here's an example of how to enable automatic updates for GeckoDriver:
FirefoxOptions options = new FirefoxOptions();
options.setCapability("enableAutoUpdate", true);
WebDriver driver = new FirefoxDriver(options);
Overall, the support for automatic updates of GeckoDriver and ChromeDriver in Selenium 4.9.0 ensures that you always have the latest and most compatible driver version installed, which improves the overall stability and performance of your tests.
Support for shadow DOM elements.
One of the new features in Selenium 4.9.0 is the support for shadow DOM elements. Here are some details on how this feature works:
WebElement shadowHost = driver.findElement(By.tagName("my-component"));
WebElement shadowRoot = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowHost);
WebElement shadowElement = shadowRoot.findElement(By.cssSelector(".shadow-class")); shadowElement.click();
In this example, we first locate the web component with the tag name my-component, which has a shadow DOM containing an element with the CSS class shadow-class. We then use the shadowRoot() method to access the shadow root of the web component, and locate the shadow DOM element using the findElement() method.
Overall, the support for shadow DOM elements in Selenium 4.9.0 allows you to interact with web components more easily and effectively, which is particularly useful when working with modern web applications that heavily rely on web components.
Enhanced error reporting.
One of the new features in Selenium 4.9.0 is enhanced error reporting. Here are some details on how this feature works:
In addition, Selenium 4.9.0 also provides improved stack traces that include more detailed information about where the error occurred, which can help you pinpoint the exact location of the error in your test code.
try {
// Perform some test actions
} catch (Exception e)
{
// Log the error message and stack trace System.out.println("Error message: " + e.getMessage());
System.out.println("Stack trace: "); for (StackTraceElement element : e.getStackTrace())
{
System.out.println("\t" + element.toString());
}
// Log additional metadata System.out.println("Browser: " + ((RemoteWebDriver)driver).getCapabilities().getBrowserName());
System.out.println("Browser version: " + ((RemoteWebDriver)driver).getCapabilities().getVersion());
System.out.println("Operating system: " + System.getProperty("os.name")); }
In this example, we catch any exceptions that occur during the test execution and log the error message, stack trace, and additional metadata such as the browser version and operating system.
Overall, the enhanced error reporting feature in Selenium 4.9.0 provides more detailed and informative error messages, which can help you quickly identify and fix issues in your tests.