WebdriverJS : driver.manage().logs().get('brow

2019-07-04 00:59发布

I have the following mocha test case, I'm trying to print the webdriver logs in the end, but its returning an empty array. The result is the same even when I pass 'browser' as argument to the logs().get(). Can someone please tell me why the logs are empty?

it('should open a url', function(done){
    var By = nemo.wd.By;
    driver.manage().logs();
    driver.get("http://www.google.com");
    driver.findElement(By.name('q')).sendKeys("webdriver");
    driver.findElement(By.name('btnG')).click()
    driver.manage().logs().get('driver').then(function(logs){
        console.log(logs);
        done();
    });
});

3条回答
家丑人穷心不美
2楼-- · 2019-07-04 01:40

It was because I didn't enable the logging option in the list of capabilities while creating the driver instance. Resolved now with these changes.

var pref = new webdriver.logging.Preferences();
pref.setLevel('browser', webdriver.logging.Level.ALL); 
pref.setLevel('driver', webdriver.logging.Level.ALL); 

var driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.firefox())
    .setLoggingPrefs(pref).build();
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-04 01:44

This also depends on browser driver being used - e.g. with chromedriver >= 2.29 the logs are sometimes 'late' - the logs().get('performance') promise resolves with empty array, but after waiting it resolves with data. This is the same with Oscar's capabilities tweaks. So, here's a workaround:

function getPerfLogs(driver) {
  let performanceLogs;

  return driver.wait(() => {
    return driver.manage().logs().get('performance')
      .then(v => {
        let nonEmpty = v.length > 0;
        if (nonEmpty) {
          performanceLogs = v;
        }
        return nonEmpty;
      })
  }, 1000)
  .then(() => {
    return performanceLogs;
  });
}

getPerfLogs(driver)
  .then(v => console.log('performance logs', v))
查看更多
戒情不戒烟
4楼-- · 2019-07-04 01:55

@Artem's answer validated the behavior I was seeing, but I found a different solution for my case, using Protractor: explicitly call browser.waitForAngular() before accessing logs:

return item.click()
    .then(function() {
        // "Protractor automatically applies this command before every WebDriver action",
        // but apparently browser.manage().logs() is not an action. Without this line
        // here, logs are sometimes not picked up until some time later.
        return browser.waitForAngular()
    })
    .then(function() { return browser.manage().logs().get('browser') })
    .then(function(logs) { ... });

https://www.protractortest.org/#/api?view=ProtractorBrowser.prototype.waitForAngular

查看更多
登录 后发表回答