如何在量角器中摆脱for循环?

时间:2014-03-21 08:46:08

标签: javascript node.js selenium-webdriver protractor

这是我的代码 -

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循环。尝试使用flag,return语句,但无法从循环中突破。

请在代码中建议更正。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

在编辑之前没关系我说的话,你可以使用caolan's async module使用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();
    });
  });
});