Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Error: route.fetch. Browser has been closed (headed) or Request context disposed (headless) #24168

Closed
1 task done
bondar-artem opened this issue Jul 11, 2023 · 4 comments

Comments

@bondar-artem
Copy link

bondar-artem commented Jul 11, 2023

System info

  • Playwright Version: [v1.36]
  • Operating System: [macOS 13.4.1]
  • Browser: [Chromium, Firefox, WebKit]
  • Other info:

Source code

  • I provided exact source code that allows reproducing the issue locally.

Config file

// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },
    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },
  ],
});

Test file (self-contained)

test('fetch returns error', async ({ page }) => {
    await page.route('https://conduit.productionready.io/api/tags', async route => {
      const response = await route.fetch()
      await route.fulfill({response})
    })
  
    await page.goto('https://angular.realworld.io/');
});

Steps

  • [Run the test]

Expected

In this test, we intercept the "https://conduit.productionready.io/api/tags" that browsers redirect with 307 code to "
https://api.realworld.io/api/tags". route.fetch() should get the response from redirected API, save it into the "response" constant, and fulfill back to complete the call.

Actual

When executed in headed mode using VS Code extension, the test fails with the error "Error: route.fetch: Browser has been closed"
When executed in headless mode using, the test fails with the error: "Error: route.fetch: Request context disposed"
Also execution log sometimes shows only the first GET request only, and sometimes the rest of the stack with 307 redirect and second GET call to redirect API Url
When executed in headed mode using CLI, sometimes error message is "Error: route.fetch: Protocol error (Storage.getCookies): Failed to find browser context for id"

As a result, I am unable to intercept the response of the redirected API URL to process the modified response.

image
image

@mxschmitt
Copy link
Member

Sounds like a duplicate of #23781

@bondar-artem
Copy link
Author

bondar-artem commented Jul 12, 2023

Similar but not duplicate in my opinion.
This one related to interaction with a redirect API call. Playwright can't fetch a response if there is a redirect.

@mxschmitt
Copy link
Member

So what happens is that you are navigating (page.goto), the browser starts a network request and its coming into your page.route handler. But the test is already tearing down, so the context is disposing while your API request is being made. If you add a page.waitForTimeout(1000) after the page.goto it works as expected. (or an expect.toHaveText etc.).

So its a duplicate of #23781

@mxschmitt mxschmitt closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2023
@bondar-artem
Copy link
Author

Thank you for your quick response! I have checked your suggestion, and you are right it works, thank you! So I guess you can close this defect though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants