new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Playwright · Test runner API · all subjects

test-api

157 notes in this subject, read out of this brain and free to use. This is page 1 of 3.

Location.file property

Location.file is a string property that contains the path to the source file where a test or suite is defined.

Location.line property

Location.line is an integer property that contains the line number in the source file where a test or suite is defined.

Location.column property

Location.column is an integer property that contains the column number in the source file where a test or suite is defined.

Location class represents test source code position

The Location class represents a location in the source code where a TestCase or Suite is defined. It has been available since v1.10.

TestInfo.file property

TestInfo.file is a string representing the absolute path to the file where the currently running test is declared.

TestInfo basic example

Example of accessing TestInfo in a test: ```js 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')); }); ```

TestInfo.status property

TestInfo.status is a TestStatus string with possible values: 'passed', 'failed', 'timedOut', 'skipped', 'interrupted', or null. It represents the actual status for the currently running test. It is available after the test has finished in Test.afterEach hook and fixtures. It is usually compared with TestInfo.expectedStatus.

TestInfo.config property

TestInfo.config is of type FullConfig and contains the processed configuration from the configuration file.

TestInfo.tags property

TestInfo.tags is an array of strings representing tags that apply to the test. Any changes made to this list while the test is running will not be visible to test reporters.

TestInfo.fail method - unconditional

TestInfo.fail() marks 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.timeout property

TestInfo.timeout is an integer representing the timeout in milliseconds for the currently running test. Zero means no timeout. Timeout is usually specified in the configuration file.

TestInfo.title property

TestInfo.title is a string representing the title of the currently running test as passed to test(title, testFunction).

TestInfo.expectedStatus vs TestInfo.status comparison

Example of comparing expectedStatus with status in an afterEach hook: ```js import { test, expect } from '@playwright/test'; test.afterEach(async ({}, testInfo) => { if (testInfo.status !== testInfo.expectedStatus) console.log(`${testInfo.title} did not run as expected!`); }); ```

TestInfo.attachments property

TestInfo.attachments is an array of objects with the following fields: name (string, attachment name), contentType (string, MIME type like 'application/json' or 'image/png'), path (optional string, filesystem path to the attached file), and body (optional Buffer, attachment body used instead of a file). The list shows files or buffers attached to the current test. Use TestInfo.attach instead of directly pushing onto this array.

TestInfo.duration property

TestInfo.duration is an integer representing the number of milliseconds the test took to finish. It is always zero before the test finishes, either successfully or not. Can be used in Test.afterEach hook.

TestInfo.testId property

TestInfo.testId is a string representing the test id that matches the test case id in the reporter API.

TestInfo.column property

TestInfo.column is an integer representing the column number where the currently running test is declared.

TestInfo.error property

TestInfo.error is of type TestInfoError or null. It contains the first error thrown during test execution, if any. This is equal to the first element in TestInfo.errors.

TestInfo.errors property

TestInfo.errors is an array of TestInfoError objects representing errors thrown during test execution, if any.

TestInfo.expectedStatus property

TestInfo.expectedStatus is a TestStatus string with possible values: 'passed', 'failed', 'timedOut', 'skipped', or 'interrupted'. It is usually 'passed', except for 'skipped' for skipped tests (e.g., with Test.skip) and 'failed' for tests marked as failed with Test.fail. It is usually compared with TestInfo.status.

TestInfo.snapshotPath method

TestInfo.snapshotPath returns a string path to a snapshot file with the given name. It accepts name as variadic string arguments for path segments and an optional kind option ('snapshot', 'screenshot', or 'aria'). kind defaults to 'snapshot'. When passing kind, multiple name segments are not supported.

TestInfo.fail method - conditional

TestInfo.fail(condition, description) conditionally marks the currently running test as 'should fail'. The condition parameter is a boolean (test is marked as 'should fail' when true). The description parameter is optional and will be reflected in a test report.

TestInfo.outputPath method

TestInfo.outputPath returns a string path inside TestInfo.outputDir where the test can safely put a temporary file. It guarantees that tests running in parallel will not interfere with each other. The method accepts pathSegments as variadic string arguments (path segments to append at the end of the resulting path). The resulting path must stay within TestInfo.outputDir directory for each test, otherwise it will throw.

TestInfo.fixme method - unconditional

TestInfo.fixme() marks a test as 'fixme', with the intention to fix it. The test is immediately aborted. This is similar to Test.fixme.

TestInfo.fixme method - conditional

TestInfo.fixme(condition, description) conditionally marks the currently running test as 'fixme'. The condition parameter is a boolean (test is marked as 'fixme' when true). The description parameter is optional and will be reflected in a test report.

TestInfo.titlePath property

TestInfo.titlePath is an array of strings representing the full title path starting with the test file name.

TestInfo.line property

TestInfo.line is an integer representing the line number where the currently running test is declared.

TestInfo.fn property

TestInfo.fn is a function type property containing the test function as passed to test(title, testFunction).

TestInfo.outputDir property

TestInfo.outputDir is a string representing the absolute path to the output directory for this specific test run. Each test run gets its own directory so they cannot conflict.

TestInfo.snapshotDir property

TestInfo.snapshotDir is a string representing the absolute path to the snapshot output directory for this specific test. Each test suite gets its own directory so they cannot conflict. This property does not account for the TestProject.snapshotPathTemplate configuration.

TestInfo.attach method - basic usage

TestInfo.attach is an async method that attaches a value or file from disk to the current test. Either path or body must be specified, but not both. The method automatically copies attached files to a location accessible to reporters and it is safe to remove the attachment after awaiting the attach call.

TestInfo.annotations property

TestInfo.annotations is an array of objects with the following fields: type (string, annotation type like 'skip' or 'fail'), description (optional string), and location (optional Location). The list includes annotations from the test, annotations from all Test.describe groups the test belongs to, and file-level annotations for the test file.

TestInfo.outputPath example

Example of using TestInfo.outputPath: ```js 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'); }); ```

TestInfo.project property

TestInfo.project is of type FullProject and contains the processed project configuration from the configuration file.

TestInfo.retry property

TestInfo.retry is an integer that specifies the retry number when the test is retried after a failure. The first test run has retry equal to zero, the first retry has it equal to one, and so on. Can be accessed in any hook or fixture.

TestInfo.retry example

Example of using TestInfo.retry: ```js 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(); // ... }); ```

TestInfo.setTimeout method

TestInfo.setTimeout(timeout) changes the timeout for the currently running test. The timeout parameter is an integer in milliseconds. Zero means no timeout. Timeout is usually specified in the configuration file, but can be changed in certain scenarios.

TestInfo.setTimeout example

Example of using TestInfo.setTimeout: ```js 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); }); ```

TestInfo.skip method - unconditional

TestInfo.skip() unconditionally skips the currently running test. The test is immediately aborted. This is similar to Test.skip.

TestInfo.skip method - conditional

TestInfo.skip(condition, description) conditionally skips the currently running test. The condition parameter is a boolean (test is skipped when true). The description parameter is optional and will be reflected in a test report.

TestInfo.slow method - unconditional

TestInfo.slow() marks the currently running test as 'slow', giving it triple the default timeout. This is similar to Test.slow.

TestInfo.slow method - conditional

TestInfo.slow(condition, description) conditionally marks the currently running test as 'slow' with an optional description, giving it triple the default timeout. The condition parameter is a boolean (test is marked as 'slow' when true). The description parameter is optional and will be reflected in a test report.

TestInfo class overview

TestInfo contains information about the currently running test. It is available to test functions, Test.beforeEach, Test.afterEach, Test.beforeAll, and Test.afterAll 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.

TestInfo.snapshotPath example

Example of using TestInfo.snapshotPath: ```js await expect(page).toHaveScreenshot('header.png'); // Screenshot assertion above expects screenshot at this path: const screenshotPath = test.info().snapshotPath('header.png', { kind: 'screenshot' }); await expect(page.getByRole('main')).toMatchAriaSnapshot({ name: 'main.aria.yml' }); // Aria snapshot assertion above expects snapshot at this path: const ariaSnapshotPath = test.info().snapshotPath('main.aria.yml', { kind: 'aria' }); expect('some text').toMatchSnapshot('snapshot.txt'); // Snapshot assertion above expects snapshot at this path: const snapshotPath = test.info().snapshotPath('snapshot.txt'); expect('some text').toMatchSnapshot(['dir', 'subdir', 'snapshot.txt']); // Snapshot assertion above expects snapshot at this path: const nestedPath = test.info().snapshotPath('dir', 'subdir', 'snapshot.txt'); ```

TestInfo.snapshotSuffix property

TestInfo.snapshotSuffix is a string used to differentiate snapshots between multiple test configurations. Use of this property is discouraged; instead use TestConfig.snapshotPathTemplate to configure snapshot paths.

TestInfoError.errorContext property

TestInfoError.errorContext is an optional string property, available since v1.60. It provides additional context for the error, such as the aria snapshot of the receiver at the time of an expect(...) matcher failure.

TestInfoError.cause property

TestInfoError.cause is an optional property of type TestInfoError (or null), available since v1.49. It contains the error cause when the thrown error has a cause property. It will be undefined if there is no cause or if the cause is not an instance of Error.

TestInfoError class overview

TestInfoError is a class available since v1.10 that provides information about an error thrown during test execution.

TestStepInfo.skip example without arguments

Example of skipping a test step without arguments: ```js import { test, expect } from '@playwright/test'; test('my test', async ({ page }) => { await test.step('check expectations', async step => { step.skip(); // step body below will not run // ... }); }); ```

TestStepInfo.titlePath property

TestStepInfo has a titlePath property of type Array<string> that contains the full title path starting with the test file name, including the step titles. Available since v1.55.

TestStepInfo class overview

TestStepInfo contains information about currently running test step. It is passed as an argument to the step function and provides utilities to control test step execution. Available since v1.51.

TestStepInfo.attach example with file

Example of attaching a file from disk to a test step: ```js import { test, expect } from '@playwright/test'; import { download } from './my-custom-helpers'; test('basic test', async ({}) => { await test.step('check download behavior', async step => { const tmpPath = await download('a'); await step.attach('downloaded', { path: tmpPath }); }); }); ```

TestStepInfo.skip example with condition

Example of conditionally skipping a test step: ```js import { test, expect } from '@playwright/test'; test('my test', async ({ page, isMobile }) => { await test.step('check desktop expectations', async step => { step.skip(isMobile, 'not present in the mobile layout'); // step body below will not run // ... }); }); ```

TestStepInfo.attach parameters

TestStepInfo.attach has the following parameters and options: name (string, required) - attachment name that will be sanitized and used as prefix of file name when saving to disk; body (string | Buffer, optional) - attachment body, mutually exclusive with path; contentType (string, optional) - content type of attachment to properly present in report like 'application/json' or 'image/png', inferred from path if omitted, defaults to text/plain for string attachments and application/octet-stream for Buffer attachments; path (string, optional) - path on filesystem to attached file, mutually exclusive with body.

TestStepInfo.attach method

Attach a value or a file from disk to the current test step. Some reporters show test step attachments. Either path or body must be specified, but not both. Calling this method will attribute the attachment to the step, as opposed to TestInfo.attach which stores all attachments at the test level. Automatically takes care of copying attached files to a location accessible to reporters, so you can safely remove the attachment after awaiting the attach call.

TestStepInfo.skip method without arguments

Abort the currently running step and mark it as skipped. Useful for steps that are currently failing and planned for a near-term fix. The step body will not run when skip() is called.

TestStepInfo.attach example with screenshot

Example of attaching a screenshot to a test step: ```js import { test, expect } from '@playwright/test'; test('basic test', async ({ page }) => { await page.goto('https://playwright.dev'); await test.step('check page rendering', async step => { const screenshot = await page.screenshot(); await step.attach('screenshot', { body: screenshot, contentType: 'image/png' }); }); }); ```

TestStepInfo.skip method with condition and description

Conditionally abort the currently running step and mark it as skipped with an optional description. Takes a condition parameter (boolean) - test step is skipped when the condition is true - and an optional description parameter (string) that will be reflected in a test report. Useful for steps that should not be executed in some cases.

WorkerInfo.project property

WorkerInfo.project is of type FullProject and contains the processed project configuration from the configuration file.

WorkerInfo.parallelIndex property

WorkerInfo.parallelIndex is of type int and represents 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, the new worker process has the same parallelIndex. Also available as process.env.TEST_PARALLEL_INDEX.

Give your agent this brain