代码之家  ›  专栏  ›  技术社区  ›  Nate May

量角器结果不一致-失败:脚本超时:未收到结果

  •  5
  • Nate May  · 技术社区  · 7 年前

    我有一个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. 我试着等待元素: expect(await element(by.css('h2.cart...
    5. 我试过 browser.sleep(1000)
    6. 我尝试过各种断言,比如 .toBe(true) , .toEqual(true) .toBeTruthy()

    导致此错误的原因是什么?如何解决?

    错误消息: enter image description here

    元素出现在由量角器启动的浏览器中 enter image description here

    2 回复  |  直到 7 年前
        1
  •  0
  •   Ganesh C    5 年前

    可以使用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
  •  -1
  •   MKrause    7 年前

    请使用 browser.ignoreSynchronization = true;

        3
  •  -1
  •   technodeath    7 年前

    isPresent()返回需要解决的承诺。 是的,使用async()函数+wait可以轻松解决这个问题 更多信息,请使用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!")
    
    推荐文章