Skip to content
Selenium executing Chrome DevTools Protocol commands
Java
Branch: master
Clone or download
Latest commit 81ad9d3 Aug 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src intermediate revision - cloned devtools fragment of seleniumhq/seleni… Aug 11, 2019
.gitignore initial revision Aug 7, 2019
LICENSE
README.md intermediate revision - cloned devtools fragment of seleniumhq/seleni… Aug 11, 2019
browser_protocol.json initial revision Aug 7, 2019
pom.xml intermediate revision - cloned devtools fragment of seleniumhq/seleni… Aug 11, 2019
pom.xml.BAK intermediate revision - cloned devtools fragment of seleniumhq/seleni… Aug 11, 2019

README.md

Info

The project practices Java Selenium 4.0.x alpha release ChromiumDriver for executing the Chrome DevTools Protocol (cdp) commands (an entirely different set of API communicated to the Chrome browser family via POST requests to /session/$sessionId/goog/cdp/execute with API-specific payload) feature.

Examples

Override User Agent

One can call cdp protocol to invoke setUserAgentOverride method and dynmically modify the user-agent header during the test:

  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.chromium.ChromiumDriver;

  ChromiumDriver driver = new ChromeDriver();
  driver.get("https://www.whoishostingthis.com/tools/user-agent/");
  By locator = By.cssSelector(".user-agent");
  WebElement element = driver.findElement(locato);
  assertThat(element.getAttribute("innerText"), containsString("Mozilla"));
  Map<String, Object> params = new HashMap<String, Object>();
  params.put("userAgent", "python 2.7");
  params.put("platform", "Windows");
  driver.executeCdpCommand("Network.setUserAgentOverride", params);
  driver.navigate().refresh();
  sleep(100);

  element = driver.findElement(locator);
  assertThat(element.isDisplayed(), is(true));
  assertThat(element.getAttribute("innerText"), is("python 2.7"));

demonstrates that the user-agent is indeed changing

Cookies

The example shows alternative API to collect the cookies available to page Javascript

  Map<String, Object> result = driver.executeCdpCommand("Page.getCookies", new HashMap<String, Object>());
  ArrayList<Map<String, Object>> cookies = (ArrayList<Map<String, Object>>) result.get("cookies");
  cookies.stream().limit(100).map(o -> o.keySet()).forEach(System.err::println);

Capture Screenshot

  String result = driver.executeCdpCommand("Page.captureScreenshot", new HashMap<>());
  String data = (String) result.get("data");
  byte[] image = new (Base64()).decode(data);
  assertThat(ImageIO.read(new ByteArrayInputStream(image)).getWidth(), greaterThan(0));
  (new FileOutputStream("temp.png")).write(image);

Note: some CDP API notably Page.printToPDF are not curently implemented:

unhandled inspector error: {"code":-32000,"message":"PrintToPDF is not implemented"}(..)

Selenum release dependency

It appears that the critical dependency jar of this project, selenium-chromium-driver only available for Selenum release 4.x. The selenium-chromium-driver is shown in interactive maven central repository search page.

Downport to Selenium 3.x

The devtools and chromium subprojects of selenium client of official seleniumhq/selenium project have no dependencies and can be cloned and built locally allowing one to use CDP API with Selenium 3.x e.g. Selenium 3.13.0. This is currently attempted this way in this project. Moving away form default 4.0.0.alpha maven profiles is a work in progress.

See Also

License

This project is licensed under the terms of the MIT license.

Author

Serguei Kouzmine

You can’t perform that action at this time.