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

时间:2017-04-20 11:56:28

标签: javascript intern leadfoot

我有以下代码段

,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()方法的异常?

1 个答案:

答案 0 :(得分:1)

在回调中,请尝试使用remote代替this.parentthis.parent使用与父链相同的上下文元素。这意味着如果您因为尝试单击陈旧元素而最终进入了catch,则在this.parent.findById(id)中调用catch将执行以该陈旧元素为根的搜索。

相关问题