How to run gherkin style test cases in Robot Framework - robotframework

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.

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.

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 test Asyncronous services using robot framework

I have been using robot framework for testing my Rest APIs?
But I have no clue if we can test asynchronous services. I started to look for robotframework-async library but still could not figure it out.
Robot Framework is able to test asynchronous services. The trick is using wait conditions before your verification.
Try:
*** Test Cases ***
Load And Verify Table Data
Click Button To Load Table
Verify First Row Details
*** Keywords ***
Click Button To Load Table
Click Element ${SOME_BUTTON}
Wait Until Element Is Enabled ${SOME_ELEMENT_TO_WAIT_FOR}
Verify First Row Details
Table Row Should Contain ${TABLE_LOCATOR} 1 ${SOME TEXT}
Or something like that. Just whatever you do, don't use Sleep keywords. You will regret it later.

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