Playwright · Junior · Question 57 of 75

How do you work with a link that opens a new tab?

const pagePromise = context.waitForEvent("page");
await page.getByText("Open docs").click();
const newPage = await pagePromise;
Show the answer

Correct answer: Wait for the context's page event while clicking

Explanation

Start waiting before the click so the event isn't missed. The new page is a normal Page you can assert on.

Further reading

Problem with a question

Playwright · Junior · question 57

Test yourself

Take the Playwright · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz