Stop guessing sleep durations: design automation waits around state

Define success, failure and deadlines for each wait, separating actionability from outcome checks without retrying submissions blindly.

Replace “sleep three seconds, then click” with “continue when a verifiable condition holds, and stop at a deadline.” That is usually a better first change than continually increasing delays. The goal is not to remove every pause, but to define what the script is waiting for and what evidence permits it to continue.

This design guide uses Selenium and Playwright documentation checked on September 15, 2026. Its example is not an SDK recipe for every phone automation tool, and no device testing was performed.

Elapsed time is not application state

Selenium explains that a fixed sleep can be too short for a page or unnecessarily long for a session. An explicit wait checks a condition and continues when it becomes true within the timeout. Selenium waiting strategies

For a search workflow, the desired state is not “three seconds elapsed.” It is “this query has a result or an explicit empty result.” Waiting only for a nonempty list misclassifies a valid zero-result search as failure.

Write a small contract for each wait

Consider this hypothetical search step:

Field What to specify
Prior action One query was submitted; do not submit again inside the wait
Success Results for the current query, or its explicit empty state
Failure An authentication, permission or service error
Budget For example, eight seconds—illustrative, not a universal setting
Evidence Last observed state, condition name, duration and error, without sensitive content

A disappeared spinner is not enough: an error page may have no spinner either. Check the page identity and its relationship to the current query so that stale results do not satisfy the condition.

Visible is not the same as actionable

Playwright checks more than visibility before a click: stability, enabled state and whether the element receives events also matter. A visible button may still move or sit behind an overlay. Playwright actionability

Use the framework's supported waiting behavior where available. If a tool only offers screenshots or coordinates, check the surrounding page and target region, but do not claim that recognizing a button-shaped image validates every actionability condition.

Wait before acting; verify after acting

Readiness to click does not establish the business outcome of the click. Playwright's retrying assertions recheck the expected state until it holds or the assertion times out, and asynchronous assertions must be awaited. Playwright assertions

The following is conceptual pseudocode, not a product API:

Wait for this page's query control to be actionable, within a deadline
Submit the query once
Repeat read-only observations:
    On explicit failure: record it and stop
    On this query's result or empty result: record it and finish
    At the deadline: record the last state and stop
    Pause for an appropriate interval, then observe again

The retried operation is observation, not submission. Before putting a side-effecting action inside a retry loop, establish what duplicate execution could do and how it is prevented.

Keep the wait itself bounded

These are implementation suggestions rather than prescribed timing values:

  • Bound each observation request too. An outer eight-second budget cannot help if an inner call never returns.
  • Read the smallest useful evidence instead of retrieving every screenshot, element and log on each iteration.
  • Exit on success or cancellation, and impose a total task budget.
  • Understand existing framework timeouts before adding wrappers. Selenium specifically warns against mixing implicit and explicit waits because timing can become unpredictable. Official guidance

Validate immediate readiness, delayed readiness, explicit failure, permanent non-readiness and cancellation. A dependable wait does not eliminate every timeout; it prevents both premature action and endless waiting.

参考来源