【问题标题】:Catch an error without failing the Protractor test (wait for element to be visible)在不通过量角器测试的情况下捕获错误(等待元素可见)
【发布时间】:2019-01-03 14:08:38
【问题描述】:

我的应用程序具有仅在某些情况下出现的条款和条件模式。如果模态出现,我希望量角器测试单击同意,如果没有出现,则不执行任何操作。

这是我的方法,我尝试等待元素出现,如果没有出现则捕获错误。如果出现该按钮,则单击该按钮并通过测试。但是,如果该按钮未出现,则测试将失败并显示“元素耗时过长...”消息。

  waitForAgree() {
    var until = protractor.ExpectedConditions;
    try {
      browser.wait(until.visibilityOf(this.agreeTerms), 5000, 'Element taking too long to appear in the DOM');
      this.agreeTerms.click();
      this.continue.click();
    } catch (e) {
      console.log("not found");
    }
  }

我也尝试了以下方法。这给了我消息Failed: Cannot read property 'agreeTerms' of undefined

  clickAgree() {
    //browser.ignoreSynchronization = true;
    this.agreeTerms.isPresent().then(function (isPresent) {
      if (isPresent) {
        this.agreeTerms.click();
        this.continue.click();
      }
      else {
        console.log("not found");
      }
    })
  }

感谢任何有关这些错误的帮助。

【问题讨论】:

    标签: javascript typescript selenium protractor try-catch


    【解决方案1】:

    为了详细说明为什么您的第一种方法不起作用,我们应该注意使用Promise 而不是调用.catch 通常是一个坏主意,因为大多数时候我们永远不会知道哪里出了问题并且@ 987654323@ 根本无济于事。这也是您的替代方法成功的原因,因为您在Promise 上调用.catch

    如果您有兴趣使您的第一种方法发挥作用,那么您应该使用async/await,如下所示:

    async waitForAgree() {
        var until = protractor.ExpectedConditions;
        try {
          await browser.wait(until.visibilityOf(this.agreeTerms), 5000, '...too long...');
          this.agreeTerms.click();
          this.continue.click();
        } catch (e) {
          console.log("not found");
        }
    }
    

    注意函数名前面的关键字async 和对browser.wait 的调用前面的await。请注意,这是 ES6 功能。

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      我会将该 web 元素 存储在 list 中,如果 size of list 等于 1(在这种情况下我知道按钮存在,因此单击它)。如果该列表返回零,则打印失败消息。

      我将使用 findElements 而不是 findElement

      这是定义列表的方式:

      var elems = driver.findElements(By.xpath("your locator")); 
      

      为了计算大小,你可以使用这个:

      var keys = Object.keys(elems);
      var len = keys.length  
      

      然后:

      if (len == 1){
          this.agreeTerms.click();
          this.continue.click();
      }
      else{
        Do something here, button did not appear.
      }
      

      【讨论】:

        【解决方案3】:

        我尝试了第一种方法的替代方法,它有效。我不完全确定为什么我的问题中的第一个失败了,而这个成功了。如果有人想详细说明,请提前谢谢。如果你只需要一个工作方法,这里:

          waitForAgree() {
            var until = protractor.ExpectedConditions;
            browser.wait(until.visibilityOf(this.agreeTerms), 5000, 'Element taking too long to appear in the DOM')
            .then(() => {
              this.agreeTerms.click();
              this.continue.click();
            })
            .catch((error) =>{
              console.log("not found");
            });
          }
        

        【讨论】:

          猜你喜欢
          • 2019-02-13
          • 1970-01-01
          • 1970-01-01
          • 2018-04-20
          • 2019-01-15
          • 2017-02-20
          • 1970-01-01
          • 2014-06-23
          • 1970-01-01
          相关资源
          最近更新 更多