繁体   English   中英

如何在实习生JS中捕获异常?

[英]How do I catch an exception in intern JS?

我有以下片段

,clickStaleElement: function(remote, id) {
  return remote
    .findById(id)
      .execute(function(id){
        //force org.openqa.selenium.StaleElementReferenceException
        $(document.getElementById(id)).addClass('hidden');
      },[id])
      .click()
      .catch(function(){
        return this.parent
          .findById(id)
            .execute(function(id){
              //bring back the element
              $(document.getElementById(id)).removeClass('hidden');
            },[id])
            .click()
          .end()
        ;
      })
    .end()
  ;
}

应该可以处理StaleElementReferenceException或其他任何原因,然后尝试再次找到该元素并单击它。 该元素是按固定间隔添加到dom或从dom中删除的,因此有时我在测试运行中遇到此异常,因此运行失败。 我想处理此异常并防止运行失败,因为不是由于错误(或不是)而导致真正失败。

所以问题是如何处理.click()方法上的异常?

在回调中,尝试使用remote代替this.parent this.parent使用与父链相同的context元素。 这意味着,如果你在catch结束了,因为你想点击一个陈旧的元素,调用this.parent.findById(id)catch将执行扎根陈旧元素上进行搜索。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM