Search code examples
nested-loopsrobotframework

Data driven scenario for Robot Framework


I want to have username and pwd set in variable file and as the test steps through each username, it advances to the next row of data.

I am using resource files to drive the data, and my initial FOR script processes username 1 (eeny), then the nested FOR loop passes all of the passwords at once.

*** Variables  ***
@{users}  eeny  meeny  miny  moe
@{pwds}  pwd  pwd1  pwd2  pwd3

*** Test Cases ***

Login Test
    :FOR    ${u}    IN    @{users}
    \   Open Browser  ${URL}
    \   set window size  1440  900
    \   set selenium speed  .5
    \   input text  id=username  ${u}
    \   :FOR  ${p}  IN  @{pwds}
    \   \  input text  id=password  ${p}
    \   \  click button  css=button.primary
    \   \  wait until page contains  css=p.logout
    \   \  capture page screenshot
    \   \  click element  css=p.logout

Currently I am trying for a nested FOR statement, or a FOR statement that declares both username and password variables in the same time, but am not opposed to changing tactics for getting the data and using a multi dimensional array or similar if that worked better.


Solution

  • Data Driven testing can be achieved through 'Data Driven through Test Template' or Read data from Excel or csv.

    'Data Driven through Test Template'
    In this we will create a user defined keyword which is nothing but the function or source code which we want to execute for multiple times. that we declare as 'Test Template' in Settings section.
    when we give the TestCase name with arguments , it will execute Test Template each time.
    here the sample code

    *** Settings ***
    Documentation     This Test Suite Deals with Data Driven Testing using Test Template Method
    Test Teardown     Close All Browsers
    Test Template     Validate Login with Differnt Valid Credentials
    
    *** Variables ***
    ${URL}            http://ururl
    ${Browser}        chrome
    ${Title}          HomePage
    ${delay}          5s
    
    *** Test Cases ***    USERNAME    PASSWORD
    Test User             user1       user1
    
    Admin User            admin       admin
    
    *** Keywords ***
    Validate Login with Differnt Valid Credentials
        [Arguments]    ${Uname}    ${Pwd}
        Open the Browser and enter the URL    ${URL}    ${Browser}
        Enter the User Name    ${Uname}
        Enter the Password    ${Pwd}
        Click on Submit button
        Verify HomePage displayed    ${Title}
    
    Open the Browser and enter the URL
        [Arguments]    ${URL}    ${Browser}
        Open Browser    ${URL}    ${Browser}
    
    Enter the User Name
        [Arguments]    ${UserName}
        Sleep    ${delay}
        Input Text    id=username    ${UserName}
    
    Enter the Password
        [Arguments]    ${Password}
        Input Text    id=password    ${Password}
    
    Click on Submit button
        Click Button    css=.btn.btn-primary
    
    Verify HomePage displayed
        [Arguments]    ${Title}
        Sleep    ${delay}
        Title Should Be    ${Title}
    

    'Data driven by read from Excel'
    Read the values from Excel sheet and run multiple times,
    here the sample code

    *** Settings ***
    Documentation     CLM Registration Test Case
    Test Teardown     Close All Browsers
    Library           Selenium2Library
    Library           Collections
    Library           ExcelLibrary
    Library           String
    
    *** Variables ***
    ${delay}          2s
    ${excelName}      LoginTestData.xls
    ${rowCount}       ${EMPTY}
    ${cellCount}      ${EMPTY}
    ${URL}            http://Ur test Test URL
    ${Browser}        chrome
    
    *** Test Cases ***
    ReadFromExcelSheet
        Open Excel Sheet    ${excelName}
        @{sheetNames}    Get Sheet Names
        ${sheetName}    Set Variable    @{sheetNames}[0]
        ${rowCount}    Get Row Count    ${sheetName}
        ${cellCount}    Get Column Count    ${sheetName}
        #for loop to read all the rows in excel
        : FOR    ${rindex}    IN RANGE    1    ${rowCount}
        \    @{rowValues}    Create List
        \    @{rowValues}    Get Values    ${sheetName}    ${rindex}    ${cellCount}
        \    Log to console    row values are for index ${rindex} : @{rowValues}
        \    Sleep    ${delay}
        \    Open Browser    ${url} ${Browser}
        \    Enter User Name    @{rowValues}[0]
        \    Enter Password     @{rowValues}[1]
        \    Click on Submit Button
    
    *** Keywords ***
    Open Excel Sheet
        [Arguments]    ${excelName}
        Open Excel    ${excelName}    useTempDir=False
    
    Get Values
        [Arguments]    ${sName}    ${row}    ${cCount}
        Log to console    user is in Get Values function
        @{rValues}    Create List
        : FOR    ${cindex}    IN RANGE    0    ${cCount}
        \    Log to console    get the data from ${sName}[${cindex}][${row}]
        \    ${cellValue}    Read Cell Data By Coordinates    ${sName}    ${cindex}    ${row}
        \    Insert Into List    ${rValues}    ${cindex}    ${cellValue}
        [Return]    @{rValues}
    
    
    Open The Browser
           [Arguments]    ${url}    ${Browser}
           Open Browser   ${url}    ${Browser}
    
    Enter User Name    
           [Arguments]    username
           Input Text    id=username    username
    
    
    Enter Password     
           [Arguments]   password
           Input Password    id=password    password
    
    Click on Submit Button
           Click Button    css=.btn.btn-primary