I just started working on Robot Framework and I am trying to use Try Keyword If
keyword, but all the examples I see online show the solution in a single line whereas I have columns and rows in RIDE.
If I have a button with the ID of "Current Status" on the current page then I want to go to URL www.xyz.com and perform some action. The confusion is when I write Run Keyword If
in the first cell of a test case in RIDE, what should I write in second column? Should this be the Page Should Contain
? or Page Should Not Contain
?
Please let me know what information I am missing for above.
The first is to create a new keyword that calls all the other keywords, and then call that from Run keyword if . This might be the most readable solution, but at the expense of having to write and document another keyword. Save this answer.
If you need both the status and the returned value, you can use Run keyword and return status to run the keyword, but you'll have to modify the keyword to set a suite or global variable that your test can get after the keyword returns.
If you are using Run Keyword If, the second column must be a python expression rather than another keyword. This is explained in the keyword documentation. For example (using pipe-separated format for clarity):
| | Run keyword if | ${answer} == 42 | Go to | http://www.example.com
If you want to run a keyword only if the page has an element with the id of "Current Status", you need to first determine if the page has the element or not, and then use that in the expression. There are many ways to do this. The documentation shows how to use "Run keyword and ignore error", which would look something like this:
| | ${status} | ${value}= | Run keyword and ignore error | Page should contain | //*[@id='Current Status']
| | Run Keyword if | '${status}' == 'PASS' | Go to | http://www.example.com
There are other ways to accomplish the same thing. For example, you could get a count of how many items on the page contain the ID, and only run the keyword if the count is greater than zero:
| | # determine if something on the page has an id of 'Current Status'
| | ${count}= | Get matching xpath count | //*[@id='Current Status']
| | # if there is at least one item on the page with that id, go to xyz.com
| | Run keyword if | ${count} > 0 | Go to | http://www.example.com
If you want to perform multiple steps, such as go to the page and do some validation, the most straight-forward thing to do is create a separate keyword, and call that.
...
| | Run keyword if | ${count} > 0 | Do extra validation
*** Keywords ***
| Do extra validation
| | Go to | http://www.example.com
| | Page should contain | Hello, world
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With