2015-10-12 36 views
6

我一直在试图在硒网格上运行我的e2e测试。 有时测试失败,因为Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.量角器/ Jasmine2 - 在指定的超时时间内未调用异步回调

试图解决它在某种程度上改变博来defaultTimeoutInterval更高价值protracotr.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, 
    }, 

我的例子规范与失败的测试:

变种LoginPage =要求(” ../页/ 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 + ')'); 
    }); 
    }); 

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

回答

6

我建议有在it块的回调函数,这将确保所有的异步代码被that.For例如前执行:

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

谢谢,请问如何在代码中查看它? – Michal

+0

@Michal修改我的答案。 –

+0

IIUC,'done()'调用应该在'then()'函数内。 – Anton

2

事实上,这将更好地工作,如果你返回的承诺。 由于您在测试中正在进行异步工作,因此您正在摆脱对代码的连续期望。 基本上,你的代码块将被执行,并结束对它的调用,但是不会引用仍在后台执行的promise。因此,量角器不能等待它完成(但它知道它需要等待),所以测试失败。 而不是手工执行done(),只需添加

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