Protractor break while in promise then()

JiggyJinjo Source

I'm automating e2e tests on an angular app. We have virtual scrolls on few input elements (only 10-20 items are loaded at a time, scrolling down loads more items and delete the previous ones to improve performance).

I made a method to scroll until I find the DOM element and click on it. However, I don't know how I can stop looping after I clicked the element.

Here is what I've done so far :

  findQuery(queryName){
    this.getSavedQueryCount().then((queryCount)=>{ // Total nb of items
    this.count().then((displayCount)=>{ //nb of items this screen can display
    let count = 0;
    let flag = 0;
    let actualCount = displayCount + 1;
    do
    {
      if(count % (actualCount) === 0 && count !== 0) count++; //handling weird behavior, index 0 never gets deleted so we increment by 1 to get the next item.
      browser.executeScript('arguments[0].scrollIntoView({behavior: "smooth", block: "end"});', this.getSavedQuery(count % actualCount)); // scroll to elem at index
      this.getSavedQueryByName(queryName).isPresent().then((bool)=>{ // clicking on elem if it's in the DOM
        if(bool)
        {
          this.getSavedQueryByName(queryName).getAttribute('class').then((cl)=>{
            if(!(cl === "selected"))//making sure to click only once so we don't deselect the item
            {
              this.getSavedQueryByName(queryName).click();
              flag = 1;
            }
          });
        }
      });
      count += 1;
    } while(queryCount > count || flag);
  });
});
}

Please don't judge the code I will refactor it after I get the answer to this question (don't take the weird index handling into account). So far this method works but it never stops looping until queryCount > count. I want it to stop looping after I clicked the element too (with the flag var).

Thanks in advance.

javascriptseleniumpromiseprotractor

Answers

comments powered by Disqus