Sarada Akurathi
Sarada Akurathi

Reputation: 1188

How to Clear or Remove the values from List in Robot Framework

I wrote a Testcase, where it will read the login details from excel sheet and for each pair of details it will login and logout.

I am reading data from Excel, each row details i am passing to list and sending that as input to the another keyword/function for login.

After logout, i just want to remove the values in the list, it showed as succeeded but in next run it showed old values also.

I searched, i didn't get any keyword to clear all the list values.

Can one please help me on this. Here the code is:

    *** Settings ***
        Documentation     CLM Registration Test Case
        Test Teardown     Close All Browsers
        Library           Selenium2Library
        Library           Collections
        Library           ExcelLibrary
        Library           String

   *** Variables ***
    ${delay}          5s
    ${excelName}      LoginTestData.xls
    @{rowValues}
    ${rowCount}       ${EMPTY}
    ${cellCount}      ${EMPTY}

    *** Test Cases ***
    Get Data from Excel
        Open Excel Sheet    ${excelName}
        @{sheetNames}    Get Sheet Names
        ${sheetName}    Set Variable    @{sheetNames}[0]
        ${rowCount}    Get Row Count    ${sheetName}
        ${cellCount}    Get Column Count    ${sheetName}
        : FOR    ${rindex}    IN RANGE    1    ${rowCount}
        \    @{rowValues}    Get Values    ${sheetName}    ${rindex}    ${cellCount}
        \    Log to console    row values are for index ${rindex} : @{rowValues}
        \    Login to the CLM    @{rowValues}
        \    Log    cell count is : ${cellCount}
        \    Change Language to English
        \    Sleep    ${delay}
        \    Logout from CLM
        \    Remove Values    ${rowValues}    ${cellCount}

    *** 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
        : 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    ${rowValues}    ${cindex}    ${cellValue}
        [Return]    @{rowValues}

    Login to the CLM
        [Arguments]    @{rowValues}
        Open Browser    http://172.20.24.74/clm-ui/#/login/    chrome
        Maximize Browser Window
        Sleep    ${delay}
        Input Text    id=username    @{rowValues}[0]
        Input Password    id=password    @{rowValues}[1]
        Click Button    css=.btn.btn-primary

    Remove Values
        [Arguments]    ${rowValues}    ${cellCount}
        Log to console    rowvalues are : ${rowValues} and cell Count: ${cellCount}
        : FOR    ${index}    IN RANGE    0    ${cellCount}
        \    ${value}=    Remove from List    ${rowValues}    -1
        \    Log to console    value removed from list is : ${value}

    Change Language to English
        Sleep    ${delay}
        Wait Until Element Is Visible    xpath=//*[@id='top-navbar']/ul[2]/li/a/span[2]    30s
        Click Element    xpath=//*[@id='top-navbar']/ul[2]/li/a/span[2]
        Click Element    xpath=//*[@id='top-navbar']//a[contains(text(),'English')]

    Logout from CLM
        Sleep    ${delay}
        Click Element    xpath=//*[@id='top-navbar']/ul[2]/li/a/span[2]
        Click Link    link=Logout

In the output it showed as value removed, but next run, it showed old values also

out put as below:

===========================================================================================================
ReadExcel :: CLM Registration Test Case                                                                    
===========================================================================================================
Get Data from Excel                                                                                user is in Get Values function
get the data from LoginDetails[0][1]
get the data from LoginDetails[1][1]
row values are for index 1 : [u'akurasa', u'Srija210$']
rowvalues are : [u'akurasa', u'Srija210$'] and cell Count: 2
value removed from list is : Srija210$
value removed from list is : akurasa
user is in Get Values function
get the data from LoginDetails[0][2]
get the data from LoginDetails[1][2]
row values are for index 2 : [u'clmui', u'tecnotree', u'akurasa', u'Srija210$']
rowvalues are : [u'clmui', u'tecnotree', u'akurasa', u'Srija210$'] and cell Count: 2
value removed from list is : Srija210$
value removed from list is : akurasa
| PASS |
-----------------------------------------------------------------------------------------------------------

Upvotes: 4

Views: 20667

Answers (2)

matebende
matebende

Reputation: 609

In my case the list what I wanted clear was global, so i couldn't clear it with Create List. In this case a

:FOR     ${elem}    IN    @{list}
    Remove values from list    ${list}    ${elem}

helped.

Upvotes: 1

Rakesh
Rakesh

Reputation: 1575

Try using the keyword "Create List" as the last step of FOR loop instead of "Remove Values" user defined keyword. Also, move the variable ${cellCount} inside the loop if number of columns varies for each row

    Get Data from Excel
    Open Excel Sheet    ${excelName}
    @{sheetNames}    Get Sheet Names
    ${sheetName}    Set Variable    @{sheetNames}[0]
    ${rowCount}    Get Row Count    ${sheetName}

    : FOR    ${rindex}    IN RANGE    1    ${rowCount}
    \    ${cellCount}    Get Column Count    ${sheetName}
    \    @{rowValues}    Get Values    ${sheetName}    ${rindex}    ${cellCount}
    \    Log to console    row values are for index ${rindex} : @{rowValues}
    \    Login to the CLM    @{rowValues}
    \    Log    cell count is : ${cellCount}
    \    Change Language to English
    \    Sleep    ${delay}
    \    Logout from CLM
    \    @{rowValues}    Create List

"@{rowValues} Create List": This command will clear the values in the list

Upvotes: 4

Related Questions