【问题标题】:How to break out of for loop in protractor?如何打破量角器中的for循环?
【发布时间】:2014-04-04 21:44:30
【问题描述】:

这是我的代码 -

formElements[0].findElements(by.repeater(repeater)).then(function(items){
            console.log(i, '>>>>>>>>>.No of items in the list --- '+items.length);
            (function(items){
                for(var x1=0; x1<itemsToBeSelected.length; x1++){
                    console.log(i, '>>>>>>.Looking for item --- '+itemsToBeSelected[x1]);
                    skip = false;
                    (function(items, x1){
                        for(var x2=0; x2<items.length; x2++){
                            (function(items, x2){
                                items[x2].getText().then(function(itemName){
                                   console.log(i, '>>>>..Verifying '+itemsToBeSelected[x1]+' with '+itemName);
                                   if(itemName == itemsToBeSelected[x1]){
                                       console.log(i, '>>>>>.Selecting the item --- '+itemName);
                                       items[x2].findElement(by.css('.regular-checkbox')).click();

                                   }
                                });
                            }(items, x2));
                        }
                    }(items, x1));
                }
            }(items));
        });

当满足条件 itemName == itemsToBeSelected[x1] 时,我想跳出内部 for 循环。尝试使用标志、返回语句,但无法跳出循环。

请在代码中提出更正建议。

【问题讨论】:

  • 你试过简单地返回吗?
  • 是的,但是没用。

标签: javascript node.js selenium-webdriver protractor


【解决方案1】:
 ptor.element.all(by.repeater(repeater)).then(function(products){
           console.log(i, '>>>>>>>>>.Products length --- '+products.length);
           async.each(products, verifyName, function(err){
               console.log('>>>>>>>>>>>err value --- '+err);
               expect(err).toBe(true);
               if(err){
                   console.log(i, '>>>>>>>>.Element is present');
               }else{
                   console.log(i, '>>>>>>>>.Element is not present');
               }
           });
            function verifyName(product, callback){
                console.log(i, '>>>>>>>>.Inside function verifyName');
                product.getText().then(function(name){
                    console.log('>>>>>>>>>>Looking for product --- '+name);
                    if(name==entityName){
                        console.log(i, '>>>>>>>>Verified the name - '+name);
                        callback(true);
                    }
                });
            }
    });

我们也可以在 async.each 模块的帮助下获得相同的结果。例如我发布了一个代码,我试图在其中找到一个值。

所以关于我的问题,我们可以在设置回调(true)之前单击或对元素执行任何操作。例如说在这里我们可以做 - product.click();

【讨论】:

  • 我收到 ReferenceError: async is not defined
【解决方案2】:

别管我在编辑前说了什么,您可以使用 caolan's async module 使用检测或检测系列函数来迭代您的数组。

它应该看起来像这样:

formElements[0].findElements(by.repeater(repeater)).then(function(items) {
  console.log(i, '>>>>>>>>>.No of items in the list --- ' + items.length);
  itemsToBeSelected.forEach(function(itemToBeSelected) {
    async.detect(items, function(item, next) {
      item.getText().then(function(itemName) {
        // This function will be called on each item in items, until next(true) is called
        console.log('Verifying ' + itemToBeSelected + ' with ' + itemName);
        // Here you call the callback with the truth value :
        return next(itemName === itemToBeSelected);
      });
    }, function(item) {
      // This function is called with the first item that resulted in a true
      // callback value.
      console.log('Selecting the item --- ' + item);
      item.findElement(by.css('.regular-checkbox')).click();
    });
  });
});

【讨论】:

  • 谢谢,但我已经尝试过了。这是行不通的。它进入if条件,它设置breaker = true。但是在它退出 function(items, x2) 之后,breaker 的值变为 false。因此无法跳出循环。
  • 哦对,没注意到异步 then()。
  • 由于我无法清楚地理解这个模块,能否请您为我的场景发布一个示例代码?
猜你喜欢
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
  • 2014-04-04
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多