The Playwright library provides a page.waitForEvent() method can be used to wait for a specific event to occur on a page.
You can use this method to wait for an event, such as a DOM element being clicked, a form being submitted, a page is loaded, or a network request being made.
Here is an example of how you might use this method to wait for a page to be created:
const pagePromise = context.waitForEvent('page');
await page.getByRole('button').click();
const page = await pagePromise;
- In this example, the script will wait for the page to be loaded and then clicks on the element once it is done waiting for that said event.
- The waitForEvent() method returns a promise that resolves to the event object once the event occurs.\
- You can also pass a timeout to the waitForEvent() method if you want to wait for a certain amount of time before timing out:
const pagePromise = context.waitForEvent('page',{ timeout: 3000 });
- In this example, the script will wait for the page to be opened for 3 seconds. If it is not opened within that time, the promise returned by waitForEvent() will be rejected with a timeout error.
- You can also use page.waitForEvent() to wait for a specific event to occur on the entire page rather than a particular element.
For example:
const frameNavigated = await page.waitForEvent('framenavigated');
- This script will wait for a frame navigation event.
- Log in to post comments