Mia
Mia

Reputation: 448

How to fetch particular value based on key from a list using robot framewrok?

I am trying to read the value from a comma-separated list using Robot framework. I am facing issues while testing the below code

I have tried Robot framework code

 Method.robot
 @{list}=[('Param_Name', 'Param_Value'), ('res_name', 'res123'), ('id', 
 '931')]
 ReadCSV
   [Arguments]       ${paramname}
    ${dict1}=    Set Variable     ${list}
    ${values}=    Set Variable    ${dict1[0]}[${paramname}]
    [return]  ${values}


 RobotFile.robot
   ${Read_Name}=    ReadCSV        res_name
   log to console        ${Read_Name} #prints None

 Error:when executing RobotFile.robot
 List '${dict1[0]}' used with invalid index 'res_name'.

Expected: res123 Actual : NONE

Upvotes: 0

Views: 513

Answers (1)

asprtrmp
asprtrmp

Reputation: 1062

That's a list of tuples, not a dictionary. That's why you cannot directly access an index by key.

If you still wanna have a list of tuples, you should first loop through the list and evaluate if the first value is the "key" and access the other value after that.

ReadCSV
[Arguments]    ${paramname}
: FOR    ${list_item}     IN    @{list}
\    ${values}=    Run Keyword If    '${list_item[0]}' == '${paramname}'    Set Variable    ${list_item[1]}    ELSE    Set Variable    ${None}
\    Run Keyword If    '${list_item[0]}' == '${paramname}'    Exit For Loop
[Return]  ${values}

Upvotes: 1

Related Questions