量角器中如何跳出for循环

How to break out of for loop in protractor?

本文关键字:for 循环 何跳出 量角器      更新时间:2023-09-26

这是我的代码-

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循环。尝试使用标志、返回语句,但无法跳出循环。

请建议修改代码

 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的帮助下获得相同的结果。每个模块也一样。例如,我发布了一个代码,我试图找到一个单一的值。

因此,关于我的问题,我们可以在设置callback(true)之前单击或对元素执行任何操作。例如,我们可以这样做- product。click();

不用管我在编辑之前说了什么,你可以使用caolan的async模块来迭代你的数组,使用detect或detectSeries函数。

应该是这样的:

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();
    });
  });
});