【问题标题】:Protractor break while of for loopfor循环的量角器中断
【发布时间】:2017-02-21 07:52:00
【问题描述】:

我需要有关环喙的帮助。

为了我的检查,我做了简单的测试:

while(i < 10) {
    element(by.xpath("//a[contains(@id, 'ma-apply')]")).isPresent().then(function(result) {
        if(!result) {
            helper.one_page_ahead();
        } else {
            console.log('there is on the page');
            break;
        }
    });
    i++;
};

此代码导致错误。

我尝试通过 StackOverflow 遵循建议并将 break 更改为 return。 但这会导致完整的循环执行(最多 10 个)。

这是输出:

[14:17:46] I/launcher - 运行 1 个 WebDriver 启动用户实例 技巧:AJAX 有在页面上有在页面上有在 页面有页面有页面有页面有页面 页面上有 页面上有。

1 个规范,0 个失败在 37.93 秒内完成

我尝试了与for 类似的循环

for(i = 0; i < 10; i++) {
    //code
    break;
}

很高兴找到答案。

【问题讨论】:

    标签: javascript loops for-loop while-loop protractor


    【解决方案1】:

    这是关于为什么while 语句不起作用的一些评论:当您调用isPresent 时,您会返回一个webdriver.promise.Promise&lt;boolean&gt;。由于你在 webdriver 控制流中,你需要抛出一个错误,

          browser.get('http://angularjs.org');
          var i = 0;
          var running = true;
          while(i < 3 && running) {
            console.log('while: ' + running + ' ' + i);
            element(by.model('username')).isPresent().then((result) => {
              console.log('element: ' + running + ' ' + i);
    
              if (result) {
                // huzzah we found it, so lets break the element after the first test
                browser.get('https://docs.angularjs.org/tutorial');
              } else {
                running = false;
                throw new Error('no username')
              }
    
            }).catch((err) => {
              console.log(err);
            });
            i++;
          }
    

    这基本上打印出来了:

    [19:07:18] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
    [19:07:18] I/launcher - Running 1 instances of WebDriver
    Started
    while: true 0
    while: true 1
    while: true 2
    element: true 3
    [Error: no username]
    element: false 3
    [Error: no username]
    element: false 3
    [Error: no username]
    

    所以基本上你的 while 循环将控制流中的项目排队执行。然后这些将按顺序异步执行。

    我喜欢 Sudharsan Selvaraj 提出的递归执行此操作的建议。

    【讨论】:

      【解决方案2】:

      你需要实现一个recursive方法来实现你想要的,试试下面的代码,

      function runTillElementFound(totalCount,currentCount){
        var self = this;
        var _element = element(by.xpath("//a[contains(@id, 'ma-apply')]"));
        if(currentCount < totalCount){
           return _element.isPresent().then(function(isElementPresent){
              if(isElementPresent){
                 return;
               }else{
                 helper.one_page_ahead();
                 self.runTillElementFound(totalCount,currentCount++);
               }
            })
        }else{
           return false; //if element not present after Max count reached.
         }
      }
      
      this.runTillElementFound(10,0); //this will execute the method untill the required element found on the page.
      

      【讨论】:

      • 我同意递归是做到这一点的方法。您想检查元素是否存在,等待结果并再次递归调用它或退出。
      • 我删除了这个和自我,它可以工作,但是在第二页之后它抛出错误“回调没有在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内调用”。我尝试在 beforeAll 部分设置 default_timeout_interval,但也没有帮助
      • 非常感谢,我只将间隔增加到 120 秒)
      • 欢迎@Halina
      【解决方案3】:

      如果你想避免递归,你可以修改返回的promise中的索引变量

      while(i < 10) {
      element(by.xpath("//a[contains(@id, 'ma-apply')]")).isPresent().then(function(result) {
          if(!result) {
              helper.one_page_ahead();
          } else {
              console.log('there is on the page');
              i = 10;
          }
      });
      i++;
      

      };

      我会在每次重复之间添加一个 browser.sleep(x) 以避免在评估 promise 的结果之前运行代码。

      【讨论】:

        【解决方案4】:

        i = 10;不生效,仍在循环迭代

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多