'How to Use IF and ELSE in Robot Framework
Create user //Is my test case name
${random_string}= Generate Random String 8 [LOWER]
Set Global Variable ${random_string}
${body}= create dictionary email=${random_string}@mail.com firstName=${random_string} lastName=${random_string} role=ADMIN
${response}= Post On Session mysession /user json=${body} headers=${headers} //This is One Response for POST Method
${getuserresponse}= GET On Session mysession /user headers=${headers} //This is 2nd response for GET method which return all the users
FOR ${i} IN @{getuserresponse.json()}
#Validation
IF ${i['firstName']} == ${random_string} // I want to check weather GET Response contains email that I send from POST request
Log User Created Successfully
ELSE
Log User Not Created Successfully
END
Instead it gives me Error as Evaluating IF condition failed: Evaluating expression 'ptrmipuy'(this is random_string) failed: NameError: name 'ptrmipuy' is not defined nor importable as module
Solution 1:[1]
Your conditions cannot have sequences with two or more spaces, since that's what robot uses to parse a statement. Everywhere you have ==
it needs to be ==
Also, your expressions either need to quote the string values or you can use the special syntax that converts the robot variables into python variables.
IF "${i['firstName']}" == "${random_string}"
-or-
IF $i['firstName'] == $random_string
This is covered in the documentation for the BuiltIn library, in a section titled Evaluating Expressions
Solution 2:[2]
Try below snippet of if else, if it works then copy paste this syntax and replace your variable. Also print your variable before comparing, if it has extra quotes, then you might get that error.
Also you should have only space before and after this symbol "=="
*** Test Cases ***
Testifelse
${Variation}= Set Variable NA
IF "${Variation}" == "NA"
Log if
ELSE
Log else
END
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Manish Kumar |