Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause the test script for 3 seconds before continue running it? Playwright

I'm running a test called create admin. The test will first create admin, then check if the admin was created successfully.

In the script, I have a part of code where I want to wait for 3 seconds before continuing because whenever the submit button was clicked, the website will need to take 3s to refresh the admin table (list of user info) after navigation was done. For more information, this refresh is not a navigation and therefore, my 'waitForNavigation()' is not working.

Therefore, the process will be like: 'fill out the form' > 'click submit button' > 'wait for navigation' > 'reload user table (3s).

If I don't wait 3s for the table to refresh, the test will throw an error because the registered user will not be found in the table (I have other scripts to find the user).

This is how the navigation looks like when 'Save button' was clicked: enter image description here

After that, the table takes 3s to refresh and it looks like: enter image description here

This is how the 'create' function looks like: enter image description here

like image 783
jialeee17 Avatar asked Sep 01 '25 01:09

jialeee17


1 Answers

Playwright has this capability natively:

await page.waitForTimeout(3000);

Documentation for this is here: https://playwright.dev/docs/api/class-page#page-wait-for-timeout


EDIT: As per the comment on this answer, I feel it is essential to reiterate that adding pauses in this manner is usually a bad idea.

If at all possible, your test should be waiting, instead, for an expected change of state on the page; there are very few situations when you would not want to do this.

Adding a waitForTimeout can be very tempting as it is so easy. But you are inviting trouble and flaky tests downstream, not to mention you are artificially increasing the duration of your test suite.

This explains these issues further: https://stackoverflow.com/a/74933906/1831456.

like image 163
Holf Avatar answered Sep 02 '25 16:09

Holf