๐ช๐ต๐ฎ๐ ๐๐ ๐ฎ ๐๐ฒ๐๐ฐ๐ฟ๐ถ๐ฏ๐ฒ ๐๐น๐ผ๐ฐ๐ธ?
test.describe() groups related tests under one name. The tests inside it still run as separate cases in this lesson. A failure in one does not cancel the others. The group name shows up in the report.
test.beforeEach() runs before every test in the group. test.afterEach() runs after every test, including a test that failed. Use beforeEach for a shared starting point, such as opening a page. Use afterEach for cleanup that must happen even when an assertion fails.
A test should not need another test to have run first. Playwright can run tests at the same time and can retry one test alone. If the second test needs a note that only the first test types, the file breaks as soon as the order changes.
๐ช๐ต๐ ๐๐ผ ๐ช๐ฒ ๐ก๐ฒ๐ฒ๐ฑ ๐๐?
Opening a shop repeats the same first step: unlock the door. You would not write that step only on the first task and assume every later task finds the door open. beforeEach is that repeated first step. Each scenario still has its own expected result.
Order dependence feels convenient and then breaks. Someone runs one test from the file, and the next test looks for data the first test never created. The failure looks like a product bug. It is a test design bug. Shared setup belongs in a hook or inside the test that needs it.
Implementation
The smallest group opens the page once per test:
test.beforeEach(๐ข๐ด๐บ๐ฏ๐ค ({ ๐ฑ๐ข๐จ๐ฆ }) => {
await page.goto('/desk');
});
goto runs before each test in the group, so neither test assumes the other one navigated.
The practical file adds cleanup and two independent checks.
test.afterEach(๐ข๐ด๐บ๐ฏ๐ค ({ ๐ฑ๐ข๐จ๐ฆ }) =>
{
await page.getByLabel('Note').fill('');
});
test('shows the desk heading', ๐ข๐ด๐บ๐ฏ๐ค ({ ๐ฑ๐ข๐จ๐ฆ }) => {
await expect(page.getByRole('heading', { name: 'Orders desk' })).toBeVisible();
});
afterEach clears the note after every test.
Each test already gets its own page, so this field cannot leak by itself.
The hook still shows where cleanup belongs.
Cleanup matters more when tests share an account, a database row, or a file.
The second test types "Call the customer" and asserts that value.
It does not read a note created by the heading test.
The incorrect design would type the note in the first test and assert it in the second.
๐๐
๐ฝ๐ฒ๐ฐ๐๐ฒ๐ฑ ๐ฅ๐ฒ๐๐๐น๐
A passing run shows the heading "Orders desk" in one test and the note "Call the customer" in the other. Each test opens the desk through beforeEach. A failing run means the heading was missing or the field value differed. Either test can run alone.
๐ง๐ฎ๐ธ๐ฒ๐ฎ๐๐ฎ๐๐
test.describe() groups scenarios. It does not merge them into one long test.
beforeEach builds the shared start. afterEach cleans up, including after a failure.
Do not make one test's result depend on another test's actions.
Each test gets its own page. Shared application data still needs cleanup.
๐๐ป๐๐ฒ๐ฟ๐๐ถ๐ฒ๐ ๐พ๐๐ฒ๐๐๐ถ๐ผ๐ป: Why is it risky to chain tests so that one prepares the next?
๐๐ป๐๐๐ฒ๐ฟ: Playwright can run or retry a single test. The later test then fails because its setup never ran. Put shared setup in a hook or in the test that needs it.
Join the TechNexuz WhatsApp Community to discuss tech, test automation, and QA strategies: https://chat.whatsapp.com/HPaOvG8XfDWAotDX2mHfFM
#QA #QAEngineer #QualityAssurance #SoftwareTesting #SQA #TestAutomation #AutomationTesting #SDET #QAAutomation #Playwright #TypeScript #WebAutomation #APITesting #TestFramework #AITesting #SoftwareQuality

0 likes ยท 0 views ยท Nov 28, 2026
๐ช๐ต๐ฎ๐ ๐๐ ๐ฎ ๐๐ฒ๐๐ฐ๐ฟ๐ถ๐ฏ๐ฒ ๐๐น๐ผ๐ฐ๐ธ? test.describe() groups related tests under one name. The tests inside it still run as separate cases in this le...