Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use "Run Keyword If" in robot framework

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.

like image 804
roger referabal Avatar asked Nov 05 '14 17:11

roger referabal


People also ask

How do you run multiple keywords in if condition in Robot Framework?

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.

How do I use run keyword and return status?

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.


1 Answers

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
like image 185
Bryan Oakley Avatar answered Sep 28 '22 16:09

Bryan Oakley