Skip to content

Supported Selenium 4 features.

aqualityAutomation edited this page Mar 17, 2023 · 12 revisions

One of the main innovations of Selenium 4 is support for the Chrome DevTools Protocol (CDP), which allows to simulate the work of the browser Developer Tools (for example, slowing down the Internet connection, getting logs from the browser console, etc.). These tools can greatly simplify test development and allow you to test previously inaccessible or hard-to-reach functionality of a web application. In Aqality Selenium, Selenium 4 DevTools functionality is implemented in the following classes: DevToolsHandling, EmulationHandling, JavaScriptHandling, NetworkHandling.

CDP Emulation Domain

Common emulation commands

The test class EmulationTests demonstrates several common emulation commands that are not related by the common functionality.

browserCanEmulateTest

The browserCanEmulateTest test demonstrates the canEmulate() method, which checks whether the current browser supports device emulation. image

setScriptExecutionDisabledAndEnableAgainTest

The setScriptExecutionDisabledAndEnableAgainTest demonstrates the ability to disable and enable browser-side javascript execution using the following methods:

void setScriptExecutionDisabled(boolean value) - enables (value = false) and disables (value = true) javascript execution in the browser; void setScriptExecutionDisabled() - disables javascript execution in the browser. This test uses a web page containing buttons that trigger various Alerts using javascscript. After calling the setScriptExecutionDisabled() method, Alert will not appear when the button is clicked (a NoAlertPresentException is thrown). Next, the JS execution is activated using setScriptExecutionDisabled(true) and now Alert will be called when the button is clicked. image

setEmulatedMediaTest

Using DevTools, it is possible to override the mediaType property of the device that CSS media queries are oriented to when generating styles (one style for PCs, another for smartphones, third for TVs, etc.). Aquality Selenium works with this property using the following methods:

void setEmulatedMedia(Optional media, Optional<Map<String, String>> mediaFeatures) - sets the media type and properties to be emulated (such as screen sizes, aspect ratio, etc.). Empty media disables mediaType override; void setEmulatedMedia(String media, Map<String, String> mediaFeatures) - the method is similar to the previous one, only without the use of Optional; void setEmulatedMedia(Map<String, Object> params) - sets the mediaType emulation through parameters that are directly used in the DevTools command for the Emulation.setEmulatedMedia method; void disableEmulatedMediaOverride() - disables media type override. The setEmulatedMediaTest test demonstrates media type overriding. image

Geolocation emulation

In Aquality Selenium, arbitrary geolocation is emulated using the following methods:

void setGeolocationOverride(Optional latitude, Optional longitude, Optional accuracy) - sets the current latitude, longitude, and geolocation accuracy. Omitting any of the parameters emulates position unavailable; void setGeolocationOverride(double latitude, double longitude, double accuracy) - has almost the same functionality as the previous method, the only difference is the use of primitives as parameters; void setGeolocationOverride(double latitude, double longitude) - a wrapper over the previous method, setting accuracy = 1. That is 100% geolocation accuracy. void clearGeolocationOverride() - resets the overridden geolocation set by the setGeolocationOverride() method. Geolocation override is demonstrated in the OverrideGeolocationTest class.

image

In the overrideGeolocationTest test, using the setGeolocationOverride() method, a new geolocation is set (LAT_FOR_OVERRIDE, LNG_FOR_OVERRIDE coordinates) and checks that the coordinates have been changed. Next, the geolocation is reset using the clearGeolocationOverride() method to the default value and it is checked that the coordinate values have returned to their original values.

Device Emulation

Using the DevTools commands, it is possible to emulate various devices by overriding the screen size (width, height), device type (desktop or mobile) and orientation (portrait or landscape). In Aquality Selenium, work with this functionality is carried out using the following methods:

void setDeviceMetricsOverride(Integer width, Integer height, Number deviceScaleFactor, Boolean mobile, Optional screenOrientationType, Optional screenOrientationAngle) - sets the width, height, scale, device type, orientation type, and angle at which the device is located; void setDeviceMetricsOverride(Integer width, Integer height, Number deviceScaleFactor, Boolean mobile) - overloaded version of the previous method with default values for screenOrientationType and screenOrientationAngle; void setDeviceMetricsOverride(Map<String, Object> params) - in this method, the device is configured using the Map<String, Object> params object, where the key is the name of the parameter (for example, the screen width), the value is the value of this parameter. These parameters are directly used in DevTools method Emulation.setDeviceMetricsOverride; void clearDeviceMetricsOverride() - resets overridden device metrics. The DeviceEmulationTest test class demonstrates how these methods work. image