TestInfo
TestInfo
contains information about currently running test. It is available to any test function, test.beforeEach(hookFunction) and test.afterEach(hookFunction) hooks and test-scoped fixtures. TestInfo
provides utilities to control test execution: attach files, update test timeout, determine which test is currently running and whether it was retried, etc.
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }, testInfo) => { expect(testInfo.title).toBe('basic test'); await page.screenshot(testInfo.outputPath('screenshot.png'));});
const { test, expect } = require('@playwright/test');
test('basic test', async ({ page }, testInfo) => { expect(testInfo.title).toBe('basic test'); await page.screenshot(testInfo.outputPath('screenshot.png'));});
- testInfo.fail([condition, description])
- testInfo.fixme([condition, description])
- testInfo.outputPath(pathSegments)
- testInfo.setTimeout(timeout)
- testInfo.skip([condition, description])
- testInfo.slow([condition, description])
- testInfo.snapshotPath(snapshotName)
- testInfo.annotations
- testInfo.attachments
- testInfo.column
- testInfo.config
- testInfo.duration
- testInfo.error
- testInfo.expectedStatus
- testInfo.file
- testInfo.fn
- testInfo.line
- testInfo.outputDir
- testInfo.project
- testInfo.repeatEachIndex
- testInfo.retry
- testInfo.snapshotSuffix
- testInfo.status
- testInfo.stderr
- testInfo.stdout
- testInfo.timeout
- testInfo.title
- testInfo.workerIndex
#
testInfo.fail([condition, description])condition
<void|boolean> Optional condition - the test is marked as "should fail" when the condition istrue
.#description
<void|string> Optional description that will be reflected in a test report.#- returns:void># <
Marks the currently running test as "should fail". Playwright Test ensures that this test is actually failing. This is similar to test.fail([condition, description]).
#
testInfo.fixme([condition, description])condition
<void|boolean> Optional condition - the test is marked as "fixme" when the condition istrue
.#description
<void|string> Optional description that will be reflected in a test report.#- returns:void># <
Marks the currently running test as "fixme". The test will be skipped, but the intention is to fix it. This is similar to test.fixme([condition, description]).
#
testInfo.outputPath(pathSegments)pathSegments
<[string...]> Path segments to append at the end of the resulting path.#- returns:string># <
Returns a path inside the testInfo.outputDir where the test can safely put a temporary file. Guarantees that tests running in parallel will not interfere with each other.
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';import fs from 'fs';
test('example test', async ({}, testInfo) => { const file = testInfo.outputPath('dir', 'temporary-file.txt'); await fs.promises.writeFile(file, 'Put some data to the dir/temporary-file.txt', 'utf8');});
const { test, expect } = require('@playwright/test');const fs = require('fs');
test('example test', async ({}, testInfo) => { const file = testInfo.outputPath('dir', 'temporary-file.txt'); await fs.promises.writeFile(file, 'Put some data to the dir/temporary-file.txt', 'utf8');});
#
testInfo.setTimeout(timeout)Changes the timeout for the currently running test. Zero means no timeout.
Timeout is usually specified in the configuration file, but it could be useful to change the timeout in certain scenarios:
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }, testInfo) => { // Extend timeout for all tests running this hook by 30 seconds. testInfo.setTimeout(testInfo.timeout + 30000);});
const { test, expect } = require('@playwright/test');
test.beforeEach(async ({ page }, testInfo) => { // Extend timeout for all tests running this hook by 30 seconds. testInfo.setTimeout(testInfo.timeout + 30000);});
#
testInfo.skip([condition, description])condition
<void|boolean> Optional condition - the test is skipped when the condition istrue
.#description
<void|string> Optional description that will be reflected in a test report.#- returns:void># <
Skips the currently running test. This is similar to test.skip().
#
testInfo.slow([condition, description])condition
<void|boolean> Optional condition - the test is marked as "slow" when the condition istrue
.#description
<void|string> Optional description that will be reflected in a test report.#- returns:void># <
Marks the currently running test as "slow", giving it triple the default timeout. This is similar to test.slow([condition, description]).
#
testInfo.snapshotPath(snapshotName)Returns a path to a snapshot file with the given snapshotName
. Learn more about snapshots.
#
testInfo.annotationsThe list of annotations applicable to the current test. Includes annotations from the test, annotations from all test.describe(title, callback) groups the test belongs to and file-level annotations for the test file.
Learn more about test annotations.
#
testInfo.attachments- type: <Array<Object>>
name
<string> Attachment name.contentType
<string> Content type of this attachment to properly present in the report, for example'application/json'
or'image/png'
.path
<void|string> Optional path on the filesystem to the attached file.body
<void|Buffer> Optional attachment body used instead of a file.
The list of files or buffers attached to the current test. Some reporters show test attachments. For example, you can attach a screenshot to the test.
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test('basic test', async ({ page }, testInfo) => { await page.goto('https://playwright.dev');
// Capture a screenshot and attach it. const path = testInfo.outputPath('screenshot.png'); await page.screenshot({ path }); testInfo.attachments.push({ name: 'screenshot', path, contentType: 'image/png' });});
const { test, expect } = require('@playwright/test');
test('basic test', async ({ page }, testInfo) => { await page.goto('https://playwright.dev');
// Capture a screenshot and attach it. const path = testInfo.outputPath('screenshot.png'); await page.screenshot({ path }); testInfo.attachments.push({ name: 'screenshot', path, contentType: 'image/png' });});
#
testInfo.column- type: <number>
Column number where the currently running test is declared.
#
testInfo.config- type: <TestConfig>
Processed configuration from the configuration file.
#
testInfo.duration- type: <number>
The number of milliseconds the test took to finish. Always zero before the test finishes, either successfully or not.
#
testInfo.error- type: <Object>
An error thrown during test execution, if any.
#
testInfo.expectedStatus- type: <"passed"|"failed"|"timedOut"|"skipped">
Expected status for the currently running test. This is usually 'passed'
, except for a few cases:
'skipped'
for skipped tests, e.g. with test.skip();'failed'
for tests marked as failed with test.fail([condition, description]).
Expected status is usually compared with the actual testInfo.status:
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test.afterEach(async ({}, testInfo) => { if (testInfo.status !== testInfo.expectedStatus) console.log(`${testInfo.title} did not run as expected!`);});
const { test, expect } = require('@playwright/test');
test.afterEach(async ({}, testInfo) => { if (testInfo.status !== testInfo.expectedStatus) console.log(`${testInfo.title} did not run as expected!`);});
#
testInfo.file- type: <string>
Absolute path to a file where the currently running test is declared.
#
testInfo.fnTest function as passed to test(title, testFunction)
.
#
testInfo.line- type: <number>
Line number where the currently running test is declared.
#
testInfo.outputDir- type: <string>
Absolute path to the output directory for this specific test run. Each test run gets its own directory so they cannot conflict.
#
testInfo.project- type: <TestProject>
Processed project configuration from the configuration file.
#
testInfo.repeatEachIndex- type: <number>
Specifies a unique repeat index when running in "repeat each" mode. This mode is enabled by passing --repeat-each
to the command line.
#
testInfo.retry- type: <number>
Specifies the retry number when the test is retried after a failure. The first test run has testInfo.retry equal to zero, the first retry has it equal to one, and so on. Learn more about retries.
#
testInfo.snapshotSuffix- type: <string>
Suffix used to differentiate snapshots between multiple test configurations. For example, if snapshots depend on the platform, you can set testInfo.snapshotSuffix
equal to process.platform
. In this case expect(value).toMatchSnapshot(snapshotName)
will use different snapshots depending on the platform. Learn more about snapshots.
#
testInfo.status- type: <void|"passed"|"failed"|"timedOut"|"skipped">
Actual status for the currently running test. Available after the test has finished in test.afterEach(hookFunction) hook and fixtures.
Status is usually compared with the testInfo.expectedStatus:
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test.afterEach(async ({}, testInfo) => { if (testInfo.status !== testInfo.expectedStatus) console.log(`${testInfo.title} did not run as expected!`);});
const { test, expect } = require('@playwright/test');
test.afterEach(async ({}, testInfo) => { if (testInfo.status !== testInfo.expectedStatus) console.log(`${testInfo.title} did not run as expected!`);});
#
testInfo.stderrOutput written to process.stderr
or console.error
during the test execution.
#
testInfo.stdoutOutput written to process.stdout
or console.log
during the test execution.
#
testInfo.timeout- type: <number>
Timeout in milliseconds for the currently running test. Zero means no timeout. Timeout is usually specified in the configuration file
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }, testInfo) => { // Extend timeout for all tests running this hook by 30 seconds. testInfo.setTimeout(testInfo.timeout + 30000);});
const { test, expect } = require('@playwright/test');
test.beforeEach(async ({ page }, testInfo) => { // Extend timeout for all tests running this hook by 30 seconds. testInfo.setTimeout(testInfo.timeout + 30000);});
#
testInfo.title- type: <string>
The title of the currently running test as passed to test(title, testFunction)
.
#
testInfo.workerIndex- type: <number>
The unique index of the worker process that is running the test. Also available as process.env.TEST_WORKER_INDEX
. Learn more about parallelism and sharding with Playwright Test.