Chrome DevTools Logging Features
Logging features using CDP.
许多浏览器提供“开发者工具”(DevTools),这是与浏览器集成的一组工具,开发人员可以使用它们来调试网页应用程序并探索网页的性能。Google Chrome 的开发者工具使用一种称为 Chrome DevTools 协议(简称 “CDP”)的协议。顾名思义,该协议并非为测试设计,也没有稳定的 API,因此功能很大程度上取决于浏览器的版本。
Selenium 正在致力于实现一种基于标准的、跨浏览器的、稳定的 CDP 替代方案,称为 [WebDriver BiDi]。在对该新协议的支持完成之前,Selenium 计划在适用的地方提供对 CDP 功能的访问。
Chrome 和 Edge 提供了发送基本 CDP 命令的方法。 但对于需要双向通信的功能,这种方法无效。你需要知道在何时启用哪些域,以及域、方法和参数的确切名称和类型。
21
28
<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 dev.selenium.BaseTest; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.Cookie; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chromium.HasCdp; import java.util.HashMap; import java.util.Map; public class CdpTest extends BaseTest { @BeforeEach public void createSession() { driver = new ChromeDriver(); } @Test public void setCookie() { Map<String, Object> cookie = new HashMap<>(); cookie.put("name", "cheese"); cookie.put("value", "gouda"); cookie.put("domain", "www.selenium.dev"); cookie.put("secure", true); ((HasCdp) driver).executeCdpCommand("Network.setCookie", cookie); driver.get("https://www.selenium.dev"); Cookie cheese = driver.manage().getCookieNamed("cheese"); Assertions.assertEquals("gouda", cheese.getValue()); } }
<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/CdpTest.java#L22-L27" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
1
8
<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-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">def</span> <span style="color:#000">test_set_cookie</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">driver</span><span style="color:#000;font-weight:bold">):</span>
cookie = {'name': 'cheese', 'value': 'gouda', 'domain': 'www.selenium.dev', 'secure': True} driver.execute_cdp_cmd('Network.setCookie', cookie) driver.get('https://www.selenium.dev') cheese = driver.get_cookie(cookie['name']) assert cheese['value'] == 'gouda'
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/python/tests/bidi/cdp/test_cdp.py#L2-L7" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
13
22
<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.Collections.Generic</span><span style="color:#000;font-weight:bold">;</span>
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocs.BiDi.CDP
{
[TestClass]
public class CDPTest : BaseChromeTest
{
[TestMethod]
public void SetCookie()
{
var cookie = new Dictionary<string, object>
{
{ "name", "cheese" },
{ "value", "gouda" },
{ "domain", "www.selenium.dev" },
{ "secure", true }
};
((ChromeDriver)driver).ExecuteCdpCommand("Network.setCookie", cookie);
driver.Url = "https://www.selenium.dev";
Cookie cheese = driver.Manage().Cookies.GetCookieNamed("cheese");
Assert.AreEqual("gouda", cheese.Value);
}
}
}
<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/CDPTest.cs#L14-L21" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
8
14
<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-rb" data-lang="rb"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"># frozen_string_literal: true</span>
require 'spec_helper' RSpec.describe 'Logging' do let(:driver) { start_session } it 'sets cookie' do driver.execute_cdp('Network.setCookie', name: 'cheese', value: 'gouda', domain: 'www.selenium.dev', secure: true) driver.get('https://www.selenium.dev') cheese = driver.manage.cookie_named('cheese') expect(cheese[:value]).to eq 'gouda' end end
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/ruby/spec/bidi/cdp/cdp_spec.rb#L9-L13" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
为简化 CDP 的使用并提供对更高级功能的访问,Selenium 绑定会自动为最常见的域生成类和方法。
不过,CDP 方法和实现可能会因版本而异,因此你需要确保 Chrome 版本和 DevTools 版本相匹配。
Selenium 在任何时间点支持 Chrome 的最近三个版本,并且尽量同步发布以确保可以访问最新版本。
这种限制给一些绑定带来了额外的挑战,动态生成的 CDP 支持要求用户定期更新代码,以引用正确版本的 CDP。
在某些情况下,已创建了一个理想化的实现,它应该适用于任何版本的 CDP,而无需用户更改代码,但这并非总是可用。
关于如何在 Selenium 测试中使用 CDP 的示例可以在以下页面找到,但我们想提到一些常被引用但实际价值有限的例子:
Logging features using CDP.
Network features using CDP.
Script features using CDP.
Learn more or view the full list of sponsors.