【问题标题】:Protractor retry a function after wait conditions fails等待条件失败后量角器重试函数
【发布时间】:2023-03-03 15:41:02
【问题描述】:

我正在尝试修复一些不稳定的测试。 量角器:5.3.2 角 6

这就是我想要做的:

  1. 加载页面(数据应从数据库加载)
  2. 等到满足条件:数据已加载,加载微调器不可见
  3. 如果不满足条件重试,重新加载页面

这是我的代码:

  async navigate(): Promise<void> {
    return await browser.get('/', 10000).then(async () => {
      return await this.waitForDataToLoad(10000);
    });
  }

  async waitForDataToLoad(timeOut?: number): Promise<any> {
    let isSpinnerInVisible = ExpectedConditions.invisibilityOf(element(By.css("spinner")));
    let isWorkflowLoaded = ExpectedConditions.visibilityOf(element(By.css("employee-list ul li:first-child a")));
    return browser.wait( ExpectedConditions.and(isSpinnerInVisible, isWorkflowLoaded), timeOut, 'data was not loaded in the given timeout');
  }

我有几个问题:

1 - 我的自定义超时错误消息永远不会在控制台中打印,它只会将默认错误打印为: - Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

2- 我尝试如下添加重试功能,但似乎不起作用:

 async navigate(): Promise<void> {
    return await browser.get('/', 10000).then(async () => {
      return await this.waitForDataToLoad(10000);
    }, (err) => {
      console.log('Data was not loaded:', err.message);
      this.navigate(); // Re try 
    });
  } 

知道如何实现这一目标吗?我知道有 Protractor flake 和 Retry npm 包,但它们并不是我所需要的。

【问题讨论】:

    标签: angular async-await protractor timeout


    【解决方案1】:

    我将我的问题解决为:

      navigate(): promise.Promise<any> {
    return browser.get('/').then(async () => {
      let waitForCondition = this.waitForDataToLoad(10000)
        .then((found) => {
          return Promise.resolve(found);
        })
        .catch((error) => {
          if (this.retryAttempts < 4) {
            console.log(`Retrying navigate, attempt: ${this.retryAttempts}.`);
            this.navigate();
          } else {
            console.log('Rejecting the promise after 3 attempts.');
            return Promise.reject(error);
          }
        });;
      this.retryAttempts++;
      return waitForCondition;
    });
    

    }

    waitForDataToLoad(timeOut?: number): promise.Promise<{}> {
        let isSpinnerInVisible =  let isSpinnerInVisible = ExpectedConditions.invisibilityOf(element(By.css("spinner")));
        let isWorkflowLoaded = ExpectedConditions.visibilityOf(element(By.css("employee-list ul li:first-child a")));
    
        return browser.wait(ExpectedConditions.and(isSpinnerInVisible, isWorkflowLoaded), timeOut)
      }
    

    【讨论】:

    • 看来您已经找到了解决方案。我通常使用setInterval 来处理这类事情。间隔将一直运行,直到满足正确的条件,然后您可以告诉它退出。你可以看到一个不错的例子here
    猜你喜欢
    • 2017-02-02
    • 1970-01-01
    • 2017-12-30
    • 2015-03-17
    • 2019-10-03
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多