playwright/docs/src/test-api/class-test.md
2022-08-10 11:21:13 -07:00

37 KiB

class: Test

  • since: v1.10
  • langs: js

Playwright Test provides a test function to declare tests and expect function to write assertions.

const { test, expect } = require('@playwright/test');

test('basic test', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  const name = await page.innerText('.navbar__title');
  expect(name).toBe('Playwright');
});
import { test, expect } from '@playwright/test';

test('basic test', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  const name = await page.innerText('.navbar__title');
  expect(name).toBe('Playwright');
});

method: Test.(call)

  • since: v1.10

Declares a test.

const { test, expect } = require('@playwright/test');

test('basic test', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  const name = await page.innerText('.navbar__title');
  expect(name).toBe('Playwright');
});
import { test, expect } from '@playwright/test';

test('basic test', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  const name = await page.innerText('.navbar__title');
  expect(name).toBe('Playwright');
});

param: Test.(call).title

  • since: v1.10
  • title <[string]>

Test title.

param: Test.(call).testFunction

  • since: v1.10
  • testFunction <[function]([Fixtures], [TestInfo])>

Test function that takes one or two arguments: an object with fixtures and optional [TestInfo].

method: Test.afterAll

  • since: v1.10

Declares an afterAll hook that is executed once per worker after all tests. When called in the scope of a test file, runs after all tests in the file. When called inside a [method: Test.describe#1] group, runs after all tests in the group. If multiple afterAll hooks are added, they will run in the order of their registration.

Note that worker process is restarted on test failures, and afterAll hook runs again in the new worker. Learn more about workers and failures.

param: Test.afterAll.hookFunction

  • since: v1.10
  • hookFunction <[function]([Fixtures], [TestInfo])>

Hook function that takes one or two arguments: an object with worker fixtures and optional [TestInfo].

method: Test.afterEach

  • since: v1.10

Declares an afterEach hook that is executed after each test. When called in the scope of a test file, runs after each test in the file. When called inside a [method: Test.describe#1] group, runs after each test in the group. If multiple afterEach hooks are added, they will run in the order of their registration.

You can access all the same [Fixtures] as the test function itself, and also the [TestInfo] object that gives a lot of useful information. For example, you can check whether the test succeeded or failed.

// example.spec.js
const { test, expect } = require('@playwright/test');

test.afterEach(async ({ page }, testInfo) => {
  console.log(`Finished ${testInfo.title} with status ${testInfo.status}`);

  if (testInfo.status !== testInfo.expectedStatus)
    console.log(`Did not run as expected, ended up at ${page.url()}`);
});

test('my test', async ({ page }) => {
  // ...
});
// example.spec.ts
import { test, expect } from '@playwright/test';

test.afterEach(async ({ page }, testInfo) => {
  console.log(`Finished ${testInfo.title} with status ${testInfo.status}`);

  if (testInfo.status !== testInfo.expectedStatus)
    console.log(`Did not run as expected, ended up at ${page.url()}`);
});

test('my test', async ({ page }) => {
  // ...
});

param: Test.afterEach.hookFunction

  • since: v1.10
  • hookFunction <[function]([Fixtures], [TestInfo])>

Hook function that takes one or two arguments: an object with fixtures and optional [TestInfo].

method: Test.beforeAll

  • since: v1.10

Declares a beforeAll hook that is executed once per worker process before all tests. When called in the scope of a test file, runs before all tests in the file. When called inside a [method: Test.describe#1] group, runs before all tests in the group. If multiple beforeAll hooks are added, they will run in the order of their registration.

// example.spec.js
const { test, expect } = require('@playwright/test');

test.beforeAll(async () => {
  console.log('Before tests');
});

test.afterAll(async () => {
  console.log('After tests');
});

test('my test', async ({ page }) => {
  // ...
});
// example.spec.ts
import { test, expect } from '@playwright/test';

test.beforeAll(async () => {
  console.log('Before tests');
});

test.afterAll(async () => {
  console.log('After tests');
});

test('my test', async ({ page }) => {
  // ...
});

Note that worker process is restarted on test failures, and beforeAll hook runs again in the new worker. Learn more about workers and failures.

You can use [method: Test.afterAll] to teardown any resources set up in beforeAll.

param: Test.beforeAll.hookFunction

  • since: v1.10
  • hookFunction <[function]([Fixtures], [TestInfo])>

Hook function that takes one or two arguments: an object with worker fixtures and optional [TestInfo].

method: Test.beforeEach

  • since: v1.10

Declares a beforeEach hook that is executed before each test. When called in the scope of a test file, runs before each test in the file. When called inside a [method: Test.describe#1] group, runs before each test in the group. If multiple beforeEach hooks are added, they will run in the order of their registration.

You can access all the same [Fixtures] as the test function itself, and also the [TestInfo] object that gives a lot of useful information. For example, you can navigate the page before starting the test.

// example.spec.js
const { test, expect } = require('@playwright/test');

test.beforeEach(async ({ page }, testInfo) => {
  console.log(`Running ${testInfo.title}`);
  await page.goto('https://my.start.url/');
});

test('my test', async ({ page }) => {
  expect(page.url()).toBe('https://my.start.url/');
});
// example.spec.ts
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }, testInfo) => {
  console.log(`Running ${testInfo.title}`);
  await page.goto('https://my.start.url/');
});

test('my test', async ({ page }) => {
  expect(page.url()).toBe('https://my.start.url/');
});

You can use [method: Test.afterEach] to teardown any resources set up in beforeEach.

param: Test.beforeEach.hookFunction

  • since: v1.10
  • hookFunction <[function]([Fixtures], [TestInfo])>

Hook function that takes one or two arguments: an object with fixtures and optional [TestInfo].

method: Test.describe#1

  • since: v1.10

Declares a group of tests.

test.describe('two tests', () => {
  test('one', async ({ page }) => {
    // ...
  });

  test('two', async ({ page }) => {
    // ...
  });
});
test.describe('two tests', () => {
  test('one', async ({ page }) => {
    // ...
  });

  test('two', async ({ page }) => {
    // ...
  });
});

param: Test.describe#1.title

  • since: v1.10
  • title <[string]>

Group title.

param: Test.describe#1.callback

  • since: v1.10
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe#1]. Any tests added in this callback will belong to the group.

method: Test.describe#2

  • since: v1.24

Declares an anonymous group of tests. This is convenient to give a group of tests a common option with [method: Test.use].

test.describe(() => {
  test.use({ colorScheme: 'dark' });

  test('one', async ({ page }) => {
    // ...
  });

  test('two', async ({ page }) => {
    // ...
  });
});
test.describe(() => {
  test.use({ colorScheme: 'dark' });

  test('one', async ({ page }) => {
    // ...
  });

  test('two', async ({ page }) => {
    // ...
  });
});

param: Test.describe#2.callback

  • since: v1.24
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe#2]. Any tests added in this callback will belong to the group.

method: Test.describe.configure

  • since: v1.10

Set execution mode of execution for the enclosing scope. Can be executed either on the top level or inside a describe. Configuration applies to the entire scope, regardless of whether it run before or after the test declaration.

Learn more about the execution modes here.

Running tests in parallel:

// Run all the tests in the file concurrently using parallel workers.
test.describe.configure({ mode: 'parallel' });
test('runs in parallel 1', async ({ page }) => {});
test('runs in parallel 2', async ({ page }) => {});
// Run all the tests in the file concurrently using parallel workers.
test.describe.configure({ mode: 'parallel' });
test('runs in parallel 1', async ({ page }) => {});
test('runs in parallel 2', async ({ page }) => {});

Running tests sequentially:

// Annotate tests as inter-dependent.
test.describe.configure({ mode: 'serial' });
test('runs first', async ({ page }) => {});
test('runs second', async ({ page }) => {});
// Annotate tests as inter-dependent.
test.describe.configure({ mode: 'serial' });
test('runs first', async ({ page }) => {});
test('runs second', async ({ page }) => {});

option: Test.describe.configure.mode

  • since: v1.10
  • mode <[TestMode]<"parallel"|"serial">>

method: Test.describe.fixme

  • since: v1.25

Declares a test group similarly to [method: Test.describe#1]. Tests in this group are marked as "fixme" and will not be executed.

test.describe.fixme('broken tests', () => {
  test('example', async ({ page }) => {
    // This test will not run
  });
});
test.describe.fixme('broken tests', () => {
  test('example', async ({ page }) => {
    // This test will not run
  });
});

param: Test.describe.fixme.title

  • since: v1.25
  • title <[string]>

Group title.

param: Test.describe.fixme.callback

  • since: v1.25
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe.fixme]. Any tests added in this callback will belong to the group, and will not be run.

method: Test.describe.only

  • since: v1.10

Declares a focused group of tests. If there are some focused tests or suites, all of them will be run but nothing else.

test.describe.only('focused group', () => {
  test('in the focused group', async ({ page }) => {
    // This test will run
  });
});
test('not in the focused group', async ({ page }) => {
  // This test will not run
});
test.describe.only('focused group', () => {
  test('in the focused group', async ({ page }) => {
    // This test will run
  });
});
test('not in the focused group', async ({ page }) => {
  // This test will not run
});

param: Test.describe.only.title

  • since: v1.10
  • title <[string]>

Group title.

param: Test.describe.only.callback

  • since: v1.10
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe.only]. Any tests added in this callback will belong to the group.

method: Test.describe.parallel

  • since: v1.10

Declares a group of tests that could be run in parallel. By default, tests in a single test file run one after another, but using [method: Test.describe.parallel] allows them to run in parallel.

:::note See [method: Test.describe.configure] for the preferred way of configuring the execution mode. :::

test.describe.parallel('group', () => {
  test('runs in parallel 1', async ({ page }) => {});
  test('runs in parallel 2', async ({ page }) => {});
});
test.describe.parallel('group', () => {
  test('runs in parallel 1', async ({ page }) => {});
  test('runs in parallel 2', async ({ page }) => {});
});

Note that parallel tests are executed in separate processes and cannot share any state or global variables. Each of the parallel tests executes all relevant hooks.

param: Test.describe.parallel.title

  • since: v1.10
  • title <[string]>

Group title.

param: Test.describe.parallel.callback

  • since: v1.10
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe.parallel]. Any tests added in this callback will belong to the group.

method: Test.describe.parallel.only

  • since: v1.10

Declares a focused group of tests that could be run in parallel. This is similar to [method: Test.describe.parallel], but focuses the group. If there are some focused tests or suites, all of them will be run but nothing else.

param: Test.describe.parallel.only.title

  • since: v1.10
  • title <[string]>

Group title.

param: Test.describe.parallel.only.callback

  • since: v1.10
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe.parallel.only]. Any tests added in this callback will belong to the group.

method: Test.describe.serial

  • since: v1.10

Declares a group of tests that should always be run serially. If one of the tests fails, all subsequent tests are skipped. All tests in a group are retried together.

:::note See [method: Test.describe.configure] for the preferred way of configuring the execution mode. :::

:::note Using serial is not recommended. It is usually better to make your tests isolated, so they can be run independently. :::

test.describe.serial('group', () => {
  test('runs first', async ({ page }) => {});
  test('runs second', async ({ page }) => {});
});
test.describe.serial('group', () => {
  test('runs first', async ({ page }) => {});
  test('runs second', async ({ page }) => {});
});

param: Test.describe.serial.title

  • since: v1.10
  • title <[string]>

Group title.

param: Test.describe.serial.callback

  • since: v1.10
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe.serial]. Any tests added in this callback will belong to the group.

method: Test.describe.serial.only

  • since: v1.10

Declares a focused group of tests that should always be run serially. If one of the tests fails, all subsequent tests are skipped. All tests in a group are retried together. If there are some focused tests or suites, all of them will be run but nothing else.

:::note Using serial is not recommended. It is usually better to make your tests isolated, so they can be run independently. :::

test.describe.serial.only('group', () => {
  test('runs first', async ({ page }) => {
  });
  test('runs second', async ({ page }) => {
  });
});
test.describe.serial.only('group', () => {
  test('runs first', async ({ page }) => {
  });
  test('runs second', async ({ page }) => {
  });
});

param: Test.describe.serial.only.title

  • since: v1.10
  • title <[string]>

Group title.

param: Test.describe.serial.only.callback

  • since: v1.10
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe.serial.only]. Any tests added in this callback will belong to the group.

method: Test.describe.skip

  • since: v1.10

Declares a skipped test group, similarly to [method: Test.describe#1]. Tests in the skipped group are never run.

test.describe.skip('skipped group', () => {
  test('example', async ({ page }) => {
    // This test will not run
  });
});
test.describe.skip('skipped group', () => {
  test('example', async ({ page }) => {
    // This test will not run
  });
});

param: Test.describe.skip.title

  • since: v1.10
  • title <[string]>

Group title.

param: Test.describe.skip.callback

  • since: v1.10
  • callback <[function]>

A callback that is run immediately when calling [method: Test.describe.skip]. Any tests added in this callback will belong to the group, and will not be run.

property: Test.expect

  • since: v1.10
  • type: <[Object]>

expect function can be used to create test assertions. Read expect library documentation for more details.

method: Test.extend

  • since: v1.10
  • returns: <[Test]>

Extends the test object by defining fixtures and/or options that can be used in the tests.

First define a fixture and/or an option.

// my-test.js
const base = require('@playwright/test');
const { TodoPage } = require('./todo-page');

// Extend basic test by providing a "defaultItem" option and a "todoPage" fixture.
exports.test = base.test.extend({
  // Define an option and provide a default value.
  // We can later override it in the config.
  defaultItem: ['Do stuff', { option: true }],

  // Define a fixture. Note that it can use built-in fixture "page"
  // and a new option "defaultItem".
  todoPage: async ({ page, defaultItem }, use) => {
    const todoPage = new TodoPage(page);
    await todoPage.goto();
    await todoPage.addToDo(defaultItem);
    await use(todoPage);
    await todoPage.removeAll();
  },
});
import { test as base } from '@playwright/test';
import { TodoPage } from './todo-page';

export type Options = { defaultItem: string };

// Extend basic test by providing a "defaultItem" option and a "todoPage" fixture.
export const test = base.extend<Options & { todoPage: TodoPage }>({
  // Define an option and provide a default value.
  // We can later override it in the config.
  defaultItem: ['Do stuff', { option: true }],

  // Define a fixture. Note that it can use built-in fixture "page"
  // and a new option "defaultItem".
  todoPage: async ({ page, defaultItem }, use) => {
    const todoPage = new TodoPage(page);
    await todoPage.goto();
    await todoPage.addToDo(defaultItem);
    await use(todoPage);
    await todoPage.removeAll();
  },
});

Then use the fixture in the test.

// example.spec.js
const { test } = require('./my-test');

test('test 1', async ({ todoPage }) => {
  await todoPage.addToDo('my todo');
  // ...
});
// example.spec.ts
import { test } from './my-test';

test('test 1', async ({ todoPage }) => {
  await todoPage.addToDo('my todo');
  // ...
});

Configure the option in config file.

// playwright.config.js
// @ts-check

/** @type {import('@playwright/test').PlaywrightTestConfig<{ defaultItem: string }>} */
const config = {
  projects: [
    {
      name: 'shopping',
      use: { defaultItem: 'Buy milk' },
    },
    {
      name: 'wellbeing',
      use: { defaultItem: 'Exercise!' },
    },
  ]
};

module.exports = config;
// playwright.config.ts
import type { PlaywrightTestConfig } from '@playwright/test';
import { Options } from './my-test';

const config: PlaywrightTestConfig<Options> = {
  projects: [
    {
      name: 'shopping',
      use: { defaultItem: 'Buy milk' },
    },
    {
      name: 'wellbeing',
      use: { defaultItem: 'Exercise!' },
    },
  ]
};
export default config;

Learn more about fixtures and parametrizing tests.

param: Test.extend.fixtures

  • since: v1.10
  • fixtures <[Object]>

An object containing fixtures and/or options. Learn more about fixtures format.

method: Test.fail#1

  • since: v1.10

Unconditonally marks a 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.

const { test, expect } = require('@playwright/test');

test('not yet ready', async ({ page }) => {
  test.fail();
  // ...
});
import { test, expect } from '@playwright/test';

test('not yet ready', async ({ page }) => {
  test.fail();
  // ...
});

method: Test.fail#2

  • since: v1.10

Conditionally mark a test as "should fail" with an optional description.

const { test, expect } = require('@playwright/test');

test('fail in WebKit', async ({ page, browserName }) => {
  test.fail(browserName === 'webkit', 'This feature is not implemented for Mac yet');
  // ...
});
import { test, expect } from '@playwright/test';

test('fail in WebKit', async ({ page, browserName }) => {
  test.fail(browserName === 'webkit', 'This feature is not implemented for Mac yet');
  // ...
});

param: Test.fail#2.condition

  • since: v1.10
  • condition <[boolean]>

Test is marked as "should fail" when the condition is true.

param: Test.fail#2.description

  • since: v1.10
  • description ?<[string]>

Optional description that will be reflected in a test report.

method: Test.fail#3

  • since: v1.10

Conditionally mark all tests in a file or [method: Test.describe#1] group as "should fail".

const { test, expect } = require('@playwright/test');

test.fail(({ browserName }) => browserName === 'webkit');

test('fail in WebKit 1', async ({ page }) => {
  // ...
});
test('fail in WebKit 2', async ({ page }) => {
  // ...
});
import { test, expect } from '@playwright/test';

test.fail(({ browserName }) => browserName === 'webkit');

test('fail in WebKit 1', async ({ page }) => {
  // ...
});
test('fail in WebKit 2', async ({ page }) => {
  // ...
});

param: Test.fail#3.condition

  • since: v1.10
  • callback <[function][Fixtures]:[boolean]>

A function that returns whether to mark as "should fail", based on test fixtures. Test or tests are marked as "should fail" when the return value is true.

param: Test.fail#3.description

  • since: v1.10
  • description ?<[string]>

Optional description that will be reflected in a test report.

method: Test.fixme#1

  • since: v1.10

Declares a test to be fixed, similarly to [method: Test.(call)]. This test will not be run.

const { test, expect } = require('@playwright/test');

test.fixme('test to be fixed', async ({ page }) => {
  // ...
});
import { test, expect } from '@playwright/test';

test.fixme('test to be fixed', async ({ page }) => {
  // ...
});

param: Test.fixme#1.title

  • since: v1.10
  • title <[string]>

Test title.

param: Test.fixme#1.testFunction

  • since: v1.10
  • testFunction <[function]([Fixtures], [TestInfo])>

Test function that takes one or two arguments: an object with fixtures and optional [TestInfo].

method: Test.fixme#2

  • since: v1.10

Mark a test as "fixme", with the intention to fix it. Test is immediately aborted when you call [method: Test.fixme#2].

const { test, expect } = require('@playwright/test');

test('test to be fixed', async ({ page }) => {
  test.fixme();
  // ...
});
import { test, expect } from '@playwright/test';

test('test to be fixed', async ({ page }) => {
  test.fixme();
  // ...
});

Mark all tests in a file or [method: Test.describe#1] group as "fixme".

const { test, expect } = require('@playwright/test');

test.fixme();

test('test to be fixed 1', async ({ page }) => {
  // ...
});
test('test to be fixed 2', async ({ page }) => {
  // ...
});
import { test, expect } from '@playwright/test';

test.fixme();

test('test to be fixed 1', async ({ page }) => {
  // ...
});
test('test to be fixed 2', async ({ page }) => {
  // ...
});

method: Test.fixme#3

  • since: v1.10

Conditionally mark a test as "fixme" with an optional description.

const { test, expect } = require('@playwright/test');

test('broken in WebKit', async ({ page, browserName }) => {
  test.fixme(browserName === 'webkit', 'This feature is not implemented on Mac yet');
  // ...
});
import { test, expect } from '@playwright/test';

test('broken in WebKit', async ({ page, browserName }) => {
  test.fixme(browserName === 'webkit', 'This feature is not implemented on Mac yet');
  // ...
});

param: Test.fixme#3.condition

  • since: v1.10
  • condition <[boolean]>

Test is marked as "fixme" when the condition is true.

param: Test.fixme#3.description

  • since: v1.10
  • description ?<[string]>

Optional description that will be reflected in a test report.

method: Test.fixme#4

  • since: v1.10

Conditionally mark all tests in a file or [method: Test.describe#1] group as "fixme".

const { test, expect } = require('@playwright/test');

test.fixme(({ browserName }) => browserName === 'webkit');

test('broken in WebKit 1', async ({ page }) => {
  // ...
});
test('broken in WebKit 2', async ({ page }) => {
  // ...
});
import { test, expect } from '@playwright/test';

test.fixme(({ browserName }) => browserName === 'webkit');

test('broken in WebKit 1', async ({ page }) => {
  // ...
});
test('broken in WebKit 2', async ({ page }) => {
  // ...
});

param: Test.fixme#4.condition

  • since: v1.10
  • callback <[function][Fixtures]:[boolean]>

A function that returns whether to mark as "fixme", based on test fixtures. Test or tests are marked as "fixme" when the return value is true.

param: Test.fixme#4.description

  • since: v1.10
  • description ?<[string]>

Optional description that will be reflected in a test report.

method: Test.info

  • since: v1.10
  • returns: <[TestInfo]>

Returns information about the currently running test. This method can only be called during the test execution, otherwise it throws.

method: Test.only

  • since: v1.10

Declares a focused test. If there are some focused tests or suites, all of them will be run but nothing else.

test.only('focus this test', async ({ page }) => {
  // Run only focused tests in the entire project.
});
test.only('focus this test', async ({ page }) => {
  // Run only focused tests in the entire project.
});

param: Test.only.title

  • since: v1.10
  • title <[string]>

Test title.

param: Test.only.testFunction

  • since: v1.10
  • testFunction <[function]([Fixtures], [TestInfo])>

Test function that takes one or two arguments: an object with fixtures and optional [TestInfo].

method: Test.setTimeout

  • since: v1.10

Changes the timeout for the test. Zero means no timeout. Learn more about various timeouts.

const { test, expect } = require('@playwright/test');

test('very slow test', async ({ page }) => {
  test.setTimeout(120000);
  // ...
});
import { test, expect } from '@playwright/test';

test('very slow test', async ({ page }) => {
  test.setTimeout(120000);
  // ...
});

Changing timeout from a slow beforeEach or afterEach hook. Note that this affects the test timeout that is shared with beforeEach/afterEach hooks.

const { test, expect } = require('@playwright/test');

test.beforeEach(async ({ page }, testInfo) => {
  // Extend timeout for all tests running this hook by 30 seconds.
  test.setTimeout(testInfo.timeout + 30000);
});
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }, testInfo) => {
  // Extend timeout for all tests running this hook by 30 seconds.
  test.setTimeout(testInfo.timeout + 30000);
});

Changing timeout for a beforeAll or afterAll hook. Note this affects the hook's timeout, not the test timeout.

const { test, expect } = require('@playwright/test');

test.beforeAll(async () => {
  // Set timeout for this hook.
  test.setTimeout(60000);
});
import { test, expect } from '@playwright/test';

test.beforeAll(async () => {
  // Set timeout for this hook.
  test.setTimeout(60000);
});

Changing timeout for all tests in a [method: Test.describe#1] group.

const { test, expect } = require('@playwright/test');

test.describe('group', () => {
  // Applies to all tests in this group.
  test.setTimeout(60000);

  test('test one', async () => { /* ... */ });
  test('test two', async () => { /* ... */ });
  test('test three', async () => { /* ... */ });
});
import { test, expect } from '@playwright/test';

test.describe('group', () => {
  // Applies to all tests in this group.
  test.setTimeout(60000);

  test('test one', async () => { /* ... */ });
  test('test two', async () => { /* ... */ });
  test('test three', async () => { /* ... */ });
});

Timeout for the currently running test is available through [property: TestInfo.timeout].

param: Test.setTimeout.timeout

  • since: v1.10
  • timeout <[int]>

Timeout in milliseconds.

method: Test.skip#1

  • since: v1.10

Declares a skipped test, similarly to [method: Test.(call)]. Skipped test is never run.

const { test, expect } = require('@playwright/test');

test.skip('broken test', async ({ page }) => {
  // ...
});
import { test, expect } from '@playwright/test';

test.skip('broken test', async ({ page }) => {
  // ...
});

param: Test.skip#1.title

  • since: v1.10
  • title <[string]>

Test title.

param: Test.skip#1.testFunction

  • since: v1.10
  • testFunction <[function]([Fixtures], [TestInfo])>

Test function that takes one or two arguments: an object with fixtures and optional [TestInfo].

method: Test.skip#2

  • since: v1.10

Unconditionally skip a test. Test is immediately aborted when you call [method: Test.skip#2].

const { test, expect } = require('@playwright/test');

test('skipped test', async ({ page }) => {
  test.skip();
  // ...
});
import { test, expect } from '@playwright/test';

test('skipped test', async ({ page }) => {
  test.skip();
  // ...
});

Unconditionally skip all tests in a file or [method: Test.describe#1] group:

const { test, expect } = require('@playwright/test');

test.skip();

test('skipped test 1', async ({ page }) => {
  // ...
});
test('skipped test 2', async ({ page }) => {
  // ...
});
import { test, expect } from '@playwright/test';

test.skip();

test('skipped test 1', async ({ page }) => {
  // ...
});
test('skipped test 2', async ({ page }) => {
  // ...
});

method: Test.skip#3

  • since: v1.10

Conditionally skip a test with an optional description.

const { test, expect } = require('@playwright/test');

test('skip in WebKit', async ({ page, browserName }) => {
  test.skip(browserName === 'webkit', 'This feature is not implemented for Mac');
  // ...
});
import { test, expect } from '@playwright/test';

test('skip in WebKit', async ({ page, browserName }) => {
  test.skip(browserName === 'webkit', 'This feature is not implemented for Mac');
  // ...
});

Skip from [method: Test.beforeEach] hook:

const { test, expect } = require('@playwright/test');

test.beforeEach(async ({ page }) => {
  test.skip(process.env.APP_VERSION === 'v1', 'There are no settings in v1');
  await page.goto('/settings');
});
import { test, expect } from '@playwright/test';

test.beforeEach(async ({ page }) => {
  test.skip(process.env.APP_VERSION === 'v1', 'There are no settings in v1');
  await page.goto('/settings');
});

param: Test.skip#3.condition

  • since: v1.10
  • condition <[boolean]>

A skip condition. Test is skipped when the condition is true.

param: Test.skip#3.description

  • since: v1.10
  • description ?<[void]|[string]>

Optional description that will be reflected in a test report.

method: Test.skip#4

  • since: v1.10

Conditionally skips all tests in a file or [method: Test.describe#1] group.

const { test, expect } = require('@playwright/test');

test.skip(({ browserName }) => browserName === 'webkit');

test('skip in WebKit 1', async ({ page }) => {
  // ...
});
test('skip in WebKit 2', async ({ page }) => {
  // ...
});
import { test, expect } from '@playwright/test';

test.skip(({ browserName }) => browserName === 'webkit');

test('skip in WebKit 1', async ({ page }) => {
  // ...
});
test('skip in WebKit 2', async ({ page }) => {
  // ...
});

param: Test.skip#4.condition

  • since: v1.10
  • callback <[function][Fixtures]:[boolean]>

A function that returns whether to skip, based on test fixtures. Test or tests are skipped when the return value is true.

param: Test.skip#4.description

  • since: v1.10
  • description ?<[string]>

Optional description that will be reflected in a test report.

method: Test.slow#1

  • since: v1.10

Unconditionally marks a test as "slow". Slow test will be given triple the default timeout.

const { test, expect } = require('@playwright/test');

test('slow test', async ({ page }) => {
  test.slow();
  // ...
});
import { test, expect } from '@playwright/test';

test('slow test', async ({ page }) => {
  test.slow();
  // ...
});

:::note [method: Test.slow#1] cannot be used in a beforeAll or afterAll hook. Use [method: Test.setTimeout] instead. :::

method: Test.slow#2

  • since: v1.10

Conditionally mark a test as "slow" with an optional description. Slow test will be given triple the default timeout.

const { test, expect } = require('@playwright/test');

test('slow in WebKit', async ({ page, browserName }) => {
  test.slow(browserName === 'webkit', 'This feature is slow on Mac');
  // ...
});
import { test, expect } from '@playwright/test';

test('slow in WebKit', async ({ page, browserName }) => {
  test.slow(browserName === 'webkit', 'This feature is slow on Mac');
  // ...
});

param: Test.slow#2.condition

  • since: v1.10
  • condition <[boolean]>

Test is marked as "slow" when the condition is true.

param: Test.slow#2.description

  • since: v1.10
  • description ?<[string]>

Optional description that will be reflected in a test report.

method: Test.slow#3

  • since: v1.10

Conditionally mark all tests in a file or [method: Test.describe#1] group as "slow". Slow tests will be given triple the default timeout.

const { test, expect } = require('@playwright/test');

test.slow(({ browserName }) => browserName === 'webkit');

test('slow in WebKit 1', async ({ page }) => {
  // ...
});
test('slow in WebKit 2', async ({ page }) => {
  // ...
});
import { test, expect } from '@playwright/test';

test.slow(({ browserName }) => browserName === 'webkit');

test('slow in WebKit 1', async ({ page }) => {
  // ...
});
test('fail in WebKit 2', async ({ page }) => {
  // ...
});

param: Test.slow#3.condition

  • since: v1.10
  • callback <[function][Fixtures]:[boolean]>

A function that returns whether to mark as "slow", based on test fixtures. Test or tests are marked as "slow" when the return value is true.

param: Test.slow#3.description

  • since: v1.10
  • description ?<[string]>

Optional description that will be reflected in a test report.

async method: Test.step

  • since: v1.10
  • returns: <[any]>

Declares a test step.

const { test, expect } = require('@playwright/test');

test('test', async ({ page }) => {
  await test.step('Log in', async () => {
    // ...
  });
});
import { test, expect } from '@playwright/test';

test('test', async ({ page }) => {
  await test.step('Log in', async () => {
    // ...
  });
});

The method returns value retuned by the step callback.

const { test, expect } = require('@playwright/test');

test('test', async ({ page }) => {
  const user = await test.step('Log in', async () => {
    // ...
    return 'john';
  });
  expect(user).toBe('john');
});
import { test, expect } from '@playwright/test';

test('test', async ({ page }) => {
  const user = await test.step('Log in', async () => {
    // ...
    return 'john';
  });
  expect(user).toBe('john');
});

param: Test.step.title

  • since: v1.10
  • title <[string]>

Step name.

param: Test.step.body

  • since: v1.10
  • body <[function]:[Promise]<[any]>>

Step body.

method: Test.use

  • since: v1.10

Specifies options or fixtures to use in a single test file or a [method: Test.describe#1] group. Most useful to set an option, for example set locale to configure context fixture. test.use can be called either in the global scope or inside test.describe, it's is an error to call it within beforeEach or beforeAll.

const { test, expect } = require('@playwright/test');

test.use({ locale: 'en-US' });

test('test with locale', async ({ page }) => {
  // Default context and page have locale as specified
});
import { test, expect } from '@playwright/test';

test.use({ locale: 'en-US' });

test('test with locale', async ({ page }) => {
  // Default context and page have locale as specified
});

It is also possible to override a fixture by providing a function.

const { test, expect } = require('@playwright/test');

test.use({
  locale: async ({}, use) => {
    // Read locale from some configuration file.
    const locale = await fs.promises.readFile('test-locale', 'utf-8');
    await use(locale);
  },
});

test('test with locale', async ({ page }) => {
  // Default context and page have locale as specified
});
import { test, expect } from '@playwright/test';

test.use({
  locale: async ({}, use) => {
    // Read locale from some configuration file.
    const locale = await fs.promises.readFile('test-locale', 'utf-8');
    await use(locale);
  },
});

test('test with locale', async ({ page }) => {
  // Default context and page have locale as specified
});

param: Test.use.fixtures

  • since: v1.10
  • options <[TestOptions]>

An object with local options.