3
votes

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 perform a check if a page includes the word "closed". If it does, refresh the page. If it doesn't, click element "this" and proceed with the rest of the scenario.

*** 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" does not appear in the page. "this" is clicked. Then the test fails because:
Page should have contained element 'xpath=//*[text()='Closed']' but did not

Please, help me in correcting it.

Edit: changed Page Should Contain to Page Should Contain Element. Same results.

1

1 Answers

4
votes

The argument after the ELSE should be another keyword.


What effectively happened is that on this line

${ClickThis}  Click Element  xpath=//*[@id="this"]

you've assigned value to ${ClickThis} - which is nothing, ${None}, as Click Element doesn't return a value.

On this line, you're again assigning ${Closed} the return value of Page Should Contain

${Closed}  Page Should Contain  xpath=//*[text()='Closed']

, which is not you expect it does - that itself keyword either fails (as was in your case) if the text is not there, or silently passes; but doesn't return a value.

So, to get to what you try to accomplish, use Run Keyword And Return status for the "page should contain" - it 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']

Now ${Closed} will have value of ${True} if the text was in the page, ${False} otherwise; use that in the Run Keyword If block, passing a keyword as the 2nd argument:

Run Keyword If    ${Closed}  Reload Page    ELSE    Click Element  xpath=//*[@id="this"]