How to Avoid Thread.Sleep in Test Automation

Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted

June 20, 2026 · 3 min read · Testing Guide

Sauce AI for Test Authoring: Move from intent to execution in minutes.

|

x

Back to Resources

Blog

Posted August 30, 2016

How to Avoid Thread.Sleep in Test Automation

quote

Several days back, I wanted to translate if it ’ s really necessary to use thread.sleep in trial automation codification. Here is what stuck in my head: Thread.Sleep (n) means blocking the current thread for the number of time slices that occur within “ n ” milliseconds.

There are a few things to deal before using thread.sleep in your examination automation codification. They include:

  1. The thread time slice varies from OS to OS and processor to processor. This entail that once your thread goes to sleep, it ’ s travel to look for its turn to come backwards into the scheduler. So it ’ s extremely probable that the thread.sleep time means more than what you truly intended. For example, with thread.sleep (1000), you intended 1,000 milliseconds, but it could potentially sleep for more than 1,000 milliseconds too as it await for its turn in the scheduler.

  2. Each thread has its own use of CPU and practical memory. That means our thread adopt its resource (say 1MB practical memory, some CPU cycle and some context switching round), and is simply not using them.

  3. If it ’ s a foreground thread, it ’ s preventing the application from exiting as well.

  4. What about Thread.Sleep (0)? This tells the mainframe that I ’ m ready to lose the remainder of my clip slice, and you may pick up the next thread from the queue. On one script, it feels full and efficient. But on the former hand, you are also telling the CPU that you are best at schedule than the CPU. (I don ’ t know the side effects this can cause, but I ’ m not smarter than the processor.)

We are speak about millisecond when dealing with thread.sleep, but it ’ s significant to understand that the more we use thread.sleep, the more we defer solving the existent problem, which is how to manage async in mod web apps.

Typically, we tend to use thread.sleep when the front-end waits for the backend to complete some activeness. During this clip, the front-end traditionally shows a spinner. Automation needs to await until the spinster goes off, but that doesn ’ t mean the mark factor is ready flop after the spinner vanish. For example, that element might not be clickable or enable, or in the expected state. So, a best approach is to check for correct state rather of expend thread.sleep or swear on the spinster ’ s visibility.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Selenium furnish a set of common conditions that can be leverage on top of webdriverwait.

We can make a wrapper method that takes the web component, max timeout and the condition:

var wait = new WebDriverWait (driver, timeout);
wait.IgnoreExceptionTypes (typeof (NoSuchElementException));
homecoming wait.Until (x= & gt; stipulation (factor));

Javascript doesn ’ t supply anything like expected conditions. However, fabric like webdriver.io provide rich APIs to cover this situation:

It ’ s worth follow webdriver.io internal implementation to understand this better. For example, waitForVisible API implementation offsethere. They check if the coveted element is in the DOM; if yes, they mail a protocol command to the webdriver waiter to check if it ’ s displayed. All this happens for a maximal time limit, and if the factor is nevertheless not visible, an error is thrown.

sahas2-600x383

This approach helps make the test more stable and reliable.

Published:
Aug 30, 2016
Share this post
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rights appropriate. SAUCE and SAUCE LABS are registered trademark own by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free