量角器测试随机失败 - 屏幕截图错误:WebDriverError:chrome无法访问

时间:2017-12-13 06:48:03

标签: webdriver protractor google-chrome-headless

我在Ubuntu 16.04中对chrome headless进行了量角器测试。测试运行正常超过3个月,但突然测试开始失败,出现以下错误 -

npm run e2e console - Chrome not reachable error from the very beginning of the test

Chrome not reachable error after successfully running few tests

Protractor configuration - 
    // Protractor configuration file, see link for more information
    // 


  https://github.com/angular/protractor/blob/master/docs/referenceConf.js

/*global jasmine */

var jasmineReporters = require('jasmine-reporters');

exports.config = {
  allScriptsTimeout: 60000,
  baseUrl: 'http://localhost:4200/',
  suites: {
    V3_Sanity: [
      './e2e/Flows/Sanity/**/analytics_dashboard.e2e-spec.ts',
      './e2e/Flows/Sanity/**/executive_dashboard.e2e-spec.ts',
      './e2e/Flows/Sanity/**/my_views.e2e-spec.ts',
      './e2e/Flows/Sanity/**/question_analysis.e2e-spec.ts',
      './e2e/Flows/Sanity/**/responses.e2e-spec.ts',
      './e2e/Flows/Sanity/**/login_page.e2e-spec.ts'
    ],
    V3_e2e: [
      './e2e/Flows/E2E/**/flow1.e2e-spec.ts',
      './e2e/Flows/E2E/**/flow2.e2e-spec.ts',
      './e2e/Flows/E2E/**/flow3.e2e-spec.ts',
      './e2e/Flows/E2E/**/flow4.e2e-spec.ts',
      './e2e/Flows/E2E/**/flow5.e2e-spec.ts',
      './e2e/Flows/E2E/**/flow6.e2e-spec.ts'
    ],
    QB_Sanity: [
      './e2e/Flows/QB Sanity/**/question_config.e2e-spec.ts',
      './e2e/Flows/QB Sanity/**/right_panel.e2e-spec.ts',
      './e2e/Flows/QB Sanity/**/question_types.e2e-spec.ts'
    ],
    CT_Sanity: [
      './e2e/Flows/Contingency Table Sanity/**/CT_load_test.e2e-spec.ts'
    ],
    Response_Filter: [
      './e2e/Flows/ResponsesFilter/responses_filter.e2e-spec.ts'
    ],
    DropOff: [
      './e2e/Flows/Response DropOff/response_drop_off.e2e-spec.ts'
    ],
    Trend_Notification: [
      './e2e/Flows/Trend Based Notification/trend_sanity.e2e-spec.ts'
    ],
    Response_Filter: [
      './e2e/Flows/ResponsesFilter/responses_filter_analytics.e2e-spec.ts',
      './e2e/Flows/ResponsesFilter/responses_filter_responses.e2e-spec.ts'
    ]
  },
  capabilities: {
    browserName: 'chrome',
    chromeOptions: {
      args: ["--headless", "--disable-gpu", "--window-size=1440x900", "--no-sandbox"]
    }
  },
  directConnect: true,
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 60000,
    print: function () { }
  },
  useAllAngular2AppRoots: true,

  beforeLaunch: function () {
    require('ts-node').register({
      project: 'e2e'
    });
  },

  onPrepare: function () {
    browser.manage().window().maximize();
    require('ts-node').register({ project: 'e2e' });
    var fs = require('fs-extra');

    fs.emptyDir('./e2e/target/screenshots/', function (err) {
      console.log(err);
    });

    jasmine.getEnv().addReporter({
      specDone: function (result) {
        if (result.status == 'failed') {
          browser.getCapabilities().then(function (caps) {
            var browserName = caps.get('browserName');

            browser.takeScreenshot().then(function (png) {
              console.log("taken")
              var stream = fs.createWriteStream('e2e/target/screenshots/' + browserName + '-' + result.fullName + '.png');
              stream.write(new Buffer(png, 'base64'));
              stream.end();
            }, (err) => {
              console.log("screenshot error : " + err)
            });
          });
        }
      }
    });

    // jasmine.getEnv().addReporter(reporter);
    jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
      consolidateAll: true,
      savePath: './e2e/target/screenshots',
      filePrefix: 'xmlresults'
    }));
  },

  afterLaunch: function (exitCode) {
  },

  //HTMLReport called once tests are finished
  onComplete: function () {

    //save browser logs
    browser.manage().logs()
      .get('browser').then(function (browserLog) {
        console.log('log :' +
          require('util').inspect(browserLog));
      });

    var browserName, browserVersion;
    var capsPromise = browser.getCapabilities();

    capsPromise.then(function (caps) {
      browserName = caps.get('browserName');
      browserVersion = caps.get('version');

      var HTMLReport = require('protractor-html-reporter');

      testConfig = {
        reportTitle: 'Test Execution Report',
        outputPath: 'e2e/target/screenshots',
        screenshotPath: '.',
        testBrowser: browserName,
        browserVersion: browserVersion,
        modifiedSuiteName: false,
        screenshotsOnlyOnFailure: true
      };
      new HTMLReport().from('e2e/target/screenshots/xmlresults.xml', testConfig);
    });
  }
};

随机地我的意思是,有时整个测试运行成功,但有时它会在测试的最初阶段或运行几个测试套件后失败,并显示错误消息“屏幕截图错误:WebDriverError:chrome无法访问”。

我在我的云ubuntu 16.04服务器上遇到了这个问题。直到最近才正常运行,但突然之间它已经开始失败了。

需要注意的另一点是,它可以成功运行少量的测试套装。这些测试在我当地的Ubuntu 16.04机器上正常运行,并且在无头运行时从未遇到过这个问题。

我也尝试重新安装chrome,但它没有帮助。

我也检查了内存使用情况,但是运行测试时它有足够的可用内存。

我提到了其他stackoverflow问题,但它们都与量角器配置问题有关。这对我来说似乎很好。

1 个答案:

答案 0 :(得分:0)

我在Ubuntu上遇到了同样的问题,发现我必须做两件事:

1)在PATH和webdriver env varible中包含ChromeDriver的路径:

export webdriver.chrome.driver="/usr/bin/chromedriver"
export PATH=$webdriver.chrome.driver:$PATH

2)在ChromeOptions中添加Chrome浏览器二进制文件(实际浏览器,而不是驱动程序)文件的路径。无头跑时需要这样做。

capabilities: {
  'browserName': 'chrome',
  "chromeOptions": {
    binary: '/path/to/chrome/binary',
    args: ["--headless", "--disable-gpu", "--window-size=1440x900", "--no-sandbox"]
  }   
}

在我的Ubuntu上,二进制路径是/ usr / bin / google-chrome(在通过.deb包安装它之后)

看看这对你有帮助。