【问题标题】:protractor inconsistent results - Failed: script timeout: result was not received量角器结果不一致 - 失败:脚本超时:未收到结果
【发布时间】:2018-10-01 00:05:05
【问题描述】:

我的 Angular 应用程序中有一个 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);
});

protractor pauses 启动的 chrome 实例显示按钮被点击并且 h2 元素存在(见底部图片)。

我尝试过的

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

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

错误信息:

元素存在于量角器启动的浏览器中

【问题讨论】:

  • 我也有同样的问题,你解决了吗?
  • 我在 GitHub 上打开了一个关于这个问题的 issue github.com/angular/protractor/issues/5151
  • 我没有,我最终切换到 cyress.io 而不是量角器。
  • Wooah Cypress 太棒了!非常感谢分享!你满意吗?

标签: angular typescript protractor e2e-testing


【解决方案1】:

您可以使用 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();
 });    
});    

【讨论】:

    【解决方案2】:

    请使用browser.ignoreSynchronization = true;

    【讨论】:

    • 这不是当元素不可见时发生的正常超时。这是量角器的内部故障。
    【解决方案3】:

    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!")
    

    【讨论】:

    • Protractor 的控制流程在 5.x.x 中为您执行此操作。不需要异步/等待。
    猜你喜欢
    • 2019-07-09
    • 2019-09-14
    • 2019-11-11
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 2019-10-21
    • 2019-07-30
    • 2016-09-01
    相关资源
    最近更新 更多