DevTools protocol in Selenium 4.

Anton Smirnov
ITNEXT
Published in
4 min readMar 27, 2021

--

Photo by Samuel Bourke

One of the most popular UI automation tools is Selenium Webdriver. At the time of writing, the second beta 4 version has been released.

The release includes an API for working with DevTools via CDP (Chrome DevTools Protocol), which takes interaction with the browser to a new level.

The Chrome DevTools Protocol allows for tools to instrument, inspect, debug and profile Chromium, Chrome, and other Blink-based browsers. Many existing projects currently use the protocol. The Chrome DevTools uses this protocol and the team maintains its API.

The DevTools toolkit is divided into blocks (DOM, Debugger, Network, etc.), each of which defines a list of supported commands and generated events.

Selenium allows you to work directly with everyone, which makes it possible:

  • Work with the browser cache.
  • Intercept and modify requests.
  • Emulate network state.
  • Set geolocation.
  • Bypass security restrictions.
  • Read browser metrics.

The driver that supports working with developer tools implements the getDevTools () method, and you can execute CDP commands directly by working with the DevTools object.

Wrapper classes are provided for each command block. To send a command, just create a session and call DevTools. send (Command<X> command).

Another way is to call the executeCdpCommand () method, which is implemented by the ChromiumDriver heirs and works not with wrappers, but with raw commands. Method arguments — the name of the operation and the list of parameters.

The new features.

  • Redefining UserAgent. Before Selenium 4, UserAgent was installed only through capabilities, before creating the driver object.
    Network. setUserAgentOverride sets the new UserAgent value in Runtime mode.
  • Granting of rights. For some operations, the browser requests the user’s permission, for example, to access the clipboard or microphone. You can grant permissions using Browser.grantPermissions by passing the list of required PermissionType. For example, let’s allow the browser to capture audio.
  • Adding headers to requests. Network. setExtraHTTPHeaders allows you to add custom headers to requests.
  • Interception and modification of requests. Fetch.enable enables interception, each request will be stopped until the client calls failRequest, fulfill request, or continue request.
    You can intercept all requests and modify them by condition, or work only with the target ones by specifying the RequestPattern for this, both options are listed below. Modifying the URL, for example, redirecting /v1/users requests to the new version of the /v2/users backend.

A failed request is emulated in a similar way. For example, to test the operation of an application with a failed integration.

  • Internet connection emulation. Network.emulateNetworkConditions allows you to set connection parameters, for example, to test behavior when 3G is weak or there is no network.
  • Clearing the cache. Network.clearBrowserCache clears the browser cache.
  • Blocking resource loading. Using Network.setBlockedURLs, you can restrict resource loading by pattern. For example, block the download of .svg
  • Setting TimeZone and Geolocation Position. Emulation.setTimezoneOverride overrides the browser’s TimeZone, and Emulation.setGeolocationOverride overrides the geolocation.
  • Circumventing security restrictions. Security.setIgnoreCertificateErrors will work around problems with the SSL certificate. I note that I do not call for such actions, but show the availability of opportunities.
  • Metrics. Performance.enable enables the collection of metrics (Resources, Documents, JSHeapUsedSize, etc.), and Performance.getMetrics returns the current values.

Conclusion.

This article examines only some of the possibilities of using DevTools for writing automation scripts, the use is limited only to the imagination and a list of commands.

The API for working with developer tools makes Selenium Webdriver even more powerful, opening up the possibilities of settings, customization, emulation, traffic interception, event tracking, and metrics.

The DevTools functionality will add flexibility to your tests. DevTools will make preconditioning easier, and reporting and the causes of crashes more informative.

https://test-engineer.site/

Author Anton Smirnov

--

--

I’m a software engineer who specializes in testing and automation. My top languages are Java and Swift. My blog is https://test-engineer.tech/