I would like to know is there any way to control the validations for testcase level. we have tags, which are testcase selection level. I have a testcase which covers addition and subtraction of two numbers.
Here my requirement, both actions are in the same testcase but, if I mention 'add' testcase will execute only addition part and similarly sub. if we didn't specify any specification then it has to run both the operations. IS there any way to control these kind of scenarios in Robot? kind of if/else scenario in testcase level. We will mention our input while running the script. Of course we can write it in different testcases like, one for Sub and another one for Add but, in my case i have total of 100+ testcases are with this kind of scenario.
Sample code:
| Setting | Value |
| * Test Cases * |
-----------------------------
| Testing1
| | [Tags] | PRIORITY:P0 | CATEGORY:NA | STC_DB_INDEX:NA
if {add}
| | Log | Addition of two numbers |
| | Run Keyword | addition | 20 | 25 |
if {sub}
| | Log | Subtraction of two numbers |
| | Run Keyword | sub | 10 | 5 |
The simplest solution is to split your test in two. Many QA experts think each test should test exactly one thing, and I've found that to be a successful strategy.
So, put your add validation in one test, your subtraction in another. Then when you specify the "add" tag, only the "add" tests will run.
Not sure if I understand what you need, but if you have a lot of repetition in your test you should consider a template approach like:
*** Test Cases ***
Add Variables Scenario [Template] Sum My Vars
${var1} ${var2} ${expectedResult1} # one line is one test
${var3} ${var4} ${expectedResult2}
Substract Variables Scenario [Template] Substract My Vars
${var1} ${var2} ${expectedResult1}
*** Keywords ***
Sum My Vars
[Arguments] ${value1} ${value2} ${result}
# do your validations ...
Substract My Vars
[Arguments] ${value1} ${value2} ${result}
# do other validations ...
Basically with this you can call one or other keyword and each sum / substraction will always generate a distinct test case without much code repetition.
If this is not what you are really looking for trying giving a bit more details and what you are really looking for
Related
I was trying to implement a keyword with arguments embedded in keyword, in a way that it is shown in Gherkin example. I have a keyword with multiple arguments. I was wondering if it is possible to split it to multiple lines? I wasn’t able to find this in the user guide. I tried to split as if it was a documentation, but with no luck.
*** Test Cases ***
test
When very long keyword ${argument_1}:${argument_2} with
... multiple arguments ${argument_4} is set to ${argument_5}
*** Test Cases ***
test
When very long keyword ${argument_1}:${argument_2} with \
... multiple arguments ${argument_4} is set to ${argument_5}
What is the standard solution here? Should I generally make shorter keywords?
Following your code to the letter, you basically have multiple errors of input separation.
Try something like this:
*** Test Cases ***
test 1
When very long keyword ${argument_1} ${argument_2}
... ${argument_4} ${argument_5}
test 2
When very long keyword ${argument_1} ${argument_2}
... ${argument_4} ${argument_5}
*** Keywords ***
When very long keyword
[Arguments] ${arg1} ${arg2} ${arg3}
... ${arg4} # You can also have new lines in arguments input!
Log My 4 arguments are: ${arg1}, ${arg2}, ${arg3}, ${arg4}
You can split long keywords with And like this:
*** Test Cases ***
Valid Login
Given login page is open
When valid username and password are inserted
And credentials are submitted
Then welcome page should be open
Cucumber has this option to split arguments but I never tried with RobotFramework.
Given the following people exist:
| name | email | phone |
| Aslak | aslak#email.com | 123 |
| Joe | joe#email.com | 234 |
| Bryan | bryan#email.org | 456 |
I want to use Robot Framework to write and execute Test Cases in Gherkin format.
What I want is when I execute a Test Case, output in the console besides the name
of the Scenario, each step (When, Then...) and log as well if the step passes or not.
You could achieve such functionality with a listener that uses the listener interface of the framework.
The end_keyword listener method will be invoked during execution when a keyword is finished. It will get the keyword name and its attributes as a parameter, so you can log both the name and the status.
You have to filter it so only keywords starting with Given, When, Then will be logged on the console.
Example:
ROBOT_LISTENER_API_VERSION = 2
def start_test(name, attributes):
# Add an extra new line at the beginning of each test case to have everything aligned.
print(f'\n')
def end_keyword(name, attributes):
if name.startswith('Given') or name.startswith('When') or name.startswith('Then'):
print(f'{name} | {attributes["status"]} |')
Console output for the behavior-driven development example of the user guide.
robot --pythonpath . --listener listener.py test.robot
==============================================================================
Test
==============================================================================
Add two numbers
Given I have Calculator open | PASS |
.When I add 2 and 40 | PASS |
.Then result should be 42 | PASS |
Add two numbers | PASS |
------------------------------------------------------------------------------
Add negative numbers
Given I have Calculator open | PASS |
.When I add 1 and -2 | PASS |
.Then result should be -1 | PASS |
Add negative numbers | PASS |
------------------------------------------------------------------------------
Test | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
We have sections in Robot Framework like below:
***Settings***
***Variables***
***Test Cases***
***Keywords***
And while running the file Robot framework engine try to find TestCases and execute it.
Similarly is it possible to create a custom section say General and when a class is run Keywords/Methods which are defined are executed?
It is not possible to add sections to a robot file. However, what you describe can be achieved using Robot Framework by filtering the test cased from the command line using Test Case Tags.
Given the following example:
*** Test Cases ***
Test Case General 1
[Tags] General
No Operation
Test Case General 2
[Tags] General
No Operation
Test Case Feature 1
[Tags] Feature 1
No Operation
Test Case Feature 2
[Tags] Feature 2
No Operation
Starting Robot framework with the argument:
--include General
Will result in
Test Case General 1 | PASS |
------------------------------------------------------------------------------
Test Case General 2 | PASS |
------------------------------------------------------------------------------
and
--include General --include Feature 2
Will result in
Test Case General 1 | PASS |
------------------------------------------------------------------------------
Test Case General 2 | PASS |
------------------------------------------------------------------------------
Test Case Feature 2 | PASS |
------------------------------------------------------------------------------
Is it possible to add to test Setup/Teardown procedure in Robot Framework more than one keyword?
Yes. Use the Run Keywords keyword, and use the special argument AND to separate your keywords.
For example:
*** Settings ***
| Test Setup | Run keywords
| ... | A keyword | with | arguments
| ... | AND | Another keyword | with | arguments
| ... | AND | One more keyword
Note that my use of the pipe-separated format isn't intended to imply that it's the only format you can use in this situation. You can use any supported format with this solution. For example, here is the same thing using the space-separated format:
*** Settings ***
Test Setup Run keywords
... A keyword with arguments
... AND Another keyword with arguments
... AND One more keyword
I would like to repeat the similar commands with replacing few variables in Robot framework. Could please suggest me how to do it?
Here is the sample code:
variable1 = ['abc']
varaible2 = ['123','456']
| | Run Keyword And Continue On Failure | testing | ${variable1} | ${variable2} | ${GetVal} | ${Check} |
variable3 = ['xyz']
varaible2 = ['678','789']
| | Run Keyword And Continue On Failure | testing | ${variable3} | ${variable4} | ${GetVal} | ${Check} |
Yes Robot framework supports for loops here is the example
:FOR ${animal} IN cat dog
\ Log ${animal}
\ Log 2nd keyword
Log Outside loop
For More Examples Please go through this link
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#for-loops
Robot framework provides a "for" loop with the special keyword :FOR (see For Loops in the user guide)
| | :FOR | ${v2} | IN | #{variable2}
| | | Run keyword and continue on failure
| | | ... | testing | ${variable1} | ${v2} | ${GetVal} | ${Check}
Notice that the body of the loop has an extra level of indentation.
If you want to do nested loops you'll need to create a separate keyword for the nested loop. It very quickly becomes easier to write a single python keyword that does all of the looping and logic, as robot really isn't designed to be a general purpose programming language.
here is an example which loops for a given number
: FOR ${INDEX} IN RANGE 200
\ Log To Console ${INDEX}
The other answers are very good at explaining how to write a simple FOR loop in Robot Framework, so this is added clarity for your information.
First of all, the code to do as you're asking is as follows, assuming the various unknown variables are already defined elsewhere:
*** Test Cases ***
Do Your Test
:FOR ${INDEX} IN RANGE ${INCLUSIVE_STARTING_INDEX1} ${EXCLUSIVE_ENDING_INDEX1}
\ Run Keyword and Continue On Failure testing ${variable1} ${variable2} ${GetVal} ${Check}
:FOR ${INDEX} IN RANGE ${INCLUSIVE_STARTING_INDEX2} ${EXCLUSIVE_ENDING_INDEX2}
\ Run Keyword and Continue On Failure testing ${variable3} ${variable4} ${GetVal} ${Check}
Second, I need to clarify that FOR Loops in Robot Framework are NOT Keywords. They're distinctively separate entities at the most basic level in the language. I learned this by spending hours delving into the code, trying to figure out how it might be possible to code a nestable For loop. To save you the effort of trying, it isn't without coding your own customized keyword in Python.
Also, I should specify that I'm taking the liberty of assuming that you made a few typos in your question, and that your personalized keyword "testing" that you wrote somewhere else accepts a list object as its second input variable. If that is incorrect, let me know and I'll give you a more accurate answer.
If you want only one loop you can use
:FOR ${iTemp} IN #{listOfStudents}
\ Log ${iTemp}
\ Log GO_ON.
but you can't make loop inside loop
for that you should use keyword for that like below
First Loop
:FOR ${i} IN #{listOfStudents}
\ Log ${i}
\ Log OutSide Loop
Outside Loop
:FOR ${j} IN #{ListOfSubject}
\ Log ${j}
\ Log new Kewords.
Thats the way you can use loops in Robot Framework
Thasnks
For loop syntax was enhanced in Robot Framework 3.1. New syntax is as follows:
*** Test Cases ***
Example
FOR ${animal} IN cat dog
Log ${animal}
Log 2nd keyword
END
Log Outside loop
Second Example
FOR ${var} IN one two ${3} four ${five}
... kuusi 7 eight nine ${last}
Log ${var}
END