🎭 Running Playwright Tests: Your Backstage Pass to Test Execution
The Big Picture: You’re a Theater Director! 🎬
Imagine you’re the director of a theater. You have many actors (tests) ready to perform different scenes (test files). Sometimes you want:
- The whole show — run all tests
- Just one scene — run a specific file
- One actor’s part — run tests by name
- A themed act — run tests by tag
- Second chances — retry if someone forgets their lines!
Playwright gives you the director’s remote control to decide exactly what plays on stage!
🏃 Running Tests: The Full Show
When you want to run ALL your tests — the entire theater production — you simply use:
npx playwright test
What happens?
- Playwright finds every
.spec.tsor.spec.jsfile - Runs them all, one by one (or in parallel!)
- Shows you who passed ✅ and who failed ❌
Think of it like: Pressing “PLAY ALL” on your music playlist. Every song plays!
Quick Example:
# Your tests folder has:
# tests/login.spec.ts
# tests/signup.spec.ts
# tests/checkout.spec.ts
npx playwright test
# Runs ALL three test files! 🎉
📁 Running Specific Test Files: Just One Scene
Sometimes you don’t need the whole show. Maybe you only want to test the login page.
npx playwright test login.spec.ts
Or multiple specific files:
npx playwright test login.spec.ts signup.spec.ts
Think of it like: Telling Netflix “Just play Episode 3!” instead of the whole season.
Real Example:
# Only test the checkout flow
npx playwright test tests/checkout.spec.ts
# Test both login AND signup
npx playwright test login.spec.ts signup.spec.ts
Pro Tip: You can use folder paths too!
# Run all tests in the "auth" folder
npx playwright test tests/auth/
🏷️ Running Tests by Name: Find Your Star Actor
What if you have 50 tests but only want to run ones about “password”? Use the -g flag (g = grep = search)!
npx playwright test -g "password"
This runs any test whose name contains “password”:
- ✅ “user can reset password”
- ✅ “password must be 8 characters”
- ❌ “user can login” (doesn’t have “password”)
Think of it like: Searching for “pizza” in your food delivery app — only pizza restaurants show up!
Examples:
# Run tests with "login" in the name
npx playwright test -g "login"
# Run tests about "shopping cart"
npx playwright test -g "shopping cart"
# Case doesn't matter!
npx playwright test -g "LOGIN" # Same result
🎯 Running Tests by Tag: Organize with Labels
Tags are like stickers you put on your tests. Some tests are @fast, some are @slow, some are @critical.
Step 1: Add Tags to Your Tests
test('user can login @smoke @critical', async () => {
// test code here
});
test('user can upload large file @slow', async () => {
// test code here
});
Step 2: Run by Tag
# Run only @smoke tests
npx playwright test --grep "@smoke"
# Run only @critical tests
npx playwright test --grep "@critical"
# Skip @slow tests
npx playwright test --grep-invert "@slow"
Think of it like: Your closet has labels — “Work Clothes”, “Gym Clothes”, “Party Clothes”. You pick what to wear based on the label!
Combining Tags:
# Run tests that are BOTH @smoke AND @fast
npx playwright test --grep "@smoke" --grep "@fast"
🔄 Test Retries: Second Chances!
Sometimes tests fail because of flaky behavior — like a slow network or a button that didn’t load yet. Retries give your test another chance!
Method 1: Command Line Retry
# Retry failed tests up to 2 times
npx playwright test --retries=2
What happens:
- Test runs → Fails ❌
- Playwright waits → Tries again (Retry 1)
- Still fails? → Tries again (Retry 2)
- Passes on retry? → Counts as passed! ✅
Method 2: Config File Retry
// playwright.config.ts
export default {
retries: 2, // All tests get 2 retries
};
Method 3: Per-Test Retry
test('flaky network test', async () => {
test.retries(3); // This test gets 3 retries
// test code
});
Think of it like: A video game giving you 3 lives. If you fail, you respawn and try again!
🗺️ The Command Cheatsheet
graph LR A["🎭 Run Tests"] --> B["All Tests"] A --> C["Specific Files"] A --> D["By Name"] A --> E["By Tag"] A --> F["With Retries"] B --> B1["npx playwright test"] C --> C1["npx playwright test file.spec.ts"] D --> D1["npx playwright test -g 'name'"] E --> E1["npx playwright test --grep '@tag'"] F --> F1["npx playwright test --retries=2"]
🎮 Quick Reference Table
| What You Want | Command | Example |
|---|---|---|
| Run ALL tests | npx playwright test |
Runs everything |
| Run ONE file | npx playwright test [file] |
npx playwright test login.spec.ts |
| Run by NAME | npx playwright test -g "[text]" |
npx playwright test -g "password" |
| Run by TAG | npx playwright test --grep "@[tag]" |
npx playwright test --grep "@smoke" |
| SKIP a tag | npx playwright test --grep-invert "@[tag]" |
npx playwright test --grep-invert "@slow" |
| RETRY fails | npx playwright test --retries=[n] |
npx playwright test --retries=2 |
🌟 Real-World Scenarios
Scenario 1: Morning Quick Check
# Run only critical smoke tests
npx playwright test --grep "@smoke"
Scenario 2: Before Deploy
# Run everything with retries for flaky tests
npx playwright test --retries=2
Scenario 3: Debugging One Test
# Run just the test you're working on
npx playwright test -g "user can add item to cart"
Scenario 4: Avoid Slow Tests
# Skip slow tests during development
npx playwright test --grep-invert "@slow"
💡 Key Takeaways
npx playwright test= Run everything (full show)npx playwright test [file]= Run specific file (one scene)-g "name"= Search by test name (find your star)--grep "@tag"= Filter by tag (organize with labels)--retries=N= Give failing tests second chances (extra lives)
🎉 You’re Now a Test Director!
You’ve learned how to control your Playwright tests like a pro director:
- 🎬 Run the full production
- 🎭 Pick specific scenes
- 🔍 Find actors by name
- 🏷️ Organize with tags
- 🔄 Grant retries for flaky performers
Go forth and run your tests with confidence! 🚀
