6

我的角度应用程序中有一个 e2e 量角器测试。我刚刚连续几次运行完全相同的测试,它通过了大约 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);
});

量角器暂停启动的 chrome 实例显示该按钮已被单击并且 h2 元素存在(参见底部的图像)。

我试过的

  1. 我已经用模拟数据替换了这个组件中的数据,以消除异步操作
  2. 我禁用了动画
  3. 我试图使它成为一个异步函数:... header', async () => { ...
  4. 我试过 await(ing) 元素:expect(await element(by.css('h2.cart...
  5. 我试过browser.sleep(1000)
  6. 我尝试了各种断言,例如.toBe(true),.toEqual(true).toBeTruthy()

是什么导致了这个错误,我该如何解决?

错误信息: 在此处输入图像描述

该元素存在于量角器启动的浏览器中 在此处输入图像描述

4

3 回答 3

0

您可以使用 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();
 });    
});    
于 2021-01-03T02:52:31.917 回答
-1

isPresent() 返回您需要解决的承诺。是的,使用 async() 函数 + await 可以轻松解决更多问题,使用 ExpectedConditions 模块,如下所示:

let cart = await element(by.css('h2.cart-header'))
await browser.wait(ExpectedConditions.visibilityOf(cart), 5000, "Cart is not visible even in 5 seconds!")
于 2018-11-05T15:39:36.033 回答
-1

请用browser.ignoreSynchronization = true;

于 2018-10-26T09:06:22.120 回答