Rayrun

How do I use the test.step and test.setTimeout methods in Playwright to slow down a specific test while keeping the slow_mo parameter at 0 for all other tests?

Answer

Sure, you can slow down a specific test in Playwright without affecting the rest. Here's how you can do it using test.step and test.setTimeout.

First, use test.step to break down your test into multiple steps. This allows you to control the execution speed of each step individually. For instance, you can introduce delays using page.waitForTimeout within a test.step block.

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

test('slow down specific test', async ({ page }) => {
  await page.goto('https://ray.run');

  await test.step('Perform action slowly', async () => {
    await page.click('#button1');
    await page.waitForTimeout(1000);
    await page.fill('#input1', 'Hello');
  });
});

Next, use test.setTimeout to increase the timeout for the entire test. This ensures that the slow-downed step has enough time to complete without causing a timeout error.

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

const SLOW_DOWN_THRESHOLD = 5000;

test.setTimeout(SLOW_DOWN_THRESHOLD + 10000);

test('slow down specific test', async ({ page }) => {
  await page.goto('https://ray.run');

  await test.step('Perform action slowly', async () => {
    await page.click('#button1');
    await page.waitForTimeout(1000);
    await page.fill('#input1', 'Hello');
  });
});

By combining test.step and test.setTimeout, you can effectively slow down a specific test while keeping the slow_mo parameter at 0 for all other tests. Adjust the SLOW_DOWN_THRESHOLD value as per your needs.

References

Thank you!
Was this helpful?
Still have questions?

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

Related Discord Threads

Related Questions

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].