Print

Ajax Synchronization Robot Framework Python

When testing web application, if web page loads data with jQuery Ajax call, we need to wait until the active ajax call is completed to interact with the element. In Selenium Robot Framework, we can use For Loop and Exit For Loop methods to achieve the ajax dynamic Ajax Synchronization.

Step 1: launch RIDE and create a test suite "WaitForAjaxComplete" and add a variable ${IsAjaxComplete} and import library "Selenium2Library".

Step 2: add a new test case "AjaxTest". The project structure will look like this below

 Step 3: write the following test steps in the "AjaxTest" case

Text version of the test case

*** Settings ***
Library           Selenium2Library

*** Variables ***
${IsAjaxComplete}    ${EMPTY}

*** Test Cases ***
AjaxTest
    [Tags]    RunThis
    Open Browser    http://www.cars.com    Firefox
    Maximize Browser Window
    Select From List    name=make    Acura
    Sleep    2s
    Select From List    name=model    RDX
    Sleep    2s
    Select From List    name=prMx    $5,000
    Sleep    2s
    Select From List    id=select-rd-new    50 Miles
    Sleep    2s
    Input Text    name=zc    21020
    Sleep    2s
    Click Element    xpath=//div/a[@class='search-start button']
    : FOR    ${INDEX}    IN RANGE    1    5000
    \    ${IsAjaxComplete}    Execute JavaScript    return window.jQuery!=undefined && jQuery.active==0
    \    Log    ${INDEX}
    \    Log    ${IsAjaxComplete}
    \    Run Keyword If    ${IsAjaxComplete}==True    Exit For Loop
    Page Should Contain Element    id=print
    Close Browser

Run the test and see the log below

Starting test: DatabaseTest.WaitForAjaxComplete.AjaxTest
20140726 13:01:33.366 :  INFO : Opening browser 'Firefox' to base url 'http://www.cars.com'
20140726 13:01:46.015 :  INFO : Selecting option(s) 'Acura' from list 'name=make'.
20140726 13:01:48.589 :  INFO : Slept 2 seconds
20140726 13:01:48.590 :  INFO : Selecting option(s) 'RDX' from list 'name=model'.
20140726 13:01:50.967 :  INFO : Slept 2 seconds
20140726 13:01:50.971 :  INFO : Selecting option(s) '$5,000' from list 'name=prMx'.
20140726 13:01:53.344 :  INFO : Slept 2 seconds
20140726 13:01:53.346 :  INFO : Selecting option(s) '50 Miles' from list 'id=select-rd-new'.
20140726 13:01:55.985 :  INFO : Slept 2 seconds
20140726 13:01:55.988 :  INFO : Typing text '21020' into text field 'name=zc'
20140726 13:01:58.226 :  INFO : Slept 2 seconds
20140726 13:01:58.229 :  INFO : Clicking element 'xpath=//div/a[@class='search-start button']'.
20140726 13:01:58.482 :  INFO : 
Executing JavaScript:
return window.jQuery!=undefined && jQuery.active==0
20140726 13:01:58.511 :  INFO : ${IsAjaxComplete} = False
20140726 13:01:58.513 :  INFO : 1
20140726 13:01:58.514 :  INFO : False
20140726 13:01:58.517 :  INFO : 
Executing JavaScript:
return window.jQuery!=undefined && jQuery.active==0
20140726 13:02:02.671 :  INFO : ${IsAjaxComplete} = True
20140726 13:02:02.673 :  INFO : 2
20140726 13:02:02.675 :  INFO : True
20140726 13:02:02.677 :  INFO : Exiting for loop altogether.
20140726 13:02:02.721 :  INFO : Current page contains element 'id=print'.
Ending test:   DatabaseTest.WaitForAjaxComplete.AjaxTest

Based on the log, we know that in the 2nd iteration in the loop, the ${IsAjaxComplete}=True, so the for loop ended. With this method, we can achieve Ajax Dynamic Synchronization in Selenium Robot Framework. 

Click the link to see the log.