【问题标题】:Protractor/Jasmine2 - async callback not invoked within specified timeoutProtractor/Jasmine2 - 在指定的超时时间内未调用异步回调
【发布时间】:2015-10-12 09:00:51
【问题描述】:

我的 e2e 测试在 selenium 网格上运行时遇到了问题。 有时测试失败是因为

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

试图以某种方式解决它,在 protracotr.conf.js 中将 defaultTimeoutInterval 更改为更高的值,但结果是等待时间更长但错误相同。

exports.config = {
    chromeOnly: true,
    chromeDriver: '../node_modules/.bin/chromedriver',
    framework: 'jasmine2',
    capabilities: {
        'browserName': 'chrome',
        shardTestFiles: true,
        maxInstances: 3
    },
    specs: ['../e2e/protractor/spec/*.js'],
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 30000,
        isVerbose: true,
        includeStackTrace: true,
    },

我的示例规范测试失败:

var LoginPage = require('../pages/login_page.js');
var UsersPage = require('../pages/users_page.js');
var WelcomePage = require('../pages/welcome_page.js');

describe('Test -> my test', function () {
  var loginPage;
  var EC = protractor.ExpectedConditions;
  var waitTimeout = 30000;

  function logIn() {
    loginPage.setUser('user');
    loginPage.setPassword('password');
    loginPage.login();
  }

  var clickOn = function (element) {
    browser.wait(EC.visibilityOf(element), waitTimeout).then(function () {
      element.click();
    });
  };

  beforeEach(function () {
    browser.ignoreSynchronization = true;
    loginPage = new LoginPage();
    browser.wait(EC.presenceOf(loginPage.userLogin), waitTimeout);
    logIn();
    var welcomePage = new WelcomePage;
    clickOn(welcomePage.usersButton);
  });

  afterEach(function () {
    var welcomePage = new WelcomePage();
    welcomePage.loginButton.click();
    welcomePage.logoutButton.click();
  });

  it('verifies counter on active tab', function () {
    var usersPage = new UsersPage();
    browser.wait(EC.visibilityOf(usersPage.firstRow), waitTimeout);
    usersPage.rowsCount.count().then(function (count) {
      expect(usersPage.activeTab.getText()).toContain('Active' + ' (' + count + ')');
    });
  });

谁能提供任何合理的解决方案如何处理/避免它并解释它为什么会发生?

【问题讨论】:

    标签: javascript testing error-handling protractor jasmine2.0


    【解决方案1】:

    我建议在it 块中有一个回调函数,这将确保所有异步代码在此之前执行。例如:

    it('verifies counter on active tab', function (done) {
      var usersPage = new UsersPage();
      browser.wait(EC.visibilityOf(usersPage.firstRow), waitTimeout);
    
      usersPage.rowsCount.count()
        .then(function (count) {
            var text = usersPage.activeTab.getText();
            expect(text).toContain('Active' + ' (' + count + ')');
            done();
         });
    });
    

    【讨论】:

    • 谢谢,你能写出它在我的代码中的样子吗?
    • @Michal 修改了我的答案。
    • IIUC,done() 调用应该在 then() 函数内。
    【解决方案2】:

    实际上,如果您返回承诺,这会更好。 当您在测试中进行异步工作时,您正在脱离代码的顺序期望。 基本上,您的代码块将被执行,并结束对它的调用,但不会引用仍在后台执行的承诺。 这样,量角器就不能等待它完成(但它知道它需要等待),因此测试失败。 无需手动执行 done(),只需添加

    return usersPage.rowsCount.count().then(function (count) {
      expect(usersPage.activeTab.getText()).toContain('Active' + ' (' + count + ')');
    });
    

    【讨论】:

      【解决方案3】:

      我在使用量角器进行 e2e 测试时遇到了同样的问题,但我尝试更改 protractor.conf.js 并且它对我有用。

       jasmineNodeOpts: {
          showColors: true,
          defaultTimeoutInterval: 180000,
          print: function() {}
        },

      如果我们将 defaultTimeOutInterval 增加的时间大于完成测试用例执行所需的时间,这种方法可能会奏效

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-01
        • 2019-11-04
        • 2016-04-21
        • 1970-01-01
        相关资源
        最近更新 更多