【问题标题】:Protractor infinite loop量角器无限循环
【发布时间】:2015-12-27 01:02:21
【问题描述】:

在上一个问题中,我在单击按钮之前遇到问题,直到它被禁用答案是:

var nextPage = function () {
    if (element(by.css('[ng-click="vm.nextPage()"]')).isEnabled()) {
        element(by.css('[ng-click="vm.nextPage()"]')).click();
        nextPage(); // next page
    }
    else {
        return; // the element is not enabled, last page
    }
}

我不得不稍微改变一下,所以我的代码现在看起来像这样

 var nextPage = function() {
  if (element(by.id('next')).isEnabled()) {
    element(by.id('next')).click().then(function() {
      browser.sleep(1000);
      nextPage(); // next page
      return;
    });
  } else {
    return; // the element is not enabled, last page
  }
  return;
}

但现在它跳入无限递归调用,因此无法执行下一步,我怎么不能改变它,不使用.then函数根本不起作用。

【问题讨论】:

    标签: javascript angularjs recursion protractor infinite-loop


    【解决方案1】:

    问题在于递归基本情况永远不会满足并且它永远不存在递归。

    请记住,protractor 中的所有内容都是一个承诺isEnabled() 在你的情况下是一个未解决的承诺,它始终是“真实的”。这就是为什么不管真正的isEnabled 值如何,您总是会进入下一个循环并最终得到最大递归深度误差。

    明确解决承诺以获得真正的isEnabled布尔值:

    element(by.id('next')).isEnabled().then(function (isEnabled) {
        if (isEnabled) {
            // ...
        } else {
            return; // the element is not enabled, last page
        }
    });
    

    作为旁注,无耻的宣传可能有助于更早地发现这个问题 - eslint-plugin-protractorit's rule to catch when protractor promises are used inside if conditions directly without being explicitly resolved。如果针对问题中发布的代码执行以下是它会输出的内容:

    $ eslint test.js
    /path/to/test.js
      2:5   warning  Unexpected "isEnabled()" inside if condition  protractor/no-promise-in-if 
      4:13  warning  Unexpected browser.sleep()                    protractor/no-browser-sleep
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      相关资源
      最近更新 更多