Sunday, March 30, 2025

How do I access playwright test runner project info in a test?

testInfo is part of Playwright's testing framework and provides information about the test that is currently running. You can use testInfo to access various details about the test, such as its name, status, duration, and even metadata.

How do I access playwright test runner project info in a test?


Here's an example of how you can use testInfo in a Playwright test:

Example 1 :
// @ts-check
import { test, expect } from '@playwright/test';

test('demo for test info', async ({page}, testInfo) => {

  await page.goto('https://www.google.com/');
  await page.locator("[name='q']").fill("skptricks");
  await page.keyboard.press('Enter');

  console.log('Test name:', testInfo.title);  // Get the name of the test
  console.log('Test status:', testInfo.status);  // Get the status of the test (passed/failed)
  console.log('Test duration:', testInfo.duration);  // Get the duration of the test in milliseconds
  console.log('Test duration:', testInfo.annotations);  //Any annotations attached to the test.   
});

Output :
npx playwright test test-info.spec.js --project=chromium

Running 1 test using 1 worker
[chromium]  tests\test-info.spec.js:4:6  demo for test info
Test name: demo for test info
Test status: passed                                                                                                                                      
Test duration: 0                                                                                                                                         
Test duration: []                                                                                                                                        
  1 passed (3.3s)

To open last HTML report run:

  npx playwright show-report

testInfo provides:
  1. title: The name or title of the test.
  2. status: The current status of the test ('passed', 'failed', or 'skipped').
  3. duration: The duration of the test execution in milliseconds.
  4. annotations: Any annotations attached to the test.

Example 2 :

Playwright config file :

How do I access playwright test runner project info in a test?


// @ts-check
import { test, expect } from '@playwright/test';

test('test info to access testconfig parameters', async ({page}, testInfo) => {

    await page.goto('https://www.google.com/');
    await page.locator("[name='q']").fill("skptricks");
    await page.keyboard.press('Enter');
  
    const projectsName = testInfo.project.name;
    expect(projectsName).toEqual('chromium');
    console.log('Project name:', projectsName);  // Get project name    
  });

Output:
npx playwright test test-info.spec.js --project=chromium

Running 1 test using 1 worker
[chromium]  tests\test-info.spec.js:16:6  test info to access testconfig parameters
Project name: chromium
  1 passed (6.1s)

To open last HTML report run:

  npx playwright show-report

You can use testInfo for logging, debugging, or reporting purposes within your tests.



No comments:

Post a Comment