使用量角器

时间:2018-03-05 16:13:39

标签: javascript protractor selenium-chromedriver

我正在使用量角器进行带有角度的e2e测试,我正在拼命地获取带有标题和正文的HTTP请求日志。 我已经配置了这样的量角器:

  {

    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"),

    //...
};

在每个场景之后,我正在执行这个钩子:

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

但我得到的只是控制台日志,但没有http请求。有没有办法从量角器中获得chromedriver的那些?

以下是提及性能日志的chromedriver doc的链接:https://sites.google.com/a/chromium.org/chromedriver/logging/performance-log

1 个答案:

答案 0 :(得分:11)

您需要添加以下chromeOptions,包括perfLoggingPrefsloggingPrefs,如enter image description here

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

获取日志时,我编写的示例在每次测试后都以afterEach方法记录输出。

  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);
        }
      });
    });
  });

从日志中,您应该能够看到加载JavaScript文件,资产等时发出的任何请求。

更新回答

更新每条评论的答案。如果您使用'Network.requestWillBeSent',则可以查看POST。