量角器在等待条件失败后重试功能

时间:2018-06-21 16:28:38

标签: angular async-await protractor timeout

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

这就是我想要做的:

  1. 加载页面(数据应从db加载)
  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软件包,但它们并不是我真正需要的。

1 个答案:

答案 0 :(得分:0)

我将问题解决为:

  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)
  }