Playwright · Reliable end-to-end tests
How do you handle multiple pages, tabs and popups?
Short answer
A context can hold several pages. Wait for the popup or page event before interacting with the new tab, then use the returned Page object rather than assuming it is ready.
Clicking a link with target=_blank or calling window.open creates another page in the same context. The new page is a separate Page object, so your locators on the original page will not see it. Wait for the event and keep the new reference.
const popupPromise = page.waitForEvent("popup");
await page.getByRole("link", { name: "Open invoice" }).click();
const popup = await popupPromise;
await popup.waitForLoadState();
await expect(popup.getByRole("heading", { name: "Invoice" })).toBeVisible();The Promise.all pattern is equivalent and often reads better. It starts both operations together and returns the click result and the new page. Either way, the key is that the wait is set up before the action that opens the page.
const [popup] = await Promise.all([
page.waitForEvent("popup"),
page.getByRole("link", { name: "Open invoice" }).click(),
]);
await popup.getByRole("button", { name: "Download" }).click();
await popup.close();Useful context methods
context.pages()returns all open pages in the contextcontext.waitForEvent("page")waits for a new page created by any actionpage.waitForEvent("popup")waits for a popup opened by that pagepage.close()closes one page without closing the context
waitForLoadState before interacting, especially for a page that redirects.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why should you start waiting before clicking?
- 2What is the difference between the page and popup events?
- 3How do you close a popup without closing the context?
- 4Does a popup get its own browser context?
Say it in the interview
A new tab is another page in the same context. I set up a wait for the popup event before clicking the link, then use the returned Page object for assertions and actions. I often start the wait and the click together with Promise all. The popup shares the context but not the page, so I never reuse locators from the original tab on it.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Playwright mock interview and get scored feedback on depth, clarity and confidence.