Rayrun

How do I set a custom timeout for API requests in Playwright Test?

Answer

To set a custom timeout for API requests in @playwright/test, you can use the action timeout setting. By default, there's no timeout set for each action, but you can set a default value in the config using testConfig.use.actionTimeout.

Here's how to set a default action timeout of 10 seconds (10000 milliseconds) in your playwright.config.ts file:

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

export default defineConfig({
  use: {
    actionTimeout: 10 * 1000,
  },
});

This sets a global action timeout of 10 seconds for all tests in your project. If you want to override the global action timeout for a specific test, pass a timeout option to individual actions.

For example, to set a 15-second (15000 milliseconds) timeout for a specific test:

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

test('example test', async ({ page }) => {
  await page.waitForSelector('#my-element', { timeout: 15000 });
});

In this example, we're using the waitForSelector method with an overridden timeout option of 15 seconds.

By understanding how timeouts work and configuring them properly in your tests or fixtures, you can ensure that your tests run smoothly and efficiently without timing out unnecessarily.

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 luc@ray.run.