性能日志

ChromeDriver 支持性能日志记录,您可以从中获取网域“时间轴”“网络”和“网页”的事件,以及指定跟踪类别的跟踪数据

启用性能日志

默认情况下,性能日志记录处于停用状态。因此,在创建新会话时 您必须启用它

DesiredCapabilities cap = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);

启用后,性能日志会收集时间轴、网络和网页事件。如需同时启用跟踪功能或自定义性能日志记录,请继续阅读。

查看使用默认选项的性能日志记录的完整示例(图片来源:Michael Klepikov)。

Angular Benchpress 也使用性能日志记录。

跟踪和自定义日志记录

如果您需要自定义性能日志记录(例如为了启用跟踪),可以使用 perfLoggingPrefs 功能(通过 ChromeOptions)。您可以通过指定一个或多个 Chrome 跟踪记录类别来启用跟踪。详细了解 Chrome 跟踪

启用跟踪后,系统会隐式停用时间轴网域。您仍然需要启用具有 loggingPrefs 功能的性能日志。

DesiredCapabilities cap = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
Map<String, Object> perfLogPrefs = new HashMap<String, Object>();
perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools"); // comma-separated trace categories
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);

您也可以使用 perfLoggingPrefs 分别启用或停用广告网络网域和网页网域。例如,您可以在跟踪时明确启用网络网域:

...

Map<String, Object> perfLogPrefs = new HashMap<String, Object>();
perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools");
perfLogPrefs.put("enableNetwork", true);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);

...

如果启用了跟踪功能,ChromeDriver 会在 Chrome 启动时启动浏览器级跟踪记录,并持续进行跟踪,直到 Chrome 关闭。跟踪记录运行时,Chrome 会在内存中缓冲跟踪记录事件,直到跟踪记录停止。

一旦跟踪缓冲区已满,系统将不再记录跟踪事件。为避免缓冲区已满(进而丢失跟踪数据),ChromeDriver 会定期停止当前跟踪,收集已缓冲的事件,并在测试期间的某些点重新开始跟踪。

收集轨迹事件可能会增加测试的开销,因此 ChromeDriver 只会在测试期间的适当时间点收集轨迹事件。目前,只有在页面导航事件以及请求任何 ChromeDriver 日志(例如性能日志)时,系统才会收集轨迹事件。缓冲区仍有可能被填满,因此 ChromeDriver 会监控受支持的 Chrome 版本(r263512 及更高版本)的缓冲区使用情况。如果缓冲区已满,ChromeDriver 会记录警告并在性能日志中添加条目。

收集日志条目

在测试中,您可以获取性能日志条目。如需了解详情,请参阅 WebDriver 日志记录文档

for (LogEntry entry : driver.manage().logs().get(LogType.PERFORMANCE)) {
  System.out.println(entry.toString());
}

每个条目都是采用以下结构的 JSON 字符串:

{
  "webview": <originating WebView ID>,
  "message": { "method": "...", "params": { ... }} // DevTools message.
}

方法值是 DevTools 事件的方法。例如,对于协议的所有版本(最高版本,包括版本 1.1,即编写此版本时的最新版本),时间轴事件中的方法均为 Timeline.eventRecorded

跟踪日志条目

从 1.1 版开始,跟踪不再包含在已发布的开发者工具协议中,因此此处提供了详细信息。

所有跟踪事件的网页视图值均为“browser”,因为事件是在浏览器范围内收集的。

跟踪事件方法有两种:

  • tracing.dataCollected:参数是字典形式的单个跟踪事件。
  • tracing.bufferUsage:参数包含一个错误键,其中会显示一条消息,指明在测试期间已填充开发者工具跟踪缓冲区。

以下是跟踪事件示例:

{
    "webview":"browser",
    "message":{
        "method":"Tracing.dataCollected",
        "params":{
            "args":{"layerTreeId":1},
            "cat":"cc,devtools",
            "name":"DrawFrame",
            "ph":"i",
            "pid":11405,
            "s":"t",
            "tid":11405,
            "ts":3846117219.0,
            "tts":1134680
        }
    }
}