Welcome back to my Playwright blog series! As someone who’s learning Playwright step by step , I’ve come to realize that keeping tests organized is just as important as writing them.
In this post, I’ll share how I started using describe
blocks to structure my tests better. If you’re also a beginner like me, this might help make your test files cleaner and easier to manage!
🧩 What I Struggled With
When my test file grew to 50+ tests, it looked like this:
test('Login valid', () => {});
test('Login invalid', () => {});
test('Add to cart', () => {});
test('Checkout', () => {});
//
// ...and 46 more!
I couldn't find anything quickly. Tests felt scattered like socks in a laundry room! 🧦
🎁 My Discovery: describe = Test Folders
Playwright's describe lets you group tests like drawers in a filing cabinet:
describe('Login Tests', () => {
// All login-related tests here
});
describe('Cart Tests', () => {
// All shopping cart tests here
});
✨ Why I Love describe (Beginner Benefits!)
🧪 My First describe Makeover
Before (Chaos):
test('Login valid', () => {});
test('Login invalid', () => {});
test('Forgot password', () => {});
After (Clarity):
describe('Login Feature', () => {
test('Valid credentials', () => {});
test('Invalid credentials', () => {});
test('Forgot password flow', () => {});
});
Terminal Output Now Shows:
Login Feature
✓ Valid credentials
✓ Invalid credentials
✓ Forgot password flow
So satisfying to see! 😌
🔄 Level-Up: Sharing Setup Steps
My biggest aha moment: Use beforeEach to avoid repeating code!
describe('Login Tests', () => {
// Runs before EVERY test in this group
beforeEach(async ({ page }) => {
await page.goto('/login'); // No more copy-pasting this line!
});
test('Valid login', () => {
// Already on login page!
});
test('Invalid login', () => {
// Already on login page!
});
});
📝 3 Simple Rules I Follow
1. Name groups clearly
describe('Checkout Flow', ...) > describe('Tests', ...)
2. One feature per group
Login ≠ Cart ≠ Profile
3.Start small
Even 2-3 tests deserve grouping!
🚀 Wrapping Up
Learning how to organize my tests with describe
was a small step that made a big difference in how I write and manage my Playwright tests. It helped me stay focused, reduce repetition, and understand my test suite better.
If you're also starting your Playwright journey, I highly recommend using describe
. It’s simple, beginner-friendly, and a great habit to build early on.
Thanks for reading — more Playwright discoveries coming soon! 😊
Comments
Post a Comment