Fixed Sleeps in Tests: Stop Guessing a Time, Wait for the Screen Instead

Fixed sleeps in tests make a suite slow and flaky. Replace each sleep with a check that waits for the screen and passes the moment the element appears.

By Ait Nohaila, Business Development Representative 10 min read

A fixed sleep in a test is a guess: too short and the test fails on a slow day, too long and every run wastes time. Wait for the screen instead, with a check that passes when the element appears.

You know the pattern. A test fails on the build machine, someone adds two seconds of sleep, and it passes. A month later it fails again, and the sleep becomes five seconds. The suite gets slower, and no steadier.

What is a fixed sleep in a test?

Definition

A fixed sleep, or fixed wait, is a step that pauses a test for a set time before the next action.

  • It is there because the app needs a moment: a page loads, a dialog opens, a search returns.

The pause does not look at the app, so it cannot know whether that moment has passed.

In test code it is a line such as a sleep of 2 seconds. In a recorded test it is a pause someone added by hand after a failure. Either way, a person guessed a number.

Why is a fixed sleep always wrong: too short or too long

An app does not take the same time twice. The build machine is busy, the network is slow, the database is larger than last month. One fixed number cannot fit every day.

A fixed sleep of 3 seconds

A fast day: the page is ready after 1 second

The test waits 3 seconds: 2 of them for nothing.

A slow day: the page is ready after 5 seconds

The test clicks after 3 seconds, too early, and fails.

A check that waits up to 10 seconds

A fast day: the page is ready after 1 second

The check passes after 1 second, and the test goes on.

A slow day: the page is ready after 5 seconds

The check passes after 5 seconds, and the test goes on.

Each bar is the time the test spent waiting. The sleep is wrong on both days. The check is right on both, because it follows the screen.
The guess isWhat happensWhat the team sees
Too shortThe test clicks before the screen is ready.A test that fails on slow days and passes when run again: a flaky test.
Too longThe test waits after the screen is ready.A suite that takes longer at every run, on fast days too.
Right todayThe app changes, and the guess is wrong again.Someone raises the number, and the suite slows down a little more.

Timing is one of the main reasons a test fails while nothing is broken.The others are in flaky tests and false positives.

What fixed waits cost a test suite: a small sum

One sleep of 2 seconds looks harmless. A suite holds hundreds of them, and every run pays for all of them.

200 tests3 sleeps in each2 seconds per sleep20 minutes of every run
An example sum, not a statistic: put your own numbers in. The time is spent whether the app needed it or not.
ExampleTests slow and flaky at the same time

Take 200 tests with three sleeps of 2 seconds each. That is 1,200 seconds: 20 minutes of every run spent waiting. With ten runs a day, more than three hours go by with nothing happening. And on the day a page takes 3 seconds, the tests still fail.

A fixed sleep makes every run pay for the slowest day, and still does not cover it.

Explicit waits in a coded framework: the fix you write yourself

In a coded framework such as Selenium, Playwright or Cypress, the answer to a fixed sleep is to wait for a condition in code, often called an explicit wait: name the element, name the condition, set a limit. It is the right idea. It is also code to write, to read and to keep up.

What you needIn a coded frameworkIn GUI Test Studio
Wait for an element to appearA line of code that names the element and the condition.Nothing to write: the check that looks for the element is the wait.
Wait for a spinner to goAnother line, with the opposite condition.A Visual Absent Assert on the spinner.
Set how long to waitA number in the code or in a configuration file.A field of the check, changed for several checks at once.
Follow an app that got slowerFind the waits in the code and tune them.Raise a timeout. It costs nothing on a fast day.

In GUI Test Studio, every check is a wait

GUI Test Studio automates end-to-end tests of any app without code. You record a workflow, and the test gets its checks from the recording. A check does not look once and give up. It keeps looking until what it expects is on the screen, and the test goes on at once.

Three things are true of every check.

  • It has a timeout, the longest it will wait, set to 5 seconds by default.
  • It passes the moment the element is there, not at the end of the timeout.
  • It fails only when the timeout ends without the element.
The timeout is one field of the check. The card on top says it in words: waits up to 5 s.

A recording does this for you.Every element you click gets a check placed before the click, so the test makes sure the button is there, then clicks it.The question after a recording calls the timeout Wait up to, and GUI Test Studio calls it Timeout.Timeouts, search areas and failures has the details.

No code is involved at any point, on a web app or on a desktop one: see Windows desktop app testing without writing code.

What are you waiting for? The check to use

Behind every sleep there is something the author was waiting for. Name it, and you have named the check.

Wait for it to appear

Export

Visual Assert or Text Assert

Passes the moment the element is on the screen.

Wait for it to go away

Loading

Visual Absent Assert or Text Absent Assert

Passes the moment the spinner has gone.

Wait for the right kind of value

ORD-20418

Dynamic Text Assert

Passes the moment a value of that shape is there, whatever the value.

Whatever you wait for, the check ends the wait the moment the screen says so. No time is guessed.
What you are waiting forThe check to useIt passes
A button, an icon or a picture to appearVisual AssertThe moment the element is on the screen and looks right.
A message or a title to appearText AssertThe moment the words are on the screen.
A spinner or a progress bar to go awayVisual Absent AssertThe moment it has left the screen.
A word such as Loading to go awayText Absent AssertThe moment the word is no longer there.
A total, a date or an order number to show upDynamic Text AssertThe moment a value of the right shape is there, whatever the value.
A list to hold the right number of rowsVisual Count Assert or Text Count AssertThe moment the count follows its rule.
Something the screen never showsA Wait stepAfter the fixed time you gave it.

Choose the right check starts from the same question for all nine checks.

How do you wait for a loading spinner to disappear?

With a check that passes when something is gone. A spinner is the app saying that it is not ready. So the best wait is the spinner's own end.

ExampleA report that takes 2 to 20 seconds

After a click on Generate Report, a spinner turns for 2 to 20 seconds. A fixed sleep would have to be 20 seconds, every time. A Visual Absent Assert on the spinner, with a timeout of 30 seconds, lets the test go on the moment the report is ready.

A value that changes at every run is waited for the same way.A Dynamic Text Assert does not expect one total or one date: it waits for a value of the right shape, such as an amount or an order number.Visual checks and Text checks describe each of these.

What happens when the time runs out: stop or continue

A wait that ends without its element is a failure, and each check says what that failure does.

If it failsGood for
Stop the testA check the next clicks depend on, such as the check of a button before its click. Going on would click the wrong place.
Continue the testA check that only confirms what is on the screen. One run then reports every problem it meets.

Either way, nothing is left to guess afterwards.The report says which check ran out of time and shows what the screen looked like at that moment: see how to read a test report.

How to replace a fixed sleep with a check that waits

A recorded test has few fixed pauses, if any. This is for the Wait someone added after a bad day, or for a step that still fails when the app is slow.

  1. 1

    Find the fixed pause

    Open the test in GUI Test Studio and find the Wait on the time ruler, or the step that fails on slow days.

  2. 2

    Ask what the pause was waiting for

    Look at the recording at that moment and name what shows that the app is ready: a button, a message, or a spinner that has gone.

  3. 3

    Add the check that waits for it

    Drag the matching check from the Assertions palette onto the Assertion track at that moment, and show it what to look for on the recording.

  4. 4

    Set how long it may wait

    Set the check's Timeout to the slowest day you accept.On a fast day the check still passes at once.

  5. 5

    Decide what a failure does

    Choose Stop the test when the next steps depend on the check, or Continue the test when it only confirms what is on the screen.

  6. 6

    Remove the Wait and run the test

    Tick Ignored on the Wait, or delete it, then run the test.The run now goes on the moment the screen is ready.

You can also ask for the change in a prompt, to your AI assistant or to a local AI model, one we provide or one you bring: give a check a longer timeout, add a wait, decide what happens if it fails.See Create a test from a prompt.

When a real Wait step is the right choice

Sometimes the app needs time and shows nothing while it takes it. No check can wait for what is not on the screen. That is what the Wait step is for.

A fixed Wait is right in a few cases.

  • A file is written in the background before the next step reads it.
  • A server job runs with no progress on the screen.
  • A tool outside the app needs a moment to start.

A Wait is found in the Events palette, under Test Flow.Drop it on the timeline and it lands on the time ruler, at that moment.Test Flow: waits, apps, phones and scripts describes it.

A Wait has one setting that matters: how long.

What to know before you start

Good to knowWhat it means
A failing check uses its whole timeoutA check that fails waits to the end before it says so. Set each timeout to the slowest day you accept, not far beyond.
A check needs something to seeWhen nothing on the screen says the app is ready, use a Wait step.
A screen that is still appearingAn element can be found while a menu still fades in or a page still loads.A visual check is compared at its best moment in the short time after it is found: see the Enhanced Accuracy Pass.
A test checks what it was told to checkA wait proves that one element arrived. What else is on that screen is checked only if a check looks at it.

Key takeaways

  • 1A fixed sleep is a guess, and a guess is too short on a slow day and too long on every other day.
  • 2Sleeps make a suite slow and flaky at once: every run pays for them, and the slowest day still fails.
  • 3Wait for the screen instead: for an element to appear, to go away, or to show the right kind of value.
  • 4In GUI Test Studio every check is that wait, up to 5 seconds by default, and it passes the moment the element is there.
  • 5Keep the Wait step for what no check can see, such as a file written in the background.

Questions

Why are fixed sleeps bad in tests?

Because a fixed sleep is a guess. When the app is slower than the guess, the test acts too early and fails. When the app is faster, the test waits for nothing, at every run.

What should I use instead of a sleep in a test?

Wait for what the screen shows when the app is ready: a button, a message, or a spinner that has gone. In GUI Test Studio, a check does that by itself: it keeps looking up to its timeout and passes the moment the element is there.

What is the difference between a fixed wait and an explicit wait?

A fixed wait pauses for a set time, whatever the app does. An explicit wait pauses until a condition is true, up to a limit, so it ends as soon as the app is ready. In a coded framework you write it for each element, and in GUI Test Studio each check is one.

How long should a test wait for an element?

As long as the slowest day you accept. In GUI Test Studio a check waits up to 5 seconds by default, and you raise that for a slow screen. The limit is only reached when the element never appears.

Does a longer timeout make my tests slower?

No, not when the tests pass. A check goes on the moment the element is there, so a 30-second timeout costs 1 second on a day the screen takes 1 second. Only a check that fails uses its whole timeout.

How do I wait for a loading spinner to disappear?

Use a check that passes when something is gone. In GUI Test Studio, a Visual Absent Assert on the spinner passes the moment the spinner has left the screen, and a Text Absent Assert does the same for a word such as Loading.

When is a fixed wait still the right choice?

When the app needs time and shows nothing while it takes it: a file written in the background, a job on a server with no progress on the screen. GUI Test Studio has a Wait step for that case.

Do I have to add the waits myself in a recorded test?

No. A recording gives every element you click a check placed before the click, and that check waits for the element. You only adjust a timeout when a screen is slower than usual.

Selenium, Playwright and Cypress are trademarks of their respective owners.

All articles