How do I tag tests and run only the smoke tests in Playwright?
Written by Jatin Thacker, Automation Architect · Updated
test('title', { tag: '@smoke' }, async ({ page }) => { ... }), or put @smoke in the title. Then run npx playwright test --grep @smoke, and use --grep-invert @smoke to run everything else.This answer is one piece of Chapter 16: The Test Runner.
- Test Structure & Lifecycle
- playwright.config.ts Complete Reference
- Test Organization at Scale
- Tags, Grep, and the Suite You Actually Run
- What Runs Where, and What Serial Costs
- Practice and interview questions: Assertions & Test Runner
Tags let one suite serve several purposes. A small @smoke set can run on every push in a couple of minutes, while the full regression set runs nightly. Without tags, teams end up maintaining separate folders or separate configs for the same tests.
A tag must start with @. The cleaner way is the tag property in the test's details object, which takes one tag or an array, such as ['@cart', '@regression']. The other way is to write @smoke inside the title. A test.describe() block can take a tag too, which tags every test inside it.
--grep takes a regular expression, and in the run below --grep @smoke picked out both styles. --grep-invert does the opposite. You can combine tags in one pattern, for example --grep "@smoke|@cart".
The example has three tests: one tagged with the details object, one with two other tags, and one with @smoke in its title. --grep @smoke ran two of them, and the report printed the tag beside each title.
import { test, expect } from '@playwright/test';
test('catalog heading', { tag: '@smoke' }, async ({ page }) => {
await page.goto('/catalog.html?mock=static&seed=42&autologin=1');
await expect(page.locator('#grid-heading')).toHaveText('All products');
});
test('adding the grinder updates the total', { tag: ['@cart', '@regression'] }, async ({ page }) => {
await page.goto('/catalog.html?mock=static&seed=42&autologin=1');
await page.locator('article[data-sku="MRD-1003"]').getByRole('button', { name: 'Add to cart' }).click();
await expect(page.getByTestId('cart-total')).toHaveText('₹11,900');
});
test('login page signed out @smoke', async ({ page }) => {
await page.goto('/login.html?seed=42');
await expect(page.getByTestId('session-status')).toContainText('signed out');
});$ npx playwright test tags.spec.js --grep @smoke Running 2 tests using 1 worker ok 1 [chromium] › tags.spec.js:3:1 › catalog heading @smoke (394ms) ok 2 [chromium] › tags.spec.js:14:1 › login page signed out @smoke (370ms) 2 passed (2.0s)
test('checkout', { tag: 'smoke' }, ...). What is wrong?Related questions
AI can write the test now. The job is knowing whether it checks anything. That's what the course teaches, one chapter at a time.
163 lessons · 226 drills · start free