How do I use Playwright to wait for a specific popup to close?


Waiting for a Popup to Close in Playwright

To wait for a popup to close in Playwright, use the waitForEvent method from the Page class. This method allows you to wait for a specific event, such as the "popupclosed" event, before continuing your code execution.

Here's a TypeScript example using @playwright/test:

import { test, expect } from '@playwright/test';

test('Wait for popup to close', async ({ page }) => {
  // Navigate to the website
  await page.goto('https://ray.run');

  // Start waiting for the "popupclosed" event
  const popupClosedPromise = page.waitForEvent('popupclosed');

  // Perform actions while waiting for the popup to close
  // ...

  // Continue after the popup has closed
  await popupClosedPromise;

  // Add your assertions or further actions
  // ...

In this example, we first navigate to the website and then start waiting for the "popupclosed" event by calling page.waitForEvent('popupclosed'). This returns a promise that resolves when the event is emitted on the page.

While waiting for the popup to close, you can perform any necessary actions in your code. When you're ready to continue after the popup has closed, simply await the promise returned by waitForEvent.

Keep in mind that this method waits for any popup on the page to close. If you need to wait for a specific popup, you may need additional logic in your code.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.