Skip to main content

Grouping Test

 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.

typescript
import { 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.

typescript
import { 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.

typescript
import { 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.

typescript
import { 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

Popular posts from this blog

Get OTP from email

/** * Retrieves an OTP from Gmail in a headless tab. * @param {Object} options - The options for the function. * @param {string} options.email - The Gmail email address. * @param {string} options.password - The Gmail password. * @returns {Promise<string | null>} The OTP retrieved from the email. */ export async function getOtpFromGmail ({ email , password , } : { email : string ; password : string ; }) : Promise < string | null > { const { chromium } = require ( "playwright" ); // Launch a headless browser context for Gmail login const browser = await chromium . launch ({ headless : true }); const context = await browser . newContext (); const page = await context . newPage (); await page . goto ( "https://mail.google.com" ); const emailInput = await page . waitForSelector ( "#identifierId" ); await emailInput . fill ( email ); const emailNextButton = await page . wa...

Playwright Solution

 There is a web site need to run with browser. and which has zero downtime and alert and notifications pop up too. Third party applications are involved in and need to fix bugs too.What is the solution and how to create a framework for this scenario : need to use playwright as well 1. Understand the Requirements and Challenges: Zero Downtime: Ensure tests do not interrupt the service. Alerts and Notifications: Handle unexpected pop-ups gracefully. Third-Party Integrations: Test interactions with third-party services. Bug Fixing: Implement a process to identify and log bugs efficiently. 2. Set Up the Playwright Framework: Installation: Ensure Playwright is installed and configured. bash Copy code npm install playwright 3. Structure the Test Framework: Test Suites: Organize tests into suites (e.g., smoke tests, regression tests, integration tests). Page Object Model (POM): Use POM to manage page elements and actions. Configuration: Set up configurations for different envir...

Challenges

 One of the challenging situations I faced was identifying the correct locators for elements in our Angular application. Despite using developer tools like Chrome DevTools, it was often difficult to pinpoint the exact locators due to the complexity of the application and the dynamic nature of Angular's rendering. To overcome this, I frequently had to review the application's code in Bitbucket to understand the structure and find reliable locators. This approach allowed me to write more robust and reliable tests. Additionally, I communicated with developers to gain insights and confirm my findings, which helped streamline the process." As our application grew, the number of test scripts also increased, making the scripts larger and more chaotic. This was especially true for our end-to-end tests using Playwright and TypeScript. The test scripts became hard to manage and maintain because they contained both the test logic and the test data, leading to a lot of redundancy and ...