Automated Testing (IT)Frontend Test Automation / QA Automation Engineer

What are the best practices for working with dynamic waits in automated UI tests?

Pass interviews with Hintsage AI assistant

Answer.

Working with dynamic waits is a pressing issue in the field of UI automation, especially for modern web applications, where content often appears and changes not instantly.

Background In early versions of automation, hard waits (sleep) were used, which made tests either too slow or flaky if applications took longer to load than usual. This became relevant in the era of SPA and heavy JS.

Problem Using static delays leads to unstable tests and wasted time: the autotest can lag or "fail" randomly. Moreover, scalability and test support suffer.

Solution Use dynamic (explicit) waits: for example, WebDriverWait in Selenium, waitFor functions in Cypress and Playwright frameworks.

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) # maximum wait of 10 seconds wait.until(EC.visibility_of_element_located((By.ID, 'my-element')))

Key features:

  • Use explicit waits only where necessary
  • Do not duplicate waits for the same event
  • Clearly define the conditions that end the wait (visibility/clickable/etc.)

Trick Questions.

Why is sleep needed if there are dynamic waits?

Using time.sleep() is justified only for debugging or in extreme cases. In real autotests, always use explicit waits.

Can the use of waits completely eliminate flakiness?

No, if the wait conditions are described inadequately (e.g., waiting for an attribute to appear instead of full data loading), flakiness will remain.

Can a single global wait be used for all tests?

No — the conditions for element appearance vary. Waits should be applied strictly to those steps where necessary.

Common Mistakes and Anti-Patterns

  • Using sleep instead of waits
  • Repeated waits where they are not needed
  • Too long timeouts that hinder catching real errors

Real-life Example

Negative Case

All autotests were written with sleep(2) between the "click" and the check. After refreshing the page, users complained about delays, and the tests sometimes failed due to slow internet.

Pros:

  • Tests are simple without wait code

Cons:

  • Tests take a long time to execute
  • Flaky failures
  • Do not adapt to the real speed of the application

Positive Case

Explicit waits were set up for loading blocks; clicks became possible after the required elements appeared. Abandoned sleep, all tests run stably and quickly.

Pros:

  • Minimum flakiness
  • Fast execution
  • Excellent readability and support

Cons:

  • Time must be spent on correctly setting wait conditions