This thread is trying to answer question "What is the alternative solution for using beforeAll and AfterAll in playwright?"
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();
});
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].