Organizing Playwright Tests with Describe Blocks and HooksOrganizing Playwright Tests with Describe Blocks and Hooks
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started
๐—ช๐—ต๐—ฎ๐˜ ๐—œ๐˜€ ๐—ฎ ๐——๐—ฒ๐˜€๐—ฐ๐—ฟ๐—ถ๐—ฏ๐—ฒ ๐—•๐—น๐—ผ๐—ฐ๐—ธ?
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
Post image
Back to feed
The network for creativity
Join 1.25M professional creatives like you
Connect with clients, get discovered, and run your business 100% commission-free
Creatives on Contra have earned over $150M and we are just getting started