CircleCI上的Intermittent Selenium ECONNREFUSED错误

时间:2017-04-26 14:41:04

标签: selenium selenium-webdriver circleci

我们的CircleCI测试使用selenium,通过selenium-webdriver通过PhantomJS运行UI测试。测试在我们的流浪者环境中100%的时间在本地工作,但在CircleCI上失败大约1次,因为ECONNREFUSED错误如下:

   Error: ECONNREFUSED connect ECONNREFUSED 10.0.4.1:59525
      at ClientRequest.<anonymous> (node_modules/selenium-webdriver/http/index.js:238:15)
      at Socket.socketErrorListener (_http_client.js:310:9)
      at emitErrorNT (net.js:1278:8)
      at _combinedTickCallback (internal/process/next_tick.js:74:11)
      at process._tickCallback (internal/process/next_tick.js:98:9)
  From: Task: WebDriver.navigate().to(http://127.0.0.1:8080/src/public/login.php?t=ur&ign=1&ut=ad)
      at thenableWebDriverProxy.schedule (node_modules/selenium-webdriver/lib/webdriver.js:816:17)
      at Navigation.to (node_modules/selenium-webdriver/lib/webdriver.js:1140:25)
      at thenableWebDriverProxy.get (node_modules/selenium-webdriver/lib/webdriver.js:997:28)
      at User.logIn (testJs/ui/utils/user.js:9:16)
      at Host.logInAsHost (testJs/ui/utils/host.js:13:14)
      at Preferences.disableRevenueGeneration (testJs/ui/utils/preferences.js:57:14)
      at Context.<anonymous> (testJs/ui/tests/preferences.js:13:22)

这些错误在我们的测试中随机发生,并且我们的测试中没有任何特定位置触发它们。据我所知,这个问题发生在selenium / selenium-webdriver端,因为Web服务器保持正常运行并且不会产生任何错误。

我已经尝试了以下所有内容,但没有一个有效:

  1. 升级到最新的selenium-webdriver(3.4.0)
  2. 升级到更新版本的nodejs(6.9.2)
  3. 使用其他网络服务器
  4. 升级至最新版本的PhantomJS(1.9.7-15)
  5. 根据使用export DBUS_SESSION_BUS_ADDRESS尝试 https://github.com/SeleniumHQ/docker-selenium/issues/87
  6. 在node_modules / selenium-webdriver / http / index.js中,我通过重用ECONNRESET的重试来修改代码以重试ECONNREFUSED错误,即if (e.code === 'ECONNRESET') {变为if (e.code === 'ECONNRESET' || e.code === 'ECONNREFUSED') {。这不起作用,因为selenium-webdriver只是无限期重试,从中我意识到问题似乎是一旦遇到ECONNREFUSED错误,selenium / selenium-webdriver就无法恢复。
  7. 有没有人找到解决方案?

1 个答案:

答案 0 :(得分:0)

警告:这只是一个黑客攻击,绝不是一个合适的解决方案,但它是唯一一个在CircleCI上可靠地为我们工作的解决方案。希望其他人能找到更好的解决方案。

我们的测试框架mocha似乎具有专门针对硒开发的重试机制(参见图):http://mochajs.org/#retry-tests。第一个问题是function getPackageDetails(packages) { var detailedPackages = {}; angular.forEach(packages, function(p) { vsmsPackageFactory.getPackage(p.id) .then(function(response) { var subsystem = response.data.packageManifest.subsystem; var partNumber = response.data.packageManifest.resultConfig.partNumber; var packageName = response.data.packageMetaData.packageName; if(detailedPackages[subsystem] === undefined) { detailedPackages[subsystem] = {}; } if(detailedPackages[subsystem][partNumber] === undefined) { detailedPackages[subsystem][partNumber] = {}; } detailedPackages[subsystem][partNumber][packageName] = response.data; }); }); return detailedPackages; // returns before all promises resolved } vsmsCampaignFactory.getCampaignPackages(queryString) .then(function(response) { vm.packageList = getPackageDetails(response.results); }); after中的代码未重试。因此,您需要将代码移至beforebeforeEach。第二个问题是你需要afterEachbeforeEach中的一些特殊逻辑来处理驱动程序错误。

因此,为了利用这种机制,我们必须重构我们的测试,以便每个测试都有一个afterEach来设置一个新的webdriver实例,然后是一个退出该驱动程序实例的beforeEach。这样,崩溃的驱动程序实例不会停止所有后续测试。我们开发了一个辅助函数,我们在所有afterEach块的开头调用,这样我们就不必在测试中添加太多代码了。

然后,我们将describe添加到我们最顶层的this.retries(10)块中。

这是辅助类:

describe

然后,您的测试看起来像:

var webdriver = require('selenium-webdriver');

// We default to using phantomjs, but in the future, when we run tests in other browsers, e.g. via
// SauceLabs, we'll want to change the browser.
var customPhantom = webdriver.Capabilities.phantomjs();
customPhantom.set('ssl-protocol', 'any');

// For convenience in tests
global.By = webdriver.By;
global.until = webdriver.until;

var SeleniumStabilizer = function () {};

SeleniumStabilizer.prototype.MAX_RETRIES = 10;

SeleniumStabilizer.prototype.createDriver = function () {
    global.driver = new webdriver.Builder()
        // .forBrowser('phantomjs')
        .withCapabilities(customPhantom)
        .build();

    // Resolves when driver is ready
    return global.driver;
};

SeleniumStabilizer.prototype.init = function (opts) {

    var self = this,
        n = 0;

    var beforeEachWithRetries = function () {
        // Create a fresh selenium driver for the next test
        return self.createDriver().then(function () {
            if (opts.onBeforeEach) {
                // Execute the test-specific defined onBeforeEach
                return opts.onBeforeEach();
            }
        }).catch(function (err) {
            // ECONNREFUSED error and we should retry?
            if (err.message.indexOf('ECONNREFUSED') !== -1 && n++ < self.MAX_RETRIES) {
                return beforeEachWithRetries();
            } else {
                throw err;              
            }
        });
    };

    opts.beforeEach(function () {
        n = 0;
        return beforeEachWithRetries();
    });

    var afterEachWithRetries = function () {
        return Promise.resolve().then(function () {
            if (opts.onAfterEach) {
                // Execute the test-specific defined onAfterEach
                return opts.onAfterEach();
            }
        }).then(function () {
            // Quit the selenium driver before starting the next test
            return driver.quit();
        }).catch(function (err) {
            // ECONNREFUSED error and we should retry?
            if (err.message.indexOf('ECONNREFUSED') !== -1 && n++ < self.MAX_RETRIES) {
                // Create a new driver
                return self.createDriver().then(function () {
                    return afterEachWithRetries();
                });
            } else {
                throw err;              
            }
        });
    };

    opts.afterEach(function () {
        n = 0;
        return afterEachWithRetries();
    });
};