如何从Chrome驱动程序性能日志中计算一个请求的总时间?

时间:2019-05-02 17:10:21

标签: java selenium selenium-webdriver webdriver google-chrome-devtools

我正在研究硒,以跟踪网站的网络流量。

我关注了两个链接: 1. Using Selenium how to get network request 2. http://chromedriver.chromium.org/logging/performance-log

我在配置后得到了日志数据。 您可以参考第2点获取示例数据: 这就是我得到的

[2015-03-21T16:50:20 + 0400] [INFO] {“ message”:{“ method”:“ Network.responseReceived”,“ params”:{“ frameId”:“ 28480.1”,“ loaderId “:” 28480.2“,” requestId“:” 28480.1“,”响应“:{” connectionId“:0,” connectionReused“:false,” encodedDataLength“:-1,” fromDiskCache“:false,” fromServiceWorker“:false, “ headers”:{“ Access-Control-Allow-Origin”:“ *”,“ Content-Type”:“ text / plain; charset = US-ASCII”},“ mimeType”:“ text / plain”,“ protocol “:”数据“,”状态“:200,” statusText“:” OK“,” url“:”数据:,“},”时间戳“:1426942217.5344,” type“:” Other“}},” webview“ :“ C359224A-06E5-42B6-8D1B-52687733920A”} [2015-03-21T16:50:20 + 0400] [INFO] {“ message”:{“ method”:“ Network.loadingFinished”,“ params”:{“ encodedDataLength”:0,“ requestId”:“ 28480.1” ,“时间戳”:1426942217.5344}},“网页视图”:“ C359224A-06E5-42B6-8D1B-52687733920A”} [2015-03-21T16:50:20 + 0400] [INFO] {“ message”:{“ method”:“ Page.frameNavigated”,“ params”:{“ frame”:{“ id”:“ 28480.1”, “ loaderId”:“ 28480.2”,“ mimeType”:“ text / plain”,“ securityOrigin”:“://”,“ url”:“ data :,”}}},“ webview”:“ C359224A-06E5- 42B6-8D1B-52687733920A“} [2015-03-21T16:50:21 + 0400] [INFO] {“ message”:{“ method”:“ Page.loadEventFired”,“ params”:{“ timestamp”:1426942220.99924}},“ webview”:“ C359224A-06E5-42B6-8D1B-52687733920A“} [2015-03-21T16:50:21 + 0400] [INFO] {“ message”:{“ method”:“ Page.frameStoppedLoading”,“ params”:{“ frameId”:“ 28480.1”}},“ webview” :“ C359224A-06E5-42B6-8D1B-52687733920A”} [2015-03-21T16:50:21 + 0400] [INFO] {“ message”:{“ method”:“ Page.domContentEventFired”,“ params”:{“ timestamp”:1426942220.99927}},“ webview”:“ C359224A-06E5-42B6-8D1B-52687733920A“} ....

但是我不知道如何计算一个请求xhr请求的时间?

我已经搜索过,但是现在好运...

1 个答案:

答案 0 :(得分:0)

启用PERFORMANCE日志后,可以使用以下命令迭代日志条目:

List<LogEntry> entries = this.driver.manage().logs().get(LogType.PERFORMANCE).getAll();

然后,您可以获得具有以下值的json:

private String url;
private Boolean timing;
private Long connectEnd;
private Long connectStart;
private Long dnsEnd;
private Long dnsStart;
private Long receiveHeadersEnd;
private Long sslStart;
private Long sslEnd;
private Long proxyStart;
private Long proxyEnd;
private Long sendStart;
private Long sendEnd;
private String mimeType;
private String requestStatus;
private String method;

要获取单个请求的总时间,可以使用:

sendStart-sendEnd

创建Chrome驱动程序:

    DesiredCapabilities capabilities = new DesiredCapabilities();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
    logPrefs.enable(LogType.DRIVER, Level.ALL);
    capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

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

    WebDriver driver = new ChromeDriver(capabilities);