Rayrun

How can I programmatically retrieve the title of each test group in Playwright Test?

Answer

Retrieving Test Group Titles in Playwright Test

You can use the test.describe() function in Playwright Test to declare a test group. The title parameter within the callback of test.describe() represents the title of the test group. Here's an example:

import { test } from '@playwright/test';

test.describe('Group 1', () => {
  // Tests for Group 1
});

test.describe('Group 2', () => {
  // Tests for Group 2
});

In this example, "Group 1" and "Group 2" are the titles of the test groups.

To access these titles programmatically, use the suite.titlePath() method. This method returns an array of titles from the root down to a specific suite (a test group). Here's how you can retrieve each test group's title:

import { suite } from '@playwright/test';

// Get all suites in your project
const allSuites = suite.allSuites();

// Iterate over each suite and retrieve its title path
for (const currentSuite of allSuites) {
    const titles = currentSuite.titlePath();
    const groupName = titles.join(' > ');
    
    console.log(`Test Group Title: ${groupName}`);
}

In this code, we first get all suites using suite.allSuites(). Then we iterate over each suite and retrieve its title path using currentSuite.titlePath(). The titles are joined together with the separator " > " to form the complete test group title. Finally, we log each test group's title to the console.

By following these steps, you can programmatically retrieve each test group's title in Playwright Test.

References

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.