Run Keyword If in RobotFramework - robotframework

Could you please help me out that how Run Keyword If works in RIDE?
I want to click the "EXIT" button if there is any online error in the page after clicking on "Create" Button. I have put the code like this. But it did not work. After running, the test case did not fail but at the same time, the EXIT button was not clicked also.
Click Button xpath=//*[#id="divHeader"]/table/tbody/tr/td[5]/input
${Result} Page Should Contain Element //*[#id="divError"]
Run Keyword If '${Result}'=='PASS' Click Button xpath=//*[#id="MyForm"]/div[4]/table/tbody/tr/td[2]/input
Expected: As there is an online error, It should click the "Exit" button.

Page should contain doesn't return a result. It either throws an exception or returns None.
If you need to get the pass/fail status of a keyword you need to use Run keyword and return status. However, it doesn't return "Pass" or "Fail". It returns the a boolean (either True or False).
${result} Run keyword and return status
... Page should contain element //*[#id="divError"]
You can use the value directly as the condition in Run keyword if, like the following:
Run keyword if ${result}
... Click Button xpath=//*[#id="MyForm"]/div[4]/table/tbody/tr/td[2]/input

Related

how to click button till there is no such element?

I have test case to delete files one by one, i want it to click each file till there is no file.
but actual execution just delete one file and stopped.
is that i need to implement for loop?
please advise how to do it in robot.
Delete All Files
Wait Until Page Contains And Click ${SETTINGS-FILES}
${FILES} Run Keyword And Return Status Wait Until Page Contains Element ${ANDROID-WIDGET-LISTVIEW}\[#resource-id="FilesList"] 2s
Run Keyword If ${FILES} Wait Until Page Contains And Click ${DELETE-FILES}
Run Keyword If ${FILES} Wait Until Page Contains And Click ${OK}
Run Keyword If ${FILES} Wait Until Page Does Not Contain Element ${ANDROID-WIDGET-LISTVIEW}\[#resource-id="$FilesList"]
Run Keyword If ${FILES} Wait Until Page Contains No Files
A very rough solution
FOR ${i} IN RANGE 999999
YOUR TEST
Exit For Loop If CONDITION TO EXIT
END

can we provide 2 actions with "Run keyword if" built in function for robot framework

Run Keyword If '${var1}'=='#{var2}[1]' Run Keyword And Return Status Check for Help Tab Click on Help button
This is the example code, i have to perform. i have to perform two actions for this using robot framework with RIDE platform, but it is showing me error like expected 0 arguments, got 1. i understand the error, but what if i have to perform 2 actions here itself or i have to put another keyword 'Click on help button' in the first keyword to 'Check for help tab'.
Using 'Run Keywords'
You can run the keyword run keywords, at which point you can run multiple keywords.
Example:
*** Test cases ***
Example
run keyword if 1 == 1 run keywords
... log this is a normal log
... AND log this is a warning WARN
... AND log to console this is a log to the console
Using a custom keyword
Your other option is to create a custom keyword that does everything you need it to do, and call that keyword:
Example:
*** Keywords ***
Do some logging
log this is a normal log
log this is a warning WARN
log to console this is a log to the console
*** Test cases ***
Example
run keyword if 1 == 1 Do some logging
The error says that Check for Help Tab needs no arguments, but one was given. The given argument was the second keyword: Click on Help Button.
I know two ways you can do this, i recommend the first:
1) Define a new keyword:
Check Help Tab and Click Help Button
Check for Help Tab
Click on Help Button
and use it like this:
Run Keyword If '${var1}'=='#{var2}[1]' Run Keyword And Return Status Check Help Tab and Click Help button
or
2)
Run Keyword If '${var1}'=='#{var2}[1]' Run Keyword And Return Status Check Help Tab
Run Keyword If '${var1}'=='#{var2}[1]' Run Keyword And Return Status Click Help button

Robot Framework Run Keyword If .. ELSE fails

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.
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"]

How to use if and else condition in robotframework ,If node a as a child it should click the child else it should click node b

How to use if and else condition in robotframework ,If 'A'node as a child it should click the child node, else it should click node 'B'.
xpath=(.//*[#id='functionals-tab-content']/ul/li/ul/li[${to clickplus firstplus}]/ul/li[${child node click value}]/ul/li[${child node click value1}]/div[1]/a)--- child node of A.
xpath=(.//*[#id='functionals-tab-content']/ul/li/ul/li[${to clickplus firstplus}]/ul/li[${child node click value}]/ul/li[${child node click value1}])--- B node.
"Run Keyword If Element Should Be Visible child node of A. click element child node of A
Run Keyword Unless Element Should Be Visible B node. click element B node.
If give like this its not exceuting throwing error. how to write if and else condition for this. can anyone help me.
Run keyword if requires a python expression; you can't substitute that with a keyword. You'll have to split your statements into two. First, call your keyword and save the result, and then use the result in the if statement.
${result}= Run keyword and ignore error Element should be visible ${node A}
Run keyword if '${result[0]}' == 'PASS'
... click element ${node A}
... ELSE
... click element ${node B}
An example using Run keyword if and Run keyword and ignore error is included in the documentation for the built-in library (specifically, in the documentation for Run Keyword If)
Note: Run keyword and ignore error returns a tuple of two values. The first value will be the string 'PASS' or 'FAIL'.
Note: the use of continuations (...) is not necessary to make the keywords work. Personally I find they make the code considerably easier to read than if you try to put all of that on a single line.

Refactoring a test case to stop logging an error

I have a Robot Framework keyword that looks like this:
_Open Search Form If Not Open
${status} ${error} Run Keyword And Ignore Error Page Should Contain Element ${PATIENT SEARCH FORM}
Run Keyword If '${status}'=='FAIL' Click Element ${PATIENT SEARCH BUTTON}
It's intended to only be run if ${PATIENT SEARCH FORM} isn't open. However, whenever pybot detects that Page Should Contain Element is false, it logs this as an error in the test log. The test cases that use this keyword pass, and you need to dig into the log to see the failure, but it's still there. It's not intended to be a failure, though, and I don't want it logged as such.
The real pain is this: I am using Selenium2Library for my tests, and one of its default import options is run_on_failure=Capture Page Screenshot. I like this functionality, but whenever Page Should Contain Element fails and writes a 'FAIL' message to the test log, this functionality fires. Then the screenshots that are created clutter up my log folder and give a false impression that a test has failed when it hasn't.
What I would like is to either refactor this keyword to not log a failure, or somehow disable Selenium2Library's screenshot functionality for just this keyword. I used Run Keyword And Ignore Error to try to get pybot to ignore the error and not write it to the log, but I must be misinterpreting the meaning of "ignore" here. A part of the problem is my use of Page Should Contain Element. I'm using a verify keyword, but really, I'm asking "Does the page contain this element?" and not verifying that it does or does not. I haven't found anything in Selenium2Library that would just return the status of a page element without trying to make an assertion on top of it. But what I'm essentially trying to do is write a conditional statement.
Then the screenshots that are created clutter up my log folder and give a false impression that a test has failed when it hasn't.
Here is my code to save the screenshots only failed tests
*** Settings ***
Library Selenium2Library run_on_failure=Nothing
Test Teardown Test Teardown
*** Test Cases ***
Simple test
${status} ${error} Run Keyword And Ignore Error Page Should Contain Element ${PATIENT SEARCH FORM}
Run Keyword If '${status}'=='FAIL' Click Element ${PATIENT SEARCH BUTTON}
Pass Execution
*** Keywords ***
Test Teardown
Run Keyword If Test Failed Selenium2Library.Capture Page Screenshot
I believe what you are expecting will not be possible. The keyword will still be logged and shown as FAIL in your log.html even though your test is passed. To control the screenshot, you can use #Dmitriy Zverev solution.
Here's my solution:
_Open Search Form If Not Open
${previous kw}= Register Keyword To Run On Failure None
${status} ${error} Run Keyword And Ignore Error Page Should Contain Element ${PATIENT SEARCH FORM}
Register Keyword To Run On Failure ${previous kw}
Run Keyword If '${status}'=='FAIL' Click Element ${PATIENT SEARCH BUTTON}
I used Register Keyword To Run On Failure to disable screenshot capture temporarily.

Resources