Skip to content

Taiko's Wait Mechanism

Nivedha edited this page Oct 12, 2020 · 1 revision

One of the important visions of Taiko is to eliminate flakiness in UI automated end to end tests. Taiko tries to achieve that by implicitly waiting for readiness of elements and events triggered after action, and also provides better custom wait mechanisms for users to use. This helps in waiting for the right state of the page to perform any action instead of arbitrarily adding waits with time that leads to flakiness and increase in execution time. There are two types of waits in Taiko namely implicit and explicit waits.

Implicit waits:

Waiting for element readiness:

Default element readiness timeout(retryTimeout) is 10 seconds.

TODO: Connected and stability checks - https://github.com/getgauge/taiko/issues/1512 Making check consistent across actions - https://github.com/getgauge/taiko/issues/630

Waiting for events after action:

Default timeout(navigationTimeout) for events after action is 30 seconds. And that is done by a wrapper around all actions (https://github.com/getgauge/taiko/blob/master/lib/doActionAwaitingNavigation.js)

Explicit waits:

Apart from the implicit waits mentioned above Taiko also provides various ways to do custom waits based on required to avoid arbitrary sleeps.

  • Customising default timeout: In some cases the default timeout may not be sufficient as page load or events might take more time. To address that default timeout can be update in two ways,
    1] global - use setConfig api to set navigationTimeout, retryTimeout
    2] local - set option to actions Eg: https://docs.taiko.dev/api/click/

  • Wait for events: Taiko emits CDP’s lifeCycleEvents ['DOMContentLoaded', 'loadEventFired', 'networkAlmostIdle', 'networkIdle', 'firstPaint', 'firstContentfulPaint', 'firstMeaningfulPaint'].
    Chromium uses few heuristics to determine if the page has loaded meaningfully to the user. Taiko uses readyState to be complete as the default event to wait for which ensures page load. And there were issues where the firstMeaningfulPaint event was not triggered by the browser hence moved from using that as default letting users choose to use it as per need.

  • Wait for element: There can be cases where a test's action has to be performed on a particular element only when another element exists. Taiko’s waitFor api can be used in this case with an element selector,
    Example: await waitFor(button(‘submit’))

  • Wait for a predicate: There can be cases where a test's action has to be performed on a particular element only when some condition is satisfied. Taiko’s waitFor api can be used in this case with an predicate,
    Example: await waitFor(() => button(‘submit’).isVisible())

  • Wait for a time period: As a last resort if none of the above implicit waits and explicit waits help, Tests can use waiting with time using Taiko’s waitFor api that takes time in milliseconds. Example: await waitFor(10000)