Refactoring a test case to stop logging an error - robotframework

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.

Related

How to add Info to custom keyword Log

In Robot Framework, when you create a custom keyword using the *** Keyword *** section of .robot file, is there a way to print an INFO message in the log file? I've tried using BuiltIn.Log keyword, but it creates a new keyword section where the INFO is written.
I want to get INFO in custom keyword this way:
Info in Keyword execution
But currently, my only option is: Info inside BuiltIn.Log definition
Is there a way to add INFO directly to my custom keyword without using Python API?
Did you try Log to console Typing text ${User} into text field 'username' like this?
To my knowledge what you are attempting, is unfortunately not doable. This way of embedding messages can be done by the robot.logger or Python's logging api - More info in the Robot Framework User Guide
However in addition to using the Log keyword, you may alleviate the need by first adding a documentation string on your keywords - the first line is always shown in the Documentation section of the keyword. Additionally by enabling Trace on the log file you'll get at least the Arguments and Return values shown on each keyword.
The Documentation is added with the [Documentation] tag similar to
Custom Keyword
[Documentation] This string is shown completely until I leave at least
... One empty row.
...
... This is shown only in the library documentation file.
And logging modes are changed with a launch option -L or --loglevel, to enable Trace mode, simply add the option when launching your robot.
robot -t TestName -s SuiteName -L TRACE .\Path\to\Tests

Re-run testsuites with Robot framework and Ride with different login information

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

How to display message in console during test when element is not found with robot framework?

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

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

How to run gherkin style test cases in Robot Framework

Is there some configuration setting that I must perform to get Robot Framework (RF) to run Gherkin/BDD style test cases?
I have installed RF 2.8.3 on Windows 7 and is running OK with Selenium2Library and DatabaseLibrary. According to the user docs and other info on the web, I should be able to write and run Gherkin style tests. However, when I do this, I get errors. RF does not strip the Gherkin keywords (Given, When, Then, ...) before trying to match the keyword:
Tests.Group001 GeneralTests
==============================================================================
Scenario: No template operation selected | FAIL |
No keyword with name 'But page does not contain a no template operation selected error message' found.
------------------------------------------------------------------------------
I run the tests using a straight-forward:
pybot ../Tests
My sample test file is:
*** settings ***
Library Selenium2Library
Library DatabaseLibrary
Library kw_common
*** Test Cases ***
Scenario: No template operation selected
Given I have logged in and I have selected to perform template configuration
When I do not select a template operation
But page does not contain a no template operation selected error message
And I press the next button
Then I should not see a template operation selected error message
*** Keywords ***
I have logged in and I have selected to perform template configuration
Log Given I have logged in and I have selected to perform template configuration
I do not select a template operation
Log No template operation selected
page does not contain a no template operation selected error message
Page Should Not Contain 'ddTemplateOperation' is required.
I press the next button
Click Element xpath=//input[contains(#id,'next')]
I should not see a template operation selected error message
Page Should Contain 'ddTemplateOperation' is required.
Help would be much appreciated. Thanks.
From the official documentation http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#ignoring-given-when-then-and-but-prefixes :
Prefixes Given, When, Then and And are dropped when matching keywords
are searched
So page does not contain a no template operation selected error message keyword needs to be renamed to But page does not contain a no template operation selected error message.

Resources