javascript 量角器/Jasmine2 - 在指定的超时时间内未调用异步回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33077093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:05:29  来源:igfitidea点击:

Protractor/Jasmine2 - async callback not invoked within specified timeout

javascripttestingerror-handlingprotractorjasmine2.0

提问by Michal

I've struggled in problem with my e2etests runs on selenium grid. Sometimes the tests are failed due to

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

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

Tried to solve it somehow bo changing defaultTimeoutIntervalto higher value in protracotr.conf.jsbut in result the wait is longer but error is the same.

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

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,
    },

My example spec with failed test:

我的测试失败的示例规范:

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

Could anyone please provide any reasonable solution how to handle/avoid it and explain me why it occurs ?

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

采纳答案by Priyanshu Shekhar

I'd suggest to have a callback function in itblock which will ensure that all the async code gets executed before that.For example:

我建议在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();
     });
});

回答by Vino

Actually, this would work better if you returned the promise. As you are doing async work in your test, you are breaking away from the sequential expectations of the code. Basically, your block of code will get executed, and end the call of it, but there will be no references to the promise that is still being executed in the background. With that, protractor cannot wait for it to be completed (but it knows that it needs to wait) so the test fails. Instead of executing the done() by hand, just add

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

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

回答by Aashutosh Singh

I faced same issue while e2e testing using protractor but i tried to change protractor.conf.js and it worked for me.

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

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

If we increase the defaultTimeOutInterval greater than time required to finish the execution of test cases this approach might work

如果我们增加的 defaultTimeOutInterval 大于完成测试用例执行所需的时间,则此方法可能有效