The power of Chrome Devtools Protocol — Part II

Network domain

German Bisogno
Globant
4 min readOct 18, 2022

--

In the previous article, we covered the powerful features of the Tracing domain using CDP and how to implement it in a Selenium test. In this article, we will cover the Network domain and how we can capture network requests using this domain.

You can find the complete code at https://github.com/germanbisogno/cdp-utils. Let’s jump into it!

Using Network domain

Let’s show the manual example first of how to use the Network tab in Chrome. We will be recording some requests based on a Google search.

  • Open devtools (F12) and go to the Network tab
  • Start recording
  • Enter to https://www.google.com
  • Perform a search on Google
The image above is showing Network tab where you can capture requests, start recording by clicking the top left circle.

After recording some requests, we will have a result similar to the following.

It is a capture of the Network requests that happened during the search

From the toolbar, you can export the HAR file by selecting the following icon in the top toolbar.

Export HAR…

The exported file will contain a lot of useful information about the request like timing, type of request, size, and much more, that can help to determine the performance of your APIs for example, or just to analyze if you have performance bottlenecks in your application.

If you need more information about how to use the Network tab, you can check here.

How do we automate?

It can be done using the Network domain, you will be able to collect information about the requests. We will be observing a set of events that will give us the necessary information about the Network. For example:

Now, let’s implement a classNetworkwritten in Typescript where we will extend again from traceOperationsas previously we have done with the Tracingclass.

Please, take into account that npm dependencies used in the previous article will be required.

Notice in this class we have implemented the usage ofPage.enableand Network.enablethat will be required to capture network events and when we stop the trace, it comes to the utility chrome-har using harFromMessages which will be used to convert network events into a readable HAR file.

Additionally, a new method has been implemented to emulate network conditions by using theNetwork.emulateNetworkConditionscommand. It is useful if you need to emulate different network conditions like 2G, 3G, 4G, GPRS, and more types of connections. Please, find below some examples:

To use the above presets, you will need to specify which Network conditions you may want to use in your test. In this case, it is provided by the parameter to the method emulateNetworkConditions that is represented with the following interface:

Interface to represent Network Conditions parameters

Developing a test!

Next, let’s implement the test to generate our HAR file!

Finally, after executing the test, let’s import the file from the toolbar by selecting

Import HAR file …

Let’s see how it looks!

Importing the generated HAR file into the Network tab

Conclusion

We went into the Network domain and some of its different features. The Network domain allows tracking the network activities of the page. It exposes information about HTTP requests and responses, their headers, bodies, timing, and much more. Also, using this domain to simulate network conditions is great!

We will continue covering more domains in future articles.

Hope you have enjoyed this journey! See you next time!

Thanks for reading!

--

--