When working with selenium in JavaScript, I have a challenge where I need to constantly refresh the page until a specific element disappears. The issue arises because selenium uses promises, and using a for loop creates multiple promises even when the condition is met, causing them to be resolved later on. Additionally, I am required to include driver.sleep(5000) for some reason.
$browser.get("https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html").
then( function()
{
var p=Promise.resolve();
for (let index = 0; index < 5; index++) {
p = p.then(() =>
isElementDisplayedAfterrefresh($driver.By.xpath("//span[text()='PouchDB']"), "input"))
.then(a=>$browser.sleep(3000))
.then(()=>$browser.navigate().refresh())
}
return p;
}
).then(
()=>console.log('done'))
The code should stop if the element is not displayed, and continue refreshing if the element is still visible. How can I iterate through promises sequentially without knowing the results or number of iterations? I need to return a Promise type and also know how to break out of the loop. Thank you!
Edit
https://gist.github.com/saiparth/045c32267e55b836f139bdac6597e57b The issue I'm facing is that it schedules commands for all 5 iterations, regardless of whether the previous index worked or not. Additionally, I am unable to use async/await in this scenario.