In this article, we are gonna see about the wait for navigation method in playwright. When a certain link is clicked and it navigates to a new page we can use the page.waitForNavigation() till the page gets navigated and action is performed.
const [response] = await Promise.all([
page.waitForNavigation(),
page.click('#button')
]);
- To avoid race condition this is wrapped inside the promise function which will wait until navigations get completed and then performs the next provided operation.
- page.waitForNavigation() waits explicitly for the event to happen and continues.
- This is actually from the puppeteer and the playwright offers the same API for waiting on events and elements.
page.waitForNavigation() is similar to page.reload() and goBack().
page.waitForNavigation({ timeout: 2000 })
- The timeout can be set inside the waitForNavigation as an option to hard timeout.