Rayrun
← Back to Discord Forum

How to get the test.describe() 's title?

I want to get each test groups's title like testInfo.title..

This thread is trying to answer question "How to get each test group's title like testInfo.title?"

2 replies

You can get it from testInfo.titlePath

@skorp32: i checked testInfo.titlePath() has each test's path : testInfo.titlePath[depth] Thank you so much!!!

i checked testInfo.titlePath() has each test's path : testInfo.titlePath[depth] Thank you so much!!!

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.

Related Discord Threads

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 [email protected].