In Playwright, you can tag and categorize your tests to better manage and filter them. This is particularly useful for organizing large test suites and running specific subsets of tests based on tags. Here’s a detailed explanation and examples:
Tagging Tests
You can tag tests in Playwright using either the test title or by providing an additional details object when declaring a test.
Tagging by Test Title
You can add tags directly in the test title using the @
symbol. Tags must start with the @
symbol.
typescriptimport { test, expect } from '@playwright/test';
test('Login functionality @smoke', async ({ page }) => {
// test code
});
test('Add to cart functionality @regression', async ({ page }) => {
// test code
});
test('Checkout process @regression @slow', async ({ page }) => {
// test code
});
Tagging with Details Object
You can also provide tags using an additional details object when declaring a test.
typescriptimport { test, expect } from '@playwright/test';
test('Login functionality', async ({ page }) => {
// test code
}).tag('@smoke');
test('Add to cart functionality', async ({ page }) => {
// test code
}).tag('@regression');
test('Checkout process', async ({ page }) => {
// test code
}).tag('@regression').tag('@slow');
Filtering Tests by Tags
You can filter tests by tags using the command line. This allows you to run only specific tests that match the given tags.
sh# Run all tests with @smoke tag
npx playwright test --grep @smoke
# Run all tests except those with @slow tag
npx playwright test --grep-invert @slow
# Run tests with either @smoke or @regression tag
npx playwright test --grep "@smoke|@regression"
Grouping Tests
You can group tests using the describe
function. This helps in logically organizing tests and scoping before
/after
hooks to the group.
typescriptimport { test, describe, expect } from '@playwright/test';
describe('User Management', () => {
test('Login functionality @smoke', async ({ page }) => {
// test code
});
test('Logout functionality @regression', async ({ page }) => {
// test code
});
});
describe('Shopping Cart', () => {
test('Add to cart functionality @regression', async ({ page }) => {
// test code
});
test('Remove from cart functionality @regression', async ({ page }) => {
// test code
});
});
Logical Names
Grouping tests using describe
gives them a logical name, which is a way to categorize tests under a common heading. This is useful for organizing tests by feature or functionality. The logical name is the name given to the describe
block.
In the example above, User Management
and Shopping Cart
are logical names that group related tests together.
Built-in Tags and Annotations
Playwright comes with a few built-in tags and annotations that you can use to manage your tests. Here are a few examples:
@slow
: Marks a test as slow.@fast
: Marks a test as fast.
You can also add custom tags like @smoke
, @regression
, @critical
, etc.
Custom Tags and Annotations
You can create your own tags and use them to categorize your tests. This helps in running specific sets of tests based on the tags.
typescriptimport { test, describe, expect } from '@playwright/test';
describe('Critical Path', () => {
test('Login functionality @critical @smoke', async ({ page }) => {
// test code
});
test('Checkout process @critical @slow', async ({ page }) => {
// test code
});
});
describe('Non-Critical Path', () => {
test('Add to cart functionality @regression', async ({ page }) => {
// test code
});
test('Search functionality @regression', async ({ page }) => {
// test code
});
});
Running Tests by Logical Group Name
When you group tests using describe
, you can run tests by the group name using the --grep
option:
sh# Run all tests in the 'Critical Path' group
npx playwright test --grep 'Critical Path'
Summary
- Tagging Tests: You can tag tests using the
@
symbol in the test title or by using a details object. - Filtering Tests: Use the
--grep
option to run specific tests based on tags. - Grouping Tests: Use the
describe
function to group tests logically and scope hooks. - Logical Names: Group names help in organizing tests by feature or functionality.
- Custom Tags: Create custom tags like
@smoke
,@regression
, etc., to categorize tests.
By tagging and grouping tests, you can better manage your test suite, run specific subsets of tests, and improve the readability and maintainability of your tests.
Comments
Post a Comment