playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[REGRESSION]: `apiRequestContext` is not accessible in `afterAll()` hook

Open aoj2 opened this issue 2 years ago • 3 comments

Context:

  • GOOD Playwright Version: 1.39.0
  • BAD Playwright Version: 1.40.0
  • Operating System: Mac
  • Extra: [any specific details about your environment]

Code Snippet

import { test, expect, APIRequestContext } from "@playwright/test";

test.describe(() => {
  let context: APIRequestContext;

  test.beforeAll(async ({ request }) => {
    context = request;
    console.log("running before all");
  });

  test.afterAll(async () => {
    const storage = await context.storageState();
    console.log(storage);
    console.log("running after all");
  });

  test("has title", async ({ page }) => {
    await page.goto("https://playwright.dev/");

    // Expect a title "to contain" a substring.
    await expect(page).toHaveTitle(/Playwright/);
  });

  test("get started link", async ({ page }) => {
    await page.goto("https://playwright.dev/");

    // Click the get started link.
    await page.getByRole("link", { name: "Get started" }).click();

    // Expects page to have a heading with the name of Installation.
    await expect(
      page.getByRole("heading", { name: "Installation" })
    ).toBeVisible();
  });
});

Describe the bug Setting up a request context in the the beforeAll()-hook using the fixture, to prep the test environment before the tests run, which works fine, but after all tests have run, when the clean-ups steps should run, the context is no longer reachable. This used to work in 1.39.0

Add any other details about the problem here.

Running 2 tests using 1 worker
[chromium] › example.spec.ts:16:7 › has title
running before all
  1) [chromium] › example.spec.ts:23:7 › get started link ──────────────────────────────────────────

    Error: apiRequestContext.storageState: Target page, context or browser has been closed

       9 |
      10 |   test.afterAll(async () => {
    > 11 |     const storage = await context.storageState();
         |                                   ^
      12 |     console.log(storage);
      13 |     console.log("running after all");
      14 |   });

  1 failed
    [chromium] › example.spec.ts:23:7 › get started link ───────────────────────────────────────────
  1 passed (2.7s)

aoj2 avatar Jan 31 '24 13:01 aoj2

@aoj2 I ran into this exact issue, the problem is you cannot re-use the apiRequestContext once it has been closed. I noticed this when I was trying to do the same thing by sharing the same apiRequestContext (Within a sort of page object/class for my apiRequestContext).

Presumably after the last test the context is cleanup up/closed. You can see where I talked about it here: https://github.com/microsoft/playwright/issues/28953#issuecomment-1889643404

msmith1114 avatar Feb 09 '24 17:02 msmith1114

Can confirm that solution mentioned in https://github.com/microsoft/playwright/issues/28953#issuecomment-1889643404 works in my case as well. Guess we were just lucky that it worked in 1.39.0

Have a follow-up question regarding the example below though, we usually the set up an api context per test spec. Should this context also be closed or disposed manually or is that handled automatically? Everything works ok, so just looking for "best practices"

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

let widget: Widget;

// Note no {request} argument.
test.beforeAll(async () => {
  // Create a new request context.
  const apiContext = await request.newContext();
  widget = new Widget(apiContext);
});

aoj2 avatar Feb 14 '24 12:02 aoj2

@aoj2 What i've done is just make a new context in a beforeEach hook (or also in a beforeAll hook if you have that). It's not as clean but it's the only way to ensure that each test has it's own new context. I've not needed to dispose the context after tests though FWIW (I think it does that automatically? but someone may need to confirm) although i've had no issues with not calling dispose

msmith1114 avatar Feb 14 '24 21:02 msmith1114