最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get HTTP requests (performance logs) from chromedriver with protractor - Stack Overflow

programmeradmin6浏览0评论

I'm using protractor for my e2e tests with angular and I'm trying desperately to get HTTP requests logs with headers and body. I've configured protractor like this:

  {

    useAllAngular2AppRoots: true,
    ignoreUncaughtExceptions: true,

    maxSessions: 1,
    multiCapabilities: [
        {
            'name': 'desktop',
            'browserName': 'chrome',
            loggingPrefs: {"driver": "ALL", "browser": "ALL", 'performance': 'ALL'},
            chromeOptions: {
                binary: process.env.CHROME_BIN,
                args: ["--headless", "--disable-gpu", "--no-sandbox"],
                perfLoggingPrefs: {
                    'traceCategories': 'blink.console,disabled-by-default-devtools.timeline'
                }
            }
        }
    ],

    framework: "custom",
    frameworkPath: require.resolve("protractor-cucumber-framework"),

    //...
};

After each scenario, I'm executing this hook:

browser.manage().logs().get("browser").then(logs => 
  //...
)

But all I get are console logs but no http requests. Is there any way to get those from chromedriver within protractor?

Here is a link to chromedriver doc mentioning performance logs: /chromedriver/logging/performance-log

I'm using protractor for my e2e tests with angular and I'm trying desperately to get HTTP requests logs with headers and body. I've configured protractor like this:

  {

    useAllAngular2AppRoots: true,
    ignoreUncaughtExceptions: true,

    maxSessions: 1,
    multiCapabilities: [
        {
            'name': 'desktop',
            'browserName': 'chrome',
            loggingPrefs: {"driver": "ALL", "browser": "ALL", 'performance': 'ALL'},
            chromeOptions: {
                binary: process.env.CHROME_BIN,
                args: ["--headless", "--disable-gpu", "--no-sandbox"],
                perfLoggingPrefs: {
                    'traceCategories': 'blink.console,disabled-by-default-devtools.timeline'
                }
            }
        }
    ],

    framework: "custom",
    frameworkPath: require.resolve("protractor-cucumber-framework"),

    //...
};

After each scenario, I'm executing this hook:

browser.manage().logs().get("browser").then(logs => 
  //...
)

But all I get are console logs but no http requests. Is there any way to get those from chromedriver within protractor?

Here is a link to chromedriver doc mentioning performance logs: https://sites.google./a/chromium/chromedriver/logging/performance-log

Share Improve this question edited Mar 5, 2018 at 16:31 cartman asked Mar 5, 2018 at 16:13 cartmancartman 1901 gold badge3 silver badges18 bronze badges 3
  • I believe you have to use a proxy, like browsermob proxy – Gunderson Commented Mar 5, 2018 at 16:19
  • 1 The chromedriver documentation tells this: ChromeDriver supports performance logging, from which you can get events of domains "Timeline", "Network", and "Page", as well as trace data for specified trace categories. I've only seen Java examples but no js ones. Is there anyway to make this work with js/ts? – cartman Commented Mar 5, 2018 at 16:22
  • Oh did not know this, cool. Glad you found an answer – Gunderson Commented Mar 6, 2018 at 17:00
Add a ment  | 

1 Answer 1

Reset to default 19

You will need to add the following chromeOptions including perfLoggingPrefs and loggingPrefs as shown in https://github./angular/protractor-cookbook/blob/master/protractor-javascript/example-network/conf.js

capabilities: {
    'browserName': 'chrome',
    'chromeOptions': {
      'perfLoggingPrefs': {
        'enableNetwork': true,
        'enablePage': false,
        'enableTimeline': false
      }
    },
    loggingPrefs: {
      performance: 'ALL',
      browser: 'ALL'
    }
  },

When getting the logs, the example I wrote has the logging in an afterEach method to output after each test.

  afterEach(() => {
    browser.manage().logs().get('performance').then((browserLogs) => {
      browserLogs.forEach((browserLog) => {
        var message = JSON.parse(browserLog.message).message;
        if (message.method == 'Network.responseReceived') {
          console.log(message);
        }
      });
    });
  });

From the logs you should be able to see any get requests made when loading javascript files, assets, etc.

Updated answer

Updating answer per ment. If you use 'Network.requestWillBeSent', you can view POSTs.

发布评论

评论列表(0)

  1. 暂无评论