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:
- title: The name or title of the test.
- status: The current status of the test ('passed', 'failed', or 'skipped').
- duration: The duration of the test execution in milliseconds.
- annotations: Any annotations attached to the test.
Example 2 :
// @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