Adding Tags to Test Descriptions
In Playwright, you can add tags to your test descriptions using the @tag syntax. While you can technically use any string as a tag, it's recommended to stick with the @tag convention for consistency. Here's an example of how to add tags to a test description:
test('user can login @smoke @login', async ({ page }) => {
// Test implementation goes here
});
Running Tests with Specific Tags
Playwright provides the --grep and --grep-invert command-line flags to run tests based on their tags. The --grep flag allows you to run tests that match a specific tag pattern, while --grep-invert lets you exclude tests that match the pattern. Here are some examples of how to run tests with specific tags:
# Run tests with the @smoke tag
npx playwright test --grep "@smoke"
# Run tests with the @login tag, excluding those with the @smoke tag
npx playwright test --grep "@login" --grep-invert "@smoke"
Combining Tags for Complex Test Selection
In addition to using single tags, you can also combine multiple tags to create more complex test selection criteria. Here are some examples of how to do this:
# Run tests with either the @smoke or @login tag (logical OR)
npx playwright test --grep "@smoke|@login"
# Run tests with both the @smoke and @login tags (logical AND)
npx playwright test --grep "(?=.*@smoke)(?=.*@login)"
These examples are taken from Organizing Playwright Tests using Tags