我的 Angular 应用程序中有一个 e2e Protractor 测试。我刚刚连续多次运行完全相同的测试,它通过了大约 5% 的时间,失败并出现下面屏幕截图中的错误。
测试:
it('should open the cart with the cart button in the header', () => {
page.navigateTo('/calendar/day/(dmy:27-9-2018)');
page.cartButton().click();
expect(element(by.css('h2.cart-header')).isPresent()).toBe(true);
});
protractor pauses 启动的 chrome 实例显示按钮被点击并且 h2 元素存在(见底部图像)。
我尝试过的
- 我已将此组件中的数据替换为模拟数据以消除异步操作
- 我禁用了动画
- 我试图使它成为一个异步函数:
... header', async () => { ...
- 我试过 await(ing) 元素:
expect(await element(by.css('h2.cart...
- 我尝试过
browser.sleep(1000)
- 我尝试了多种断言,例如
.toBe(true)
、.toEqual(true)
和.toBeTruthy()
是什么导致了这个错误,我该如何解决?
最佳答案
您可以使用 Jasmine 回调断言异步行为。 Jasmine 测试提供附加参数作为回调参数。完成断言后,您可以调用回调 API。
例子:
it('should have a button element present', function(done) {
browser.get('http://juliemr.github.io/protractor-demo/');
var gobtn = element(by.id('gobutton'));
gobtn.isPresent().then( (result) => {
expect(result).toBe(true);
done();
});
});
关于angular - Protractor 结果不一致 - 失败 : script timeout: result was not received,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52583161/