DEV Community

Styrow.dev
Styrow.dev

Posted on Originally published at styrow.dev

How do you effectively manage and interact with new browser windows opened via `window.open()` in Playwright, especially

⚠️ Playwright Popup Panic: Can you tame a window.open() beast?
Ever struggled to automate interactions with those pesky new browser windows opened via window.open() in Playwright? It's a common trap!


📌 Problem Statement
Automating applications that use window.open() to spawn new browser windows or tabs (popups) presents a unique challenge in Playwright.

❌ Unlike creating a new page programmatically or handling simple dialogs, these popups require a specialized approach.
• Without proper handling, your tests can fail to interact with, or even recognize, these dynamically created pages.

💡 Solution & Code Walkthrough
The key is to leverage Playwright's page.waitForEvent('popup') concurrently with the action that triggers the new window.

✅ Use Promise.all() to prevent race conditions, ensuring you capture the new Page object as soon as it appears.
• This listener returns the new Page object, allowing you to interact with it just like any other Playwright page.

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

test('manage window.open() popups', async ({ page }) => {
  // Simulate a page that opens a popup
  await page.goto('data:text/html,<button id="open">Open Popup</button>');

  // ✅ Concurrently wait for the popup event and click the trigger
  const [popupPage] = await Promise.all([
    page.waitForEvent('popup'), // 1. Listen for the new window
    page.click('#open')         // 2. Trigger the window.open()
  ]);

  // Now interact with the popup page
  await popupPage.waitForLoadState(); // Ensure content is loaded
  await popupPage.goto('data:text/html,<h1>Popup Content</h1><input id="data">');
  await expect(popupPage.locator('h1')).toHaveText('Popup Content');
  await popupPage.fill('#data', 'Playwright rocks!');
  await expect(popupPage.locator('#data')).toHaveValue('Playwright rocks!');

  await popupPage.close(); // Clean up: close the popup
});
Enter fullscreen mode Exit fullscreen mode

🔑 Key Takeaways
• Always use page.waitForEvent('popup') to capture new windows opened by window.open().
• Combine it with Promise.all() to avoid race conditions between trigger and event.
• The captured popup is a standard Playwright Page object, allowing full interaction.
• Remember to close popups (popupPage.close()) if they are no longer needed.

❓ Quick Summary Q&A
Q: How do you detect a window.open() popup in Playwright?
A: Use page.waitForEvent('popup').
Q: Why use Promise.all() with popups?
A: To avoid race conditions and ensure the event listener is active before the popup opens.

TAGS: playwright, automation, testing, javascript, typescript, web-automation

────────────────────────────────────────
Ready to master advanced Playwright scenarios? Download our app for more expert insights and code examples!
────────────────────────────────────────

📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:

🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261102

🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261102&mt=8

────────────────────────────────────────
────────────────────────────────────────

Top comments (0)