Rayrun
โ† Back to Discord Forum

Alternative of beforeAll and AfterAll

deniztekeliposted in #help-playwright
Open in Discord

In my tests I have to use beforeAll and AfterAll, but they are not supported in playwright. Do you have an alternative solution?

This thread is trying to answer question "What is the alternative solution for using beforeAll and AfterAll in playwright?"

7 replies
.aleksandaraleksandrov
.aleksandaraleksandrov

What do you mean they are not supported? I use them in my tests and it is all good ๐Ÿ™‚

Playwright supports those hooks

I have this error when I use them Error: "context" and "page" fixtures are not supported in beforeAll.

What are you trying to do?

.aleksandaraleksandrov
.aleksandaraleksandrov

Yeah, I went through that too ๐Ÿ™‚

I think this is a bad practice, but you can still create what needs to be created Here I send an API call to create an object, that I can use in the whole test set

test.beforeAll(async ({ request, browser, baseURL }) => { const context2 = await browser.newContext(); // Create a new page within the context const cleanupPage = await context2.newPage(); // Getting the authentication token from the browser context const cookies = await cleanupPage.context().cookies();

const jwtCookie = cookies.find((cookie: { name: string; }) => cookie.name === "adminToken");
let token: string;
if (jwtCookie) {
  token = jwtCookie.value;
  // Use the `request` fixture to make a POST request with the authentication token
  const response = await request.post(`apiURL`, {
    headers: {
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json',
    },
    data: {
      "object": {
        "property": value
      },
      "property": value,
      "property": value,
    }
  });
  // Validate the response if needed
  expect(response.status()).toBe(200);
  const jsonResponse = await response.json();

  // Now you can work with the parsed JSON data (e.g., access properties)
  reservationId = jsonResponse.id
}
await cleanupPage.close();
await context2.close();

});

.aleksandaraleksandrov
.aleksandaraleksandrov

And I think you can create a custom fixture, as was shown in the 1.39 update and merge it ๐Ÿ™‚

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].