0

I have a scenario where there are repeating elements with classname .product-tile and I am trying to get the elements by that class name and loop through each of them and finding the element whose title is Products.

If so, I'm trying clicking on apt-add-to-cart-button, but here the code isn't going in for loop.

     var products = element.all(by.css('.product-tile'));
    for (var i = 0; i < products.length; i++) {
    console.log(i);
    var product = products.get(0);
    if (product.element(by.css('.productName .title')).getText() === 'Products') {
        product.element(by.css('apt-add-to-cart-button')).click();
    }
}

html :

enter image description here

Any help would be appreciated.

tania saxena
  • 173
  • 1
  • 2
  • 16

2 Answers2

1

actually the repeating selector should be resolved as a promise like below

element.all(by.css('.product-tile.ng-scope')).then(function(items){
    console.log(items.length); //will get the length here

// do the stuff here
});
tania saxena
  • 173
  • 1
  • 2
  • 16
0

Please read this through, as getText() returns a promise which needs to be resolved.

You may need something like this

var products = element.all(by.css('.product-tile.ng-scope'));
for (var i = 0; i <= products.length; i++) {
console.log(i);
var product = products.get(0);
if (product.element(by.css('.productName .title')).getText().then(function(returnText){
    if(returnText==='Products'){
   product.element(by.css('apt-add-to-cart-button')).click();
}else{
   console.log('Something happened');
}

 });
 }

Edit - Your class contains compound class, so you cannot use a single class inside your CSS selector. Please see updated code

demouser123
  • 4,108
  • 9
  • 50
  • 82