Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

For Loop in Robot Framework Python

When using robot framework to test web application, we may need to use for loop to iterate over elements in a list or numbers in a range. The example below shows you how to use for loop.

 

Step 1: create a project in RIDE and add following test cases.

Step 2: add following test steps in each test case. This is the text view  of the test case.

 

*** Settings ***
Default Tags
Library           String

*** Variables ***
${finalstring}    ${EMPTY}
@{Values}         1    2    3    4    5    6    7
...               8    9    10    11    12
${Var}            ${EMPTY}

*** Test Cases ***
For-Loop-In-Range
    [Tags]    NoTest
    : FOR    ${INDEX}    IN RANGE    1    10
    \    Log    ${INDEX}
    Log    For loop is over

For-Loop-Elements
    [Tags]    NoTest
    @{StringItems}    Create List    Selenium    Master    Test    Automation
    : FOR    ${Item}    IN    @{StringItems}
    \    Log    ${Item}
    Log    Item iteration is over

For-Loop-Exiting
    [Tags]    NoTest
    @{ITEMS}    Create List    1    2    3
    : FOR    ${ELEMENT}    IN    @{ITEMS}
    \    Log    ${ELEMENT}
    \    Run Keyword If    '${ELEMENT}' == '2'    Exit For Loop
    Log    Exit For Loop triggered at the second element

Concatenate-String
    ${finalstring}    Catenate    Hello    World
    Log    ${finalstring}
    ${finalstring}    Catenate    SEPARATOR=---    Hello    World
    Log    ${finalstring}
    ${finalstring}    Catenate    SEPARATOR=:    Hello    World
    Log    ${finalstring}

Step 3: run the test and see the result log in RIDE console

Starting test: Tests.For-Loop-In-Range
20140727 21:28:05.852 :  INFO : 1
20140727 21:28:05.853 :  INFO : 2
20140727 21:28:05.855 :  INFO : 3
20140727 21:28:05.856 :  INFO : 4
20140727 21:28:05.857 :  INFO : 5
20140727 21:28:05.858 :  INFO : 6
20140727 21:28:05.859 :  INFO : 7
20140727 21:28:05.860 :  INFO : 8
20140727 21:28:05.862 :  INFO : 9
20140727 21:28:05.863 :  INFO : For loop is over
Ending test:   Tests.For-Loop-In-Range

Starting test: Tests.For-Loop-Elements
20140727 21:28:05.866 :  INFO : @{StringItems} = [ Selenium | Master | Test | Automation ]
20140727 21:28:05.867 :  INFO : Selenium
20140727 21:28:05.869 :  INFO : Master
20140727 21:28:05.870 :  INFO : Test
20140727 21:28:05.872 :  INFO : Automation
20140727 21:28:05.873 :  INFO : Item iteration is over
Ending test:   Tests.For-Loop-Elements

Starting test: Tests.For-Loop-Exiting
20140727 21:28:05.876 :  INFO : @{ITEMS} = [ 1 | 2 | 3 ]
20140727 21:28:05.877 :  INFO : 1
20140727 21:28:05.879 :  INFO : 2
20140727 21:28:05.881 :  INFO : Exiting for loop altogether.
20140727 21:28:05.882 :  INFO : Exit For Loop triggered at the second element
Ending test:   Tests.For-Loop-Exiting

Starting test: Tests.Concatenate-String
20140727 21:28:05.884 :  INFO : ${finalstring} = Hello World
20140727 21:28:05.885 :  INFO : Hello World
20140727 21:28:05.886 :  INFO : ${finalstring} = Hello---World
20140727 21:28:05.887 :  INFO : Hello---World
20140727 21:28:05.888 :  INFO : ${finalstring} = Hello:World
20140727 21:28:05.889 :  INFO : Hello:World
Ending test:   Tests.Concatenate-String