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"]
Related
I have done some test suites with Robot framework and Ride including several testcases
which requires starting of web browser and user login to website.
Without just copying the testsuites/testcase and hardcode other browser and user login,
how can I re-use and re-run existing ones just changing first the used browser,
then re-run the same with user2?
Test Cases are something like:
Open Browser - Chrome
#Open Browser - Firefox
Login1
#Login2
Do something....
#For loop already used inside the testcase
FOR ${var} IN IN RANGE 1 10
Exit For Loop If Some condition is true
${var} = Evaluate ${var} + 1
#do something until condition is true
Scroll Element Into View test_element
Set Focus To Element test_element
END
Continue test case....
Keywords:
Open Browser - Chrome
Open Browser https://test.testpage.com/ chrome
......
Open Browser - Firefox
Open Browser https://test.testpage.com/ ff
......
Login1
Wait Until Element Is Visible test_Login_username
Input Text test_Login_username test.id1
Input Password test_Login_password test.password1
Click Button test_Login_LoginBtn
.....
Login2
Wait Until Element Is Visible test_Login_username
Input Text test_Login_username test.id2
Input Password test_Login_password test.password2
Click Button test_Login_LoginBtn
I know that for example I could set the browsers inside for-loop like
*** Variables ***
#{BROWSERS} Chrome ff
And the loop them inside the testcase like
FOR ${Browser} IN #{BROWSERS}
Open Browser https://test.testpage.com/ ${Browser}
END
But the broblem in this is, that you can't use nested for-loops??
It's true you can't have nested loops in RF. It's also true that there's an easy workaround as documented in the user guide:
*** Keywords ***
Handle Table
[Arguments] #{table}
FOR ${row} IN #{table}
Handle Row #{row}
END
Handle Row
[Arguments] #{row}
FOR ${cell} IN #{row}
Handle Cell ${cell}
END
Your code examples seems a bit messy to me, so I won't go into using these as an example. But in general, I'd:
create a keyword with a for loop that represents some test steps
implement a test case where I loop over different browsers executing the keyword from 1. inside the for loop
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
i want to know how to display a message in console when an element is not found with robot framework:
I tried this:
S2L.Wait Until Page Contains Element ${checkbox} 10s checkbox not found
It's not working for this element and don't know why. It works for any other element but not for my checkbox.
So for now i have this:
Wait Until Keyword Succeeds 5 times 2 sec S2L.Click Element ${checkbox}
But when it fails it says only the element not found bu i would prefer to code a personalized message.
Any help is welcome.
Thank you
Although I do think that your problem is something different, your question can be answered. Run Keyword And Return Status will capture the error and continue and provide a status. Run Keyword If then allows for using the keyword Fail and it will also generate a message on the console.
*** Settings ***
Library SeleniumLibrary
Suite Teardown Close All Browsers
*** Test Cases ***
Wait And Click succesfully
Open Browser http://google.com HeadlessChrome
Wait and Click Element name:q This should work
Wait And Click unsuccesfully
Open Browser http://google.com HeadlessChrome
Wait and Click Element name:nobtn This should not work
*** Keywords ***
Wait and Click Element
[Arguments] ${locator} ${message}=None
${status} Run Keyword And Return Status
... Wait Until Keyword Succeeds
... 5 times 2 sec
... Click Element ${locator}
Run Keyword If
... "${status}" == "False"
... Fail ${message}
How about using the error argument available with the Wait Until Page Contains Element keyword?
Wait Until Page Contains Element id=elementId timeout=60s error=Display whatever you want, e.g. Lorem Ipsum...
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.
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.