javascriptprotractorplaywright

Playwright equivalent to Protractor's browser.wait to loop while condition is not met


I'm trying to find a way to loop in Playwright while certain condition is not met. In Protractor I could use browser.wait. In the following example which I'm migration from Protractor to Playwright, the code clicks on the refreshButton while the testButton is not visible, and once it's visible it clicks on the testButton:

        await browser.wait(async function () {
            return testButton.isPresent().then(async function (present) {
                // Refresh the grid until testButton is present
                if (present) {
                    await testButton.click();
                    return true;
                } else {
                    await refreshButton.click();
                    return false;
                }
        });

I've seen that Playwright offers a set of calls to wait for specific conditions such as waitForSelector, but I'm having troubles trying to get them to work in a loop, since a plain while wouldn't be resolved before the promises.


Solution

  • Since Playwright v1.29, you are able to retry blocks of code until all assertions within pass:

    await expect(async () => {
        await expect(testButtonLocatorHere).toBeVisible();
    }).toPass();
    
    await testButtonLocatorHere.click();
    

    I think that might be what you are looking for :)

    You could also look at polling as a possible solution. I've used that for something similar to your issue before.