Rayrun
← Back to Discord Forum

Target page, context or browser has been closed

danielstoica13posted in #help-playwright
Open in Discord
danielstoica13
danielstoica13

Hey guys, I need some help with this error.

These are the steps that I am going through:

  1. Perform some steps on one test page.
  2. Close browser with await browser.close();.
  3. Open a new browser session with a mobile device:
const mobileContext = await browser.newContext({ ...galaxyS9 });
 const page = await mobileContext.newPage();```
4. Go to a new test page with `await page.goto(testUrl);`
5. Try to make an ```isVisible()``` assertion: ```await expect(this.closeButton).toBeVisible();```

At this point, the test fails with:  ``` Error: expect.toBeVisible: Target page, context or browser has been closed```

This thread is trying to answer question "Why does the test fail with 'Target page, context or browser has been closed' error when trying to make an 'isVisible()' assertion after closing and opening a new browser session?"

1 reply
danielstoica13
danielstoica13

I can't share the entire code, unfortunately, but the test steps are something like this:

await test.step( 'Bla bla', async () => { // perform some actions before closing the browser await browser.close(); }, );

`await test.step( 'Bla bla', async () => { const browser = await chromium.launch(); const mobileContext = await browser.newContext({ ...galaxyS9 }); const page = await mobileContext.newPage();

await expect('#close-button').toBeVisible();
  },
);`

Answer

Handling 'Target page, context or browser has been closed' Error in Playwright

You might encounter an error saying "target page, context or browser has been closed" when you're trying to make an isvisible() assertion after closing and opening a new browser session in Playwright. This happens because the page or context you're referencing in the assertion is no longer valid.

When you open a new browser session or close a page in Playwright, any references to that page or context become invalid. So, if you try to perform an assertion on an element using isvisible() after closing and opening a new browser session, Playwright will throw an error because it can't find the target element in the closed or newly opened session.

To avoid this error, you need to ensure that you have a valid reference to the page or context before making any assertions. Here's how you can do it:

// Open a new browser session
const browser = await playwright.chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();

// Perform some actions on the page

// Close and reopen the browser session
await browser.close();
const newBrowser = await playwright.chromium.launch();
const newContext = await newBrowser.newContext();
const newPage = await newContext.newPage();

// Check if the reference to 'page' is still valid before making assertions
if (page.isClosed()) {
  // The 'page' reference is no longer valid
  console.log("The 'page' reference is no longer valid");
} else {
  // Make assertions on elements using 'isvisible()'
  const element = await newPage.$('#myElement');
  const isVisible = await element.isvisible();
  
  // Continue with other assertions and test logic
}

By checking if page (or any other reference to a page or context) is closed before making assertions, you can avoid the "target page, context or browser has been closed" error. If the reference is closed, you can handle it accordingly and proceed with your test logic.

For more tips on handling flaky tests in Playwright, check out this blog post.

Related Discord Threads

TwitterGitHubLinkedIn
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 [email protected].