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.annotations
- testInfo.attach(name[, options])
- testInfo.attachments
- testInfo.column
- testInfo.config
- testInfo.duration
- testInfo.error
- testInfo.errors
- testInfo.expectedStatus
- testInfo.fail()
- testInfo.fail(condition[, description])
- testInfo.file
- testInfo.fixme()
- testInfo.fixme(condition[, description])
- testInfo.fn
- testInfo.line
- testInfo.outputDir
- testInfo.outputPath(...pathSegments)
- testInfo.parallelIndex
- testInfo.project
- testInfo.repeatEachIndex
- testInfo.retry
- testInfo.setTimeout(timeout)
- testInfo.skip()
- testInfo.skip(condition[, description])
- testInfo.slow()
- testInfo.slow(condition[, description])
- testInfo.snapshotDir
- testInfo.snapshotPath(...pathSegments)
- testInfo.snapshotSuffix
- testInfo.status
- testInfo.stderr
- testInfo.stdout
- testInfo.timeout
- testInfo.title
- testInfo.titlePath
- testInfo.workerIndex
testInfo.attach(name[, options])
Added in: v1.10name
<string> Attachment name.#options?
<Object>body?
<string|Buffer> Attachment body. Mutually exclusive withpath
.#contentType?
<string> Content type of this attachment to properly present in the report, for example'application/json'
or'image/png'
. If omitted, content type is inferred based on thepath
, or defaults totext/plain
for string attachments andapplication/octet-stream
for Buffer attachments.#path?
<string> Path on the filesystem to the attached file. Mutually exclusive withbody
.#
- returns:Promise<void>># <
Attach a value or a file from disk to the current test. Some reporters show test attachments. Either path
or body
must be specified, but not both.
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');
const screenshot = await page.screenshot();
await testInfo.attach('screenshot', { body: screenshot, contentType: 'image/png' });
});
const { test, expect } = require('@playwright/test');
test('basic test', async ({ page }, testInfo) => {
await page.goto('https://playwright.dev');
const screenshot = await page.screenshot();
await testInfo.attach('screenshot', { body: screenshot, contentType: 'image/png' });
});
Or you can attach files returned by your APIs:
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test('basic test', async ({}, testInfo) => {
const { download } = require('./my-custom-helpers');
const tmpPath = await download('a');
await testInfo.attach('downloaded', { path: tmpPath });
});
const { test, expect } = require('@playwright/test');
test('basic test', async ({}, testInfo) => {
const { download } = require('./my-custom-helpers');
const tmpPath = await download('a');
await testInfo.attach('downloaded', { path: tmpPath });
});
testInfo.attach(name[, options]) automatically takes care of copying attached files to a location that is accessible to reporters. You can safely remove the attachment after awaiting the attach call.
testInfo.fail()
Added in: v1.10Marks the currently running test as "should fail". Playwright Test runs this test and ensures that it is actually failing. This is useful for documentation purposes to acknowledge that some functionality is broken until it is fixed. This is similar to test.fail().
testInfo.fail(condition[, description])
Added in: v1.10condition
<boolean> Test is marked as "should fail" when the condition istrue
.#description?
<string> Optional description that will be reflected in a test report.#- returns:void># <
Conditionally mark the currently running test as "should fail" with an optional description. This is similar to test.fail(condition[, description]).
testInfo.fixme()
Added in: v1.10Mark a test as "fixme", with the intention to fix it. Test is immediately aborted. This is similar to test.fixme().
testInfo.fixme(condition[, description])
Added in: v1.10condition
<boolean> Test is marked as "fixme" when the condition istrue
.#description?
<string> Optional description that will be reflected in a test report.#- returns:void># <
Conditionally mark the currently running test as "fixme" with an optional description. This is similar to test.fixme(condition[, description]).
testInfo.outputPath(...pathSegments)
Added in: v1.10...pathSegments
<Array<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');
});
Note that
pathSegments
accepts path segments to the test output directory such astestInfo.outputPath('relative', 'path', 'to', 'output')
. However, this path must stay within the testInfo.outputDir directory for each test (i.e.test-results/a-test-title
), otherwise it will throw.
testInfo.setTimeout(timeout)
Added in: v1.10Changes the timeout for the currently running test. Zero means no timeout. Learn more about various timeouts.
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()
Added in: v1.10Unconditionally skip the currently running test. Test is immediately aborted. This is similar to test.skip().
testInfo.skip(condition[, description])
Added in: v1.10condition
<boolean> A skip condition. Test is skipped when the condition istrue
.#description?
<string> Optional description that will be reflected in a test report.#- returns:void># <
Conditionally skips the currently running test with an optional description. This is similar to test.skip(condition[, description]).
testInfo.slow()
Added in: v1.10Marks the currently running test as "slow", giving it triple the default timeout. This is similar to test.slow().
testInfo.slow(condition[, description])
Added in: v1.10condition
<boolean> Test is marked as "slow" when the condition istrue
.#description?
<string> Optional description that will be reflected in a test report.#- returns:void># <
Conditionally mark the currently running test as "slow" with an optional description, giving it triple the default timeout. This is similar to test.slow(condition[, description]).
testInfo.snapshotPath(...pathSegments)
Added in: v1.10...pathSegments
<Array<string>> The name of the snapshot or the path segments to define the snapshot file path. Snapshots with the same name in the same test file are expected to be the same.#- returns:string># <
Returns a path to a snapshot file with the given pathSegments
. Learn more about snapshots.
Note that
pathSegments
accepts path segments to the snapshot file such astestInfo.snapshotPath('relative', 'path', 'to', 'snapshot.png')
. However, this path must stay within the snapshots directory for each test file (i.e.a.spec.js-snapshots
), otherwise it will throw.
testInfo.annotations
Added in: v1.10The 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
Added in: v1.10The list of files or buffers attached to the current test. Some reporters show test attachments.
To add an attachment, use testInfo.attach(name[, options]) instead of directly pushing onto this array.
testInfo.column
Added in: v1.10- type: <number>
Column number where the currently running test is declared.
testInfo.config
Added in: v1.10- type: <TestConfig>
Processed configuration from the configuration file.
testInfo.duration
Added in: v1.10- type: <number>
The number of milliseconds the test took to finish. Always zero before the test finishes, either successfully or not. Can be used in test.afterEach(hookFunction) hook.
testInfo.error
Added in: v1.10- type: <TestError>
First error thrown during test execution, if any. This is equal to the first element in testInfo.errors.
testInfo.errors
Added in: v1.10Errors thrown during test execution, if any.
testInfo.expectedStatus
Added in: v1.10- type: <"passed"|"failed"|"timedOut"|"skipped"|"interrupted">
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().
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
Added in: v1.10- type: <string>
Absolute path to a file where the currently running test is declared.
testInfo.fn
Added in: v1.10- type: <function>
Test function as passed to test(title, testFunction)
.
testInfo.line
Added in: v1.10- type: <number>
Line number where the currently running test is declared.
testInfo.outputDir
Added in: v1.10- 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.parallelIndex
Added in: v1.10- type: <number>
The index of the worker between 0
and workers - 1
. It is guaranteed that workers running at the same time have a different parallelIndex
. When a worker is restarted, for example after a failure, the new worker process has the same parallelIndex
.
Also available as process.env.TEST_PARALLEL_INDEX
. Learn more about parallelism and sharding with Playwright Test.
testInfo.project
Added in: v1.10- type: <TestProject>
Processed project configuration from the configuration file.
testInfo.repeatEachIndex
Added in: v1.10- 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
Added in: v1.10- 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.
- TypeScript
- JavaScript
import { test, expect } from '@playwright/test';
test.beforeEach(async ({}, testInfo) => {
// You can access testInfo.retry in any hook or fixture.
if (testInfo.retry > 0)
console.log(`Retrying!`);
});
test('my test', async ({ page }, testInfo) => {
// Here we clear some server-side state when retrying.
if (testInfo.retry)
await cleanSomeCachesOnTheServer();
// ...
});
const { test, expect } = require('@playwright/test');
test.beforeEach(async ({}, testInfo) => {
// You can access testInfo.retry in any hook or fixture.
if (testInfo.retry > 0)
console.log(`Retrying!`);
});
test('my test', async ({ page }, testInfo) => {
// Here we clear some server-side state when retrying.
if (testInfo.retry)
await cleanSomeCachesOnTheServer();
// ...
});
testInfo.snapshotDir
Added in: v1.10- type: <string>
Absolute path to the snapshot output directory for this specific test. Each test suite gets its own directory so they cannot conflict.
testInfo.snapshotSuffix
Added in: v1.10- 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
Added in: v1.10- type: <"passed"|"failed"|"timedOut"|"skipped"|"interrupted">
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.stderr
Added in: v1.10Output written to process.stderr
or console.error
during the test execution.
testInfo.stdout
Added in: v1.10Output written to process.stdout
or console.log
during the test execution.
testInfo.timeout
Added in: v1.10- type: <number>
Timeout in milliseconds for the currently running test. Zero means no timeout. Learn more about various timeouts.
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
Added in: v1.10- type: <string>
The title of the currently running test as passed to test(title, testFunction)
.
testInfo.titlePath
Added in: v1.10The full title path starting with the project.
testInfo.workerIndex
Added in: v1.10- type: <number>
The unique index of the worker process that is running the test. When a worker is restarted, for example after a failure, the new worker process gets a new unique workerIndex
.
Also available as process.env.TEST_WORKER_INDEX
. Learn more about parallelism and sharding with Playwright Test.