Shizzleliz
Shizzleliz

Reputation: 55

Robot framework Return value from keyword in for loop is't passed through

For a test I made 2 keywords. The general idea is to check in a range of objects if a certain field, fieldname and message is present. If the field or fieldname is absent the keyword has to return to forloop. The keyword has to return ${index} and ${result}. When I run this test and it fails the variable ${results} is shown as ${result}=None and when the test passes it becomes ${result}= [3, u'PASS']

I think this has something to do with the ${result}= in the forloop

I tried Run keyword and return but it didn't work, i am a little confused where to put the ${variables} and where to use it. Can anyone help me to figure this out?

FORLOOP:

${index}= |  set variable |   0
${result}= | Set Variable |  not started
: FOR   | ${value} |   IN RANGE |  ${index}  | 15
        \    ${resultaat}= |"other keyword" | ${index} 
        \    ${index}= |   "keyword add 1 to index" |   ${index}
        \    Return From Keyword if  |  '${result}'=='PASS'
${testsuccess}= |  Set Variable If | '${result}'=='PASS' | TEST GESLAAGD

the 'other keyword' is:

[ARGS] ${index} 
${index}= | set variable | ${index}
${check1}= <IS FIELD THERE>
${result}= | set variable if | ${check1}==False | FIELD NOT THERE
return from keyword if | ${check1}=False
${check2}= <HAS FIELD VALUE X>
${result}= | set variable if | ${check2}==False | WRONG VALUE
return from keyword if | ${check2}=False
${check3}= <IS MESSAGE X>
${result}= | set variable if | ${check3}==False | FAIL
${result}= | set variable if | ${check3}==True | PASS
[RETURN] ${index} ${result}

LOG: keyword = '${result}'=='PASS' FOR = '${testsucces}=None' (But that is not right because the condition has been met)

Upvotes: 1

Views: 5500

Answers (3)

Balaji Dinakaran
Balaji Dinakaran

Reputation: 385

Try the below code for return inside the for loop

*** Keywords ***
ReturnFromForLoop
    FOR    ${i}    IN RANGE    1    5
       Return From Keyword    ${i}
    END
ReturnFromForLoopWithIf
    FOR    ${i}    IN RANGE    1    5
       Return From Keyword If    ${i}==2    ${i}
    END

Upvotes: 0

Shizzleliz
Shizzleliz

Reputation: 55

It works when i put ${result} after the return from keyword if

for example:

return from keyword if | ${check1}=False | ${result}

Upvotes: 0

Jan Kovař&#237;k
Jan Kovař&#237;k

Reputation: 1582

Set Variable If sets the value to None once there isn't any 'Else' option and the condition isn't met. See doc: http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Variable%20If

So adapt your code:

${result} | Set Variable If | ${check3}==False | FAIL
... | ${check3}==True | PASS

as shown in documentation examples.

Upvotes: 1

Related Questions