how to send screenshots from robotframework to reportportal - robotframework

I have a robot testcase to upgrade my box...
If there is any error, the robot framework takes the screen shot and saves the screenshot.png in reports dirctory..
Now how do i send it to reportportal.in
I am passing the reportportal info while running the robot like below..
robot --outputDir /opt/robotframework/reports --listener robotframework_reportportal.listener -v RP_UUID:07-aeb0-315c81358edd -v RP_ENDPOINT:http://<reportportalipaddress>:8080 -v RP_LAUNCH:TEST_UPGRADE -v RP_PROJECT:TEST_UPGRADE /opt/robotframework/tests
My robot testcase
***Test Cases***
FROM_GUI
Close All Browsers
Open Browser ${URL} gc
Input Text name:username admin
Input Password name:password &{${CPE}}[cpe_password]
Click Button name:Continue
Log to Console Inside GUI ${uploadPath}//${uploadFile}
input text name=uploadFile ${uploadPath}//${uploadFile}
Page Should Contain firmware update is in progress
sleep 10 seconds
click link link=Logout
Close All Browsers
Sleep 180 seconds
with open("../", "rb") as image_file:
file_data = image_file.read()
rp_logger.info("Some Text Here",
attachment={"name": "selenium-screenshot-1.png",
"data": file_data,
"mime": "image/png"})
[Teardown]
In Report portal
I just see the below, can't see the screenshot
</td></tr><tr><td colspan="3"><img src="selenium-screenshot-1.png" width="800px">

I have tried 2 ways to do this (there can be more):
To embed the image into the report and it would also be embedded to the reportportal report.
Using the Run-on-failure functionality provided by SeleniumLibrary:
SeleniumLibrary has a handy feature that it can automatically execute a keyword if any of its own keywords fails. By default, it uses the Capture Page Screenshot
keyword. More info is available on the official documentation
By default the Capture Page Screenshot has the value for file name as
filename = selenium-screenshot-{index}.png
You can provide a custom keyword to the run_on_failure argument while loading the Selenium library.
In the custom keyword call the Capture Page Screenshot with filename=EMBED as shown below.
*** Settings ***
Library SeleniumLibrary run_on_failure=Capture Screenshot and embed it into the report
*** Keywords ***
Capture Screenshot and embed it into the report
Capture Page Screenshot filename=EMBED
Note: When you embed the image into the report, the image file will not be present as a separate file in your Result folder.
Capturing the screenshot and sending it as an attachment:
Create a custom python library with the below function robotframework-reportportal documentation , eg: utilityLib.py:
from robotframework_reportportal import logger
#keyword
def send_attachment_to_report_portal(path):
with open(path, "rb") as image_file:
file_data = image_file.read()
logger.info("Some Text Here",
attachment={"name": "test_name_screenshot.png",
"data": file_data,
"mime": "image/png"})
Similar to the steps in the 1st solution: Call the custom keyword via the Run-on-failure functionality
*** Settings ***
Library SeleniumLibrary run_on_failure=Capture Screenshot and attach it to reportportal
Library utilityLib.py
*** Keywords ***
Capture Screenshot and attach it to reportportal
${path} = Capture Page Screenshot
Send attachment to report portal ${path}

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

Robot framework, RIDE, keyword errors

I use the following configuration for Robot Framework:
Python 3.7.8
Robot Framework: 4.0.2
RIDE: 1.7.4.2
I tried the challenge of robocorp. After many attempt, I tried out the solution on the website, but a lot of keywords are not active. Despite of the fact, I imported the needed Libraries.
I copied the code in RIDE, just made one modifications: I used *** Test Cases *** instead of *** Tasks ***, because it caused an error.
The following keywords are not active:
Open Available Browser
Click Button
Capture Element Screenshot
Input Text
My code:
*** Settings ***
Library Selenium2Library
Library OperatingSystem
Library String
Library ExcelLibrary
Library Collections
Library RPA.Excel.Files #Library RPA.Tables
Library RPA.Excel.Application
Library DateTime
Library RPA.Browser.Selenium
Library RPA.HTTP
*** Test Cases ***
Start The Challenge
Open Available Browser http://rpachallenge.com/ chrome
Download http://rpachallenge.com/assets/downloadFiles/challenge.xlsx overwrite=True
Click Button Start
Fill The Forms
${people}= Get The List Of People From The Excel File
FOR ${person} IN #{people}
Fill And Submit The Form ${person}
END
Collect The Results
Capture Element Screenshot css:div.congratulations
[Teardown] Close All Browsers
*** Keywords ***
Get The List Of People From The Excel File
Open Workbook challenge.xlsx
${table}= Read Worksheet As Table header=True
Close Workbook
[Return] ${table}
Fill And Submit The Form
[Arguments] ${person}
Input Text css:input[ng-reflect-name="labelFirstName"] ${person}[First Name]
Input Text css:input[ng-reflect-name="labelLastName"] ${person}[Last Name]
Input Text css:input[ng-reflect-name="labelCompanyName"] ${person}[Company Name]
Input Text css:input[ng-reflect-name="labelRole"] ${person}[Role in Company]
Input Text css:input[ng-reflect-name="labelAddress"] ${person}[Address]
Input Text css:input[ng-reflect-name="labelEmail"] ${person}[Email]
Input Text css:input[ng-reflect-name="labelPhone"] ${person}[Phone Number]
Click Button Submit
Error Message:
[ WARN ] Profiles are supported only with Chrome
[ WARN ] Profiles are supported only with Chrome
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.
ERROR: The system was unable to find the specified registry key or value.
[ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: Multiple keywords with name 'Capture Page Screenshot' found. Give the full name of the keyword you want to use:
RPA.Browser.Selenium.Capture Page Screenshot
Selenium2Library.Capture Page Screenshot
Start The Challenge | FAIL |
BrowserNotFoundError: Failed to start a browser:
Chrome: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
BrowserNotFoundError: Failed to start a browser:
Chrome: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
Can someone tell me, what did I wrong? Thanks a lot.
You should have pasted the exact error you have.
You are using old library name for SeleniumLibrary.
You are probably wanting to use the Browser library, and you missed the installation of it.
EDIT
After uninstall, replace Selenium2Library by SeleniumLibrary.
Better not even use SeleniumLibrary if using RPA.Browser.Selenium.
More people having problems with Open Available Browser here.

is Eclipse RED has feature to search Keywords from imported libraries

I installed Eclipse( Mars ) + RED + RobotFramework-EclipseIDE plugin.
I am writing test scripts in Eclipse.
Earlier i used RIDE to write testcases, there we have Keyword Search functionality to search keywords from the imported libraries.
I want to know, is same functionality exists in Eclipse also?
Thanks
Sarada
Create a folder named "robot-indices" under your project home.
Under "robot-indices" create a file with name "library_name.index"
Add all the keywords which come under your library to this file, one keyword per line.
e.g.
<keyword1>
<keyword2>
.... so on
Now whenever you open your test file with "robotframework editor" and add below settings
*** Settings ***
Library <Library_name>
*** Test Cases ***
Test Name
<Cursor here>
in your test file. On adding Library reference under setting and hitting "Ctrl + Space" with cursor at that point. You will get a drop down with list of your keywords under that library!
Hope it helps!

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.

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