Waits in Robot Framework

The Robot Framework provides keywords that can be used to implement implicit or explicit wait in our automation scripts. Waits are an essential part of automation as many times elements take time to load on a web page. 

The wait commands that are available in the robot framework will help us to pause the script execution for the desired period before throwing an element not found exception.  

In this blog, we will discuss- 

  • How to apply implicit waits 
  • How to apply explicit waits 

Project setup for Waits-

Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/

To ensure that the required libraries are installed, use pip list command or manually navigate to: 

File > settings > project: robot automation> python interpreter 

Project setup

After verifying the prerequisites, create a file named ‘Waits’  

— Create the file under the ‘Testcases’ directory with .robot extension >Enter 

File name

Implicit Wait: 

 Implicit waits are applicable to all the elements in a web page at a global level. 

Some of the keywords that are provided by the robot framework to apply implicit waits are: 

  • Set Selenium Implicit Wait: Sets the Implicit wait value used by selenium. 

This keyword sets the implicit wait for all opened browsers. By applying implicit wait, we can ask the web driver to wait for a specific amount of time before it throws an exception. 

  • Get Selenium Implicit Wait: Gets the Implicit wait value used by selenium. 

This keyword will return the value of the implicit wait time. The value is returned as a human-readable string, e.g., 1 second. 

  • Set Browser Implicit Wait: Sets the implicit wait value used by Selenium. 

Same as Set Selenium Implicit Wait but only affects the current browser. 

Explicit Wait: 

Explicit waits are specifically applied to an element intended by us, instead of all the elements of the web page. It is most useful when we get certain elements on a webpage that need a longer time to load. 

 Keywords that are provided by the robot framework to apply explicit waits are: 

  • Wait Until Page Contains – Waits until text appears on the current page 
  • Wait Until Page Contains Element – Waits until the element locator appears on the current page  
  • Wait Until Page Does Not Contain – Waits until the text disappears from the current page  
  • Wait Until Page Does Not Contain Element – Waits until the element locator disappears from the current page  
  • Wait Until Location Is – Waits until the current URL is expected  
  • Wait Until Location Is Not – Waits until the current URL is not location  
  • Wait Until Location Contains – Waits until the current URL contains expected 
  • Wait Until Location Does Not Contain – Waits until the current URL does not contain the location  
  • Wait Until Element Contains – Waits until the element locator contains text  
  • Wait Until Element Does Not Contain – Waits until the element locator does not contain text  
  • Wait Until Element Is Enabled – Waits until the element locator is enabled  
  • Wait Until Element Is Not Visible – Waits until the element locator is not visible  
  • Wait Until Element Is Visible – Waits until the element locator is visible 

Given below is an example for a better understanding of how we can apply waits in our test 

*** Settings ***
Library    SeleniumLibrary 
*** Variables ***
${browser}  chrome
${url}  https://opensource-demo.orangehrmlive.com/
*** Test Cases ***
ImplicitWait Test
    ${implicit_wait}=   get selenium implicit wait      #will give default implicit wait that is set
    log to console    ${implicit_wait}      #will print the default implicit wait
    set selenium implicit wait    10seconds     #will set a new implicit wait time
    open browser    ${url}  ${browser}
    maximize browser window
    input text    xpath://input[@id="txtUsername"]  Admin
    input text    xpath://input[@id="txtPassword"]  admin123
    click element    xpath://input[@id="btnLogin"]
ExplicitWait Test
    go to        https://demo.anhtester.com/
    maximize browser window
    wait until page does not contain    OrangeHRM
    wait until page contains    Selenium Easy - Best Demo website to practice Selenium Webdriver Online
    click element    xpath://a[@id='btn_basic_example']
    wait until page contains element    xapth://a[@class='list-group-item'][normalize-space()='Simple Form Demo']
    click element    xpath://a[@id='btn_inter_example']
    wait until page does not contain element    xpath://a[@class='list-group-item'][normalize-space()='Simple Form Demo']
    click element    xpath://a[contains(text(),'Input Form with')]
    wait until location is    https://demo.anhtester.com/input-form-demo.html
    go back
   wait until location is not    https://demo.anhtester.com/input-form-demo.html
    wait until page does not contain    input-form-demo.html
    wait until location contains    demo.anhtester.com
    go to   https://demo.anhtester.com/ajax-form-submit-demo.html
    wait until element contains        xpath://div[normalize-space()='Ajax Form']   Ajax Form
    wait until element is visible   xpath://textarea[@id='description']
    wait until element is enabled    xpath://input[@id='btn-submit']
    go back
    wait until element is not visible    xpath://textarea[@id='description']
    close browser

***Settings*** – To work with robot framework keywords we need to import   Library SeleniumLibrary in the settings section because all the keywords are built in the selenium library. Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section. 

***Variables*** – This section contains the defined variables that are used commonly, here we declared ${url} and ${browser} variables for browser and URL so that we can reuse these variables. In the same way, we can create more variables according to the project requirement. 

Code explanation:  

Let us understand the code by diving into different parts 

We will first understand the implicit wait test: 

*** Settings ***
Library    SeleniumLibrary
*** Variables ***
${browser}  chrome
${url}  https://opensource-demo.orangehrmlive.com/
*** Test Cases ***
ImplicitWait Test
 ${implicit_wait}=   get selenium implicit wait      #will give default implicit wait that is set 
       log to console    ${implicit_wait}      #will print the default implicit wait
    set selenium implicit wait    10seconds     #will set a new implicit wait time
    open browser    ${url}  ${browser}
    maximize browser window
    input text    xpath://input[@id="txtUsername"]  Admin
    input text    xpath://input[@id="txtPassword"]  admin123
    click element    xpath://input[@id="btnLogin"]
Waits code

In this test case, we used get selenium implicit wait keyword, stored it into a variable named ${implicit_wait} in order to get the default implicit wait that is provided, and printed the default implicit wait value using log to console command, then we used set selenium implicit wait keyword to set a new implicit wait time to 10 seconds.  

Next, we visited https://opensource-demo.orangehrmlive.com/ URL on the chrome browser, after which we redirected to a login page, where we entered the username, and password and clicked the log-in button. 

Now, let us understand the explicit wait test: 

ExplicitWait Test
    go to        https://demo.anhtester.com/
    maximize browser window
    wait until page does not contain    OrangeHRM
    wait until page contains    Selenium Easy - Best Demo website to practice Selenium Webdriver Online
    click element    xpath://a[@id='btn_basic_example']
    wait until page contains element    xapth://a[@class='list-group-item'][normalize-space()='Simple Form Demo']
    click element    xpath://a[@id='btn_inter_example']
    wait until page does not contain element    xpath://a[@class='list-group-item'][normalize-space()='Simple Form Demo']
    click element    xpath://a[contains(text(),'Input Form with')]
    wait until location is    https://demo.anhtester.com/input-form-demo.html
    go back
    wait until location is not    https://demo.anhtester.com/input-form-demo.html
    wait until page does not contain    input-form-demo.html
    wait until location contains    demo.anhtester.com
    go to   https://demo.anhtester.com/ajax-form-submit-demo.html
    wait until element contains        xpath://div[normalize-space()='Ajax Form']   Ajax Form
    wait until element is visible   xpath://textarea[@id='description']
    wait until element is enabled    xpath://input[@id='btn-submit']
    go back
    wait until element is not visible    xpath://textarea[@id='description']
    close browser
Waits code 2

In this test case, we first visited https://demo.anhtester.com/ URL on chrome browser, after which we redirected to another web page, we then used wait until page does not contain keyword to check that the page does not contain the title of the previous web page and wait until page contains to check that page contains the title of the new page to ensure that we have been redirected to a new web page.  

After loading the web page, we clicked on one of its elements using the click element keyword, which takes us onto another page where we wait until we get one of its elements using the wait until page contains element and we clicked on an element using the click element keyword, to get onto another page, then used wait until page does not contain element keyword to ensure that we have visited the other page by not having the element of the previous page. 

We then used the wait until location is a keyword with the URL to open a new page until it is successfully loaded and then went back to the previous page using the go back keyword.

Next, we used wait until location is not keyword with the URL to ensure that the current page does not contain the specified URL, then we used wait until page does not contain keyword with a specific substring that is not a part of the current page URL, and wait until location contains keyword with a specific substring that is a part of the current page URL. 

Next, we used the go to keyword to go to another page and used the wait until element contains keyword to wait until we get the text of the element from the page visited, and wait until element is visible to ensure that the specific element of the page is visible, wait until element is enabled to ensure that the specific element of the page is enabled and then used the go back keyword to again get back to the previous page.  

Lastly, we used wait until element is not visible keyword to ensure that the element of the previous page is not visible, and we have loaded the next page successfully. Then we ended the test and closed the browser using the close browser keyword. 

Execution: 

To execute the test case we need to use the command below: 

Execution command

After execution, we should get the result as a pass. 

Pass result

Reports: 

After every execution, the robot framework creates detailed HTML reports. 

We get three files – log.html, output.xml, and report.html. 

Log.html: 

Waits log
Test statistics

Report.html: 

Waits Report

Read our next blog on “How to handle Web Tables in Robot Framework”.