Chrome DevTools Script Features

Script features using CDP.

While Selenium 4 provides direct access to the Chrome DevTools Protocol, these methods will eventually be removed when WebDriver BiDi implemented.

Script Pinning

31
35
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">package</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">dev.selenium.bidi.cdp</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline">

import static org.openqa.selenium.devtools.events.CdpEventTypes.domMutation; import dev.selenium.BaseTest; import java.time.Duration; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.logging.HasLogEvents; import org.openqa.selenium.support.ui.WebDriverWait; public class ScriptTest extends BaseTest { @BeforeEach public void createSession() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void pinScript() { driver.get("https://www.selenium.dev/selenium/web/xhtmlTest.html"); WebElement element = driver.findElement(By.id("id1")); ScriptKey key = ((JavascriptExecutor) driver).pin("return arguments;"); List<Object> arguments = (List<Object>) ((JavascriptExecutor) driver).executeScript(key, 1, true, element); Assertions.assertEquals(List.of(1L, true, element), arguments); } @Test public void mutatedElements() { driver.get("https://www.selenium.dev/selenium/web/dynamic.html"); CopyOnWriteArrayList<WebElement> mutations = new CopyOnWriteArrayList<>(); ((HasLogEvents) driver).onLogEvent(domMutation(e -> mutations.add(e.getElement()))); driver.findElement(By.id("reveal")).click(); wait.until(_d -> !mutations.isEmpty()); Assertions.assertEquals(mutations.get(0), driver.findElement(By.id("revealed"))); } }

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/java/src/test/java/dev/selenium/bidi/cdp/ScriptTest.java#L32-L34" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>
20
23
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cs" data-lang="cs"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">using</span> <span style="color:#000">System</span><span style="color:#000;font-weight:bold">;</span>

using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.IdentityModel.Tokens; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Support.UI; namespace SeleniumDocs.BiDi.CDP { [TestClass] public class ScriptTest : BaseChromeTest { [TestMethod] public async Task PinScript() { driver.Url = "https://www.selenium.dev/selenium/web/xhtmlTest.html"; var element = driver.FindElement(By.Id("id1")); var key = await new JavaScriptEngine(driver).PinScript("return arguments;"); var arguments = ((WebDriver)driver).ExecuteScript(key, 1, true, element); var expected = new List<object> { 1L, true, element }; CollectionAssert.AreEqual(expected, (ICollection)arguments); } [TestMethod] public async Task MutatedElements() { driver.Url = "https://www.selenium.dev/selenium/web/dynamic.html"; var mutations = new List<IWebElement>(); using IJavaScriptEngine monitor = new JavaScriptEngine(driver); monitor.DomMutated += (, e) => { var locator = By.CssSelector($"*[data-__webdriver_id='{e.AttributeData.TargetId}']"); mutations.Add(driver.FindElement(locator)); }; await monitor.StartEventMonitoring(); await monitor.EnableDomMutationMonitoring(); driver.FindElement(By.Id("reveal")).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(
=> !mutations.IsNullOrEmpty()); await monitor.DisableDomMutationMonitoring(); monitor.StopEventMonitoring(); var revealed = driver.FindElement(By.Id("revealed")); Assert.AreEqual(revealed, mutations[0]); } } }

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/dotnet/SeleniumDocs/BiDi/CDP/ScriptTest.cs#L21-L22" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>