Robot framework: Is there a way to write dynamic test cases? - robotframework

I am pretty new to robot framework. I would like to create test cases dynamically without having a input key-value driven approach.
Found some material that suggested the following:
suite = TestSuite('Example suite', doc='...')
tc = TestCase('Example test')
tc.add_step(TestStep('Log', args=['Hello, world!'])
suite.add_test(tc)
I dont see add_step in test case class, Will continue to look around and see if there are any solutions.

The TestSuite object has a keywords attribute which itself has a create method which can be used to create new keywords.
The robot framework api documentation gives this example:
from robot.api import TestSuite
suite = TestSuite('Activate Skynet')
suite.resource.imports.library('OperatingSystem')
test = suite.tests.create('Should Activate Skynet', tags=['smoke'])
test.keywords.create('Set Environment Variable', args=['SKYNET', 'activated'], type='setup')
test.keywords.create('Environment Variable Should Be Set', args=['SKYNET'])
The above gives you the same test as if you had written it like this:
*** Settings ***
Library OperatingSystem
*** Test Cases ***
Should Activate Skynet
[Tags] smoke
[Setup] Set Environment Variable SKYNET activated
Environment Variable Should Be Set SKYNET

Related

How to Parameterize test cases in robot framework?

I need to provide multiple inputs to a testcase using robot framework. I had done similarly in pytest with parameterization, is there any similar way to do in robot framework as well..
You can use variables for this.
for example
robot --variable HOST:10.0.0.2:1234 /testfolder/
variable ${HOST} will have value 10.0.0.2:1234 in this testrun
I think you can use Arguments using Robot framework. Keywords can accept zero or more arguments, and some arguments may have default values. It is best way to supply parameters to your testcase/keyword based on input required. More documentation can be found at - http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#using-arguments
Approach that i made:
As i'm calling my robot call only once for a suite and in test suite each indivdual test case may have different no and different variables.
I made a json file to have:
test suite name >
test case name >
test case params
max no of params in that test suite .
I'm adding a common tag as param1 param2 for test cases based on no of params for each tc and iterate the call for the robot test suite with above tags and variable as ${params} with tag name. So that only those test cases will be picked.
Param details i'm reading the json file in the test case based on the variable passed ${params}.
eg,.
robot --variable params:param1 -i param1
robot --variable params:param2 -i param2
TestCase[xxx]: Sample Test Case
[Documentation] Sample Test Case
[Tags] Sanity param1 param2 param3

Robotframework, How to define a dynamic variable name

I want to Define variable having dynamic name.
For example
${${FILE}{VAR}} Create List // ${FILE} = TEST, ${VAR} = VAR
Then I want to get variable named '${TESTVAR}'.
Here is my Summarized code...
*** Settings ***
Library SeleniumLibrary
Library ExcelLibrary
Library Collections
*** Variables ***
${VAR}= VAR
*** Keywords ***
Open Document And Assign Variable
[Arguments] ${FILE}
Open Excel Document filename=${FILE} doc_id=doc_var
${${FILE}${VAR}} Create List # It doesn't work ..
The previous answer is not really accurate.
This can be achieved exactly with robot's "Variables inside variables" feature like so:
FOR ${idx} IN RANGE 3
${var_name} = Catenate SEPARATOR=_ var ${idx}
Set Suite Variable ${${var_name}} ${idx}
END
you get:
   var_1=1
   var_2=2
   var_3=3
Please note that variables resolution is happening only for keywords arguments (at least for robot version 3.1.2 that I am using), therefore either one of 'Set test variable', 'Set Suite Variable' or 'Set Global Variable' keywords must be used. The following won't work:
FOR ${idx} IN RANGE 3
${var_name} = Catenate SEPARATOR=_ var ${idx}
${${var_name}} = ${idx}
END
results in:
    No keyword with name '${${var_name}} =' found.
For your list case you just need to create a list variable and reassign it to dynamically named variable using the above mentioned keywords
This is currently not possible with Robot Framework. You can use "variables inside variables" to resolve the values of variables (see the documentation on this topic) but not to resolve/set the name of the variable itself.
Though I am afraid that would be confusing anyway. Maybe you can explain your motivations and people can come up with another solution for your problem.

Robotframework Change testcase name with variable

Is there any possible to change testcase name with variable like below?
(I don't want to change name from python side)
*** Variables ***
${country} US
*** Test Cases ***
test_${country}
As far as I know, it isn't possible to use a variable inside a test case name. It follows the same logic as normal Python functions, so normally, it isn't possible.
Instead, you can use the variable in the setup or in the test case directly to modify it's behaviour.
If you want to generate test cases based on a variable, you can write a (python) script that can generate the needed file/test cases with the corresponding values. Or, even better, use an Model-Based Testing tool to produce them.
Yes, you can. The way you have shown it should work. Are you facing any issue with that? If yes, pls provide the detailed error.
Yes this is supported.
example:-
*** Test Cases ***
Test title ${name}
[Tags] DEBUG
Log Welcome ${name}
output:-
robot --variable name:sample eg.robot

How to use "get library instance" with global variable in robot framework

I have to define two variables ${p1} and ${p2} whose scope should be global means they can be use in various teat cases in a single test suite.
when I am doing the below activity inside test case it is working fine:
${p1}= GET LIBRARY INSTANCE P1
${p2}= GET LIBRARY INSTANCE P2
But when I am assigning p1 and p2 as global, I am not able to get the desired result:
set Suite Variable ${p1}= GET LIBRARY INSTANCE P1
set Suite Variable ${p2}= GET LIBRARY INSTANCE P2
I did not want to write ${p1}= GET LIBRARY INSTANCE P1 line in all test cases, what should I do? Any help will be appreciated.
You should define a Suite Setup in which you could set your variables for the whole suite. You should get your library instances first and then simply set those variables as suite variables like it is shown in the example.
${ID} = Get ID
Set Suite Variable ${ID}
In your case it should look like something this:
*** Settings ***
Suite Setup Setup Global Variables
*** Keywords ***
Setup Global Variables
${p1}= GET LIBRARY INSTANCE P1
${p2}= GET LIBRARY INSTANCE P2
Set Suite Variable ${p1}
Set Suite Variable ${p2}
*** Test Cases ***
Test CaseA
Log ${p1}
Log ${p2}
Test CaseB
Log ${p1}
Log ${p2}
Note that these variables will be accessible only in this suite file.
You are using invalid syntax. The documentation for Set suite variable says that it takes a variable name as the first argument and one or more values (not a keyword) as subsequent arguments. You are giving the string ${p1}= GET LIBRARY INSTANCE as the variable name, and the string P1 as the value.
The correct form is like the following. Because ${p1} exists locally you do not need to specify it when calling set suite variable.
${p1}= GET LIBRARY INSTANCE P1
set Suite Variable ${p1}

Robot: assign variables in setup phase using Run Keywords

I'm trying to create a setup phase for a test case in which I assign variables. I know in order to do multiple keywords I need to use Run Keywords, but is it possible to set variables when doing this? For example:
*** Test Cases ***
Case1
[Setup] Run Keywords
... ${var1}= Keyword1
... AND ${var2}= Keyword2
obviously the above doesn't work because ${var1} and ${var2} are just treated as arguments to Run Keywords. Since they haven't been defined yet, setup fails.
No, you cannot. Even though you added "using Run Keywords", this question has the same answer as Is possible to create new variable in suite/test set up - Robot Framework?
You can use the Set Suite Variable keywork to do that.
set suite variable ${var1} Hello World
You might need to escape the variable...
set suite variable \${var1} Hello World
From the builtin library documentation:
If a variable already exists within the new scope, its value will be overwritten. Otherwise a new variable is created. If a variable already exists within the current scope, the value can be left empty and the variable within the new scope gets the value within the current scope.
The question here why are you trying to do this?
The way I do it, if I want to call keywords and set their outputs in variables
to reuse them in my test suite, I do the following:
*** Settings ***
Library BuiltIn
Suite Setup Initialize Variables
*** Keywords ***
Initialize Variables
${Argument1} = Set Variable some_value
${output1} = Keyword1 ${Argument1}
${output2} = Keyword2
${output3} = Keyword3 ${Argument1} other_value
*** Test Cases ***
Test Case 1
# Here you can use the variables that you initialized in the Suite Setup.
Log ${output1}
Log ${output2}
Log ${output3}
Test Case 2
# Also here you can use the same variables.
No Operation
Note: If you want to setup the variables for each test case, you can do it either in the settings section like this:
*** Settings ***
Test Setup Initialize Variables
Or you can use the setting in the test case itself (same as what you did in your question)
Test Case 1
[Setup] Initialize Variables
Note that "Initialize Variables" can take arguments as well if you need.

Resources