Home > database >  Why it's alert no defination about ${index} value in For IN express of Robotframework
Why it's alert no defination about ${index} value in For IN express of Robotframework

Time:12-27

Define a list which contian serveral name to check, If found 'lvbu' string, then jump out the LOOP body. But the express seems not suit for robotframwork, Could tell me why it's notice name'liubei is not define about the variable ${index}
Code:

Exit For Loop
@{items}    Create List    liubei    zhangfei    guanyu    lvbu    zhaoyun    machao
FOR    ${index}    IN    @{items}
    Run Keyword If    ${index}==lvbu    Exit For Loop
END

Trace info:

20211224 23:34:12.775 : TRACE : Arguments: [ 'liubei' | 'zhangfei' | 'guanyu' | 'lvbu' | 'zhaoyun' | 'machao' ]

20211224 23:34:12.775 : TRACE : Return: ['liubei', 'zhangfei', 'guanyu', 'lvbu', 'zhaoyun', 'machao']

20211224 23:34:12.776 : INFO : @{items} = [ liubei | zhangfei | guanyu | lvbu | zhaoyun | machao ]

20211224 23:34:12.779 : TRACE : Arguments: [ 'liubei==lvbu' | 'Exit For Loop' ]

20211224 23:34:12.785 : FAIL : Evaluating expression 'liubei==lvbu' failed: NameError: name 'liubei' is not defined nor importable as module

CodePudding user response:

The expression must be a valid python expression after robot substitutes variables. Like the error shows, the expression being evaluated becomes liubei == lvbu. That means that python (not robot) must have a variable named liubei and another one named lvbu.

If you are trying to compare strings, you must properly quote them. For example, this will work if you know ${index} itself doesn't have any quote characters:

Run Keyword If  '${index}' == 'lvbu'  Exit For Loop

Robot has a special syntax for variables used in expressions. If you leave off the curly braces, robot will create the python variable for you, which avoids the problem of having to know if the variable has quotes in it or not.

Run Keyword If  $index == 'lvbu'  Exit For Loop

For more information see the section titled Evaluating expressions in the documentation for the BuiltIn library.

  • Related