Waits in Robot Framework

While automating web applications, elements may not appear immediately due to page load time, AJAX calls, or dynamic content.

If the test tries to interact with an element before it is ready, it will fail. That’s why waits are crucial.

Types of Waits

a) Hard Waits (Sleep)

Pauses the test for a fixed time.

Example:

Sleep    5s


Problem: Slows tests unnecessarily if the element appears earlier, and may still fail if the element takes longer.

b) Explicit Waits

Waits dynamically until a specific condition is met, up to a maximum timeout. Recommended over Sleep.

Example:

Wait Until Element Is Visible    id=logoutBtn    10s


Explanation:

     - Checks repeatedly if the element with id=logoutBtn is visible.
     - Stops waiting as soon as the element appears.
     - Waits up to 10 seconds before failing the test.

Explicit Wait keywords in SeleniumLibrary:

Keyword

Description

Example

Wait Until Element Is Visible

Waits for an element to become visible

Wait Until Element Is Visible id=username 15s

Wait Until Element Is Not Visible

Waits until an element disappears

Wait Until Element Is Not Visible id=loading 20s

Wait Until Page Contains

Waits until the page contains specified text

Wait Until Page Contains Welcome 10s

Wait Until Page Does Not Contain

Waits until the page no longer contains specific text

Wait Until Page Does Not Contain Loading 15s

Wait Until Element Contains

Waits until an element contains specific text

Wait Until Element Contains id=message Success 10s


Why Use Waits Instead of Sleep?

     - Dynamic: stops as soon as the condition is met.
     - Reduces test execution time.
     - Prevents flaky tests caused by timing issues.
     - Handles slow-loading elements gracefully.

Best Practices

     - Prefer explicit waits over hardcoded Sleep.
     - Set reasonable timeout values (5–15 seconds for most elements).
     - Use locators that uniquely identify elements for accurate waits.
     - Combine waits with assertions to validate that elements are ready before performing actions.

Example Usage of Waits

*** Test Cases ***
Login And Wait Example
    Open Browser    https://example.com/login    chrome
    Input Text      id=username    Yogesh
    Input Text      id=password    1234
    Click Button    id=loginBtn
    
    # Wait until logout button is visible before interacting
    Wait Until Element Is Visible    id=logoutBtn    10s

    
    Page Should Contain    Welcome
    Close Browser


Explanation:

     - The test waits dynamically for the logoutBtn to appear.
     - Once visible, it continues and validates that the page contains “Welcome”.
     - This prevents failures due to slow page load.
 

Related Tutorials