
Cypress vs. Playwright: Mastering Test Execution with Tags & Filters
This article explores how to control test execution in Cypress and Playwright using tags and filters. Learn how to leverage these features to streamline your testing process and improve efficiency by running specific subsets of tests.
What's Inside: Cypress vs. Playwright Test Control (Part 2)
This guide dives into the world of test tags and filters in Cypress and Playwright, including practical plugins to enhance their functionality. Understanding these features is crucial for efficient test management in your projects.
Tags: Categorize and Conquer Your Tests
Tags are essential for efficient organization and management of tests. They allow you to categorize tests based on various criteria such as test type (smoke, regression), priority, or specific functionality. By using tags effectively, you can enhance the flexibility, readability, and maintainability of your testing framework.
Common Uses of Tags:
- Test Selection: Run specific subsets of tests based on certain criteria.
- Filtering: Include or exclude tests with certain tags during execution (useful in CI/CD pipelines).
- Organization: Organize tests into categories for better understanding and scope management.
- Reporting: Generate detailed reports highlighting specific areas of the software being tested.
Cypress Tags: Plugin Power to the Rescue
Cypress doesn't natively support tags. But don't worry, @bahmutov/cy-grep
is a great Cypress plugin by Gleb Bahmutov to adds tag support.
Why Use @bahmutov/cy-grep
?
- Frequent Updates: Gleb actively maintains the plugin, ensuring dependencies are up-to-date.
- Rapid Issue Resolution: Gleb quickly addresses reported issues.
- Community Recognition: Gleb is well-known and respected in the Cypress community.
- Real-time Grep: Ability to perform grep operations directly from the browser's DevTools console using the Cypress.grep() command when running your tests in 'open' mode.
- Failed tests Grep: Ability to run just the failed tests from the DevTools console using Cypress.grepFailed() command when running your tests in 'open' mode.
Installing @bahmutov/cy-grep
: A Step-by-Step Guide
-
Install:
npm i -D @bahmutov/cy-grep
-
Register in
e2e.js
: -
Load in
cypress.config.js
: -
Configure Environment Variables: Set
grepFilterSpecs
andgrepOmitFiltered
totrue
incypress.config.js
.
Using Tags in Cypress
Tags can be created at the group test level ( describe
or context
) and the test level ( it
) by passing an object with a tags property as the function's second parameter.
It is very common to use the character @
at the beginning of the tag name for clarity (like @smoke
).
Playwright Tags: Native Tagging for the Win
Playwright has built-in support for tags. A tag in Playwright must start with the character @
. You set tags using the tag property in the details parameter when defining a test.
Tags also can be created at the group test level ( test.describe
):
Test Filters: Precision Targeting in Cypress and Playwright
Filtering tests by tags, spec files, and test titles allows precise control over which tests to execute. This feature enhances efficiency by enabling QA Engineers to target specific tests relevant to their current work.
Cypress Test Filters:
Cypress can filter using:
- Spec files (natively supported).
- Test titles (using
@bahmutov/cy-grep
). - Tags (using
@bahmutov/cy-grep
).
Filter by Spec Files
Filter by Test Title (with @bahmutov/cy-grep
)
Filter by Tags (with @bahmutov/cy-grep
)
Playwright Test Filters:
Playwright supports filtering by:
- Spec files (natively supported - implicit, by indicating the file).
- Test titles (natively supported with
--grep
). - Tags (natively supported with
--grep
).