Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-client remote debugging using ChromeDriver and Chrome DevTools protocol

So with Chrome 63 there is now support for Multi-client remote debugging (https://developers.google.com/web/updates/2017/10/devtools-release-notes)

What I want to achieve is use the Chrome DevTools Protocol HeapProfiler with some selenium tests. I'm running version 64 Chrome dev channel and ChromeDriver 2.33.

ChromeOptions options = newChromeOptions();
options.addArguments("--remote-debugging-port=9222");
WebDriver driver = new ChromeDriver(options);
... selenium stuff

A new chrome window will open and hang until it times out. I can confirm that the chrome window opened is chrome 64 by going to help > about google chrome to check the version. I get this error which appears to be the the webdriver losing connection.

Exception in thread "main" org.openqa.selenium.WebDriverException: chrome not 
reachable

The DevTools Protocol is working because I am able to open http://localhost:9222 in another chrome window and see debugging interface.

Has anyone been able to get these two things to work together?

Thanks :)

like image 536
RapidStar Avatar asked Nov 04 '17 07:11

RapidStar


People also ask

What is Chrome DevTools protocol?

The Chrome DevTools Protocol provides APIs to instrument, inspect, debug, and profile Chromium-based browsers. The Chrome DevTools Protocol is the foundation for the Microsoft Edge DevTools. Use the Chrome DevTools Protocol for features that aren't implemented in the WebView2 platform.

What are the two important methods to implement Chrome DevTools protocol?

Selenium 4 introduces the new ChromiumDriver class, which includes two methods to access Chrome DevTools: getDevTools() and executeCdpCommand(). The getDevTools() method returns the new DevTools object which allows you to send() the built-in Selenium commands for CDP.

What is Cdpsession?

A customer data platform (CDP) is designed for marketing. It collects and unifies first-party customer data from multiple sources to build a single, coherent, complete view of each customer. It then makes that data available to marketers to create targeted and personalized marketing campaigns.


2 Answers

Here the catch was that if you pass the "remote-debugging-port" switch then chromedriver has a bug where it still internally assigns a randon port and keep trying to connect to it rather than connecting to 9222 port.

options.addArguments("--remote-debugging-port=9222");

We can solve this by skipping this command switch and let chrome decides this random port and extract this port number from chromedriver logs.

I made it work and here I have blogged it in detail.

https://medium.com/@sahajamit/selenium-chrome-dev-tools-makes-a-perfect-browser-automation-recipe-c35c7f6a2360

like image 194
Amit Rawat Avatar answered Sep 28 '22 01:09

Amit Rawat


Selenium 4 release will have a user friendly API for Chrome DevTools protocol. I just finished implementing Network and Performance domains for the Selenium Java client. https://github.com/SeleniumHQ/selenium/pull/7212

In addition, there is a generic API for all domains in Java client that was merged a while ago. All those new features will be released probably in the next Alpha release.

This is a nice article on how to use Log: https://codoid.com/selenium-4-chrome-devtools-log-entry-listeners/

like image 40
Adi Ohana Avatar answered Sep 28 '22 01:09

Adi Ohana