Robot Framework Run Keyword If .. ELSE not working
I am new to Robot Framework and I am trying to use Run Keyword If .. ELSE ..
.
What it should do:
Add a new keyword to check if the page contains the word "closed". If so, please refresh the page. If not, click the "this" element and continue the rest of the script.
*** Keywords ***
Check if anything is closed
${ClickThis} Click Element xpath=//*[@id="this"]
${Closed} Page Should Contain Element xpath=//*[text()='Closed']
Run Keyword If ${Closed} =='PASS' Reload Page ELSE ${ClickThis}
What happens when I run it:
"Closed" is not displayed on the page. "this". Then the test fails because:Page should have contained element 'xpath=//*[text()='Closed']' but did not
Please help me fix it.
Edit: changed Page Should Contain
to Page Should Contain Element
. The same results.
source to share
The argument after ELSE
must be another keyword.
Effectively what happened on this line
${ClickThis} Click Element xpath=//*[@id="this"]
you assigned a value ${ClickThis}
- nothing ${None}
, because it Click Element
does not return a value.
In this line you are assigning the ${Closed}
return value againPage Should Contain
${Closed} Page Should Contain xpath=//*[text()='Closed']
that you don't expect it to be - it is the keyword itself either fails (as it did in your case) if the text is missing or silently passes; but doesn't return a value.
So, to achieve what you are trying to accomplish, use the Run keyword and return status for "page must contain" - this will return True / False according to the exit status of the wrapped keyword:
${Closed}= Run Keyword And Return Status Page Should Contain xpath=//*[text()='Closed']
${Closed}
Will now matter ${True}
if the text was on the page, ${False}
otherwise; use this in a block Run Keyword If
, passing the keyword as the second argument:
Run Keyword If ${Closed} Reload Page ELSE Click Element xpath=//*[@id="this"]
source to share