The power of Chrome Devtools Protocol — Part III

German Bisogno
Globant
Published in
2 min readOct 26, 2022

--

Part III — Runtime domain

In the previous article, we covered the powerful features of the Network domain using CDP and how to implement it in a Selenium test. In this article, we will cover the Runtime domain and how we can capture console messages like errors, warnings, and logs using this domain.

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

Using Runtime domain

We will use the Runtime domain to obtain data from the console during our test execution. The Runtime domain exposes JavaScript runtime using remote evaluation and mirror objects, and you can have access to the console through this domain.

  • Open devtools (F12) and go to the Console tab
  • Write a console log message console.log('Hello world');
  • The message shows up!
Printing log messages in the console

You can log messages to the console, as your app will probably do too. It can also be used to log warnings and errors using console.warning or console.error. (If you need more information about how to use the Console tab, you can check here.)

No recording is available from developer tools for the console, so we will jump directly into the code to implement this class.

How do we automate?

Let’s implement a classRuntimeto listen to console events. Please consider that npm dependencies used in the previous article will be required.

We have used in this caseRuntime.enableto enable the Runtime domain and Runtime.consoleAPICalledto listen to console events. Optionally, as it has been implemented in other classes, console events can be saved into a file in JSON format or just return this information in your test to perform assertions.

Developing a test!

Let’s capture browser console messages in the following test. We will generate messages like errors, warnings, or logs to test this API.

As you will observe in the code, we are intentionally making it produce errors, warnings, and logs in the console by using driver.executeScript to test this domain. Note that in the real-world scenario for your app under testing, it is possible to capture this information similarly.

Conclusion

We went into the Runtime domain and some of its different features. This domain will help capture console messages during the Test Automation.

We will continue covering more domains in the following articles.

I hope you have enjoyed this journey! See you next time!

Thanks for reading!

--

--