Rayrun

What are other ways to change timeouts for individual tests or groups of tests in Playwright Test?

Answer

Changing Timeouts in Playwright Test

If you need to change timeouts for individual tests or groups of tests in @playwright/test, there are several options available.

Set Timeout for Individual Test

Use test.setTimeout(timeout) within each test function to set a custom timeout for that specific test:

import { test } from "@playwright/test";

test("Custom timeout test", async ({ page }) => {
  test.setTimeout(10000); // Set a 10-second timeout for this test
  await page.goto("https://ray.run");
  // Your test code here
});

Set Global Timeout in Configuration File

Set a global timeout value for all tests in your project using defineConfig({timeout: ...}) in your playwright.config.ts file:

import { defineConfig } from "@playwright/test";

export default defineConfig({
  timeout: 60000, // Set a 60-second global timeout for all tests
  // Other configuration options
});

Change Timeouts in Hooks

Configure timeouts within hooks like beforeEach and afterEach using methods such as page.setDefaultTimeout(timeout):

import { test } from "@playwright/test";

test.beforeEach(async ({ page }) => {
  page.setDefaultTimeout(10000); // Set a 10-second default timeout for all tests
});

test("Example test", async ({ page }) => {
  await page.goto("https://ray.run");
  // Your test code here
});

Or set custom timeouts for expect statements:

import { test } from "@playwright/test";

test("Custom timeout for expect", async ({ page, locator }) => {
  await page.goto("https://ray.run");
  await expect(locator).toBeVisible({ timeout: 10000 }); // Set a 10-second timeout for this expect statement
});

By using these methods, you can easily customize timeouts for your Playwright tests to suit your specific needs.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].