simplify/implizit variable assignment in robotframework - robotframework

consider the following robotframework code example:
*** Variables ***
${VAR_1} any value
${VAR_2} another value
*** Test Cases ***
For Example only
${VAR_1}= Some Conversion ${VAR_1}
${VAR_2}= Some Conversion ${VAR_2}
A User Keyword ${VAR_1} ${VAR_2}
Desired Notation
A User Keyword Some Conversion ${VAR_1} Some Conversion ${VAR_2}
*** Keywords ***
Some Conversion
[Arguments] ${value_to_convert}
${value_to_convert}= Catenate ${value_to_convert} Foobar
[Return] ${value_to_convert}
A User Keyword
[Arguments] ${arg1} ${arg2}
Log ${arg1}
Log ${arg2}
Question: is there a possibility to simplify the working testcase For Example only to the (non working) Desired Notation - or - can I somehow use the return value of a keyword to be passed as parameter without doing an explicit assignment before?
For clarification:
Some Conversion would be far more complex and is implemented within
a jrobotremotelibrary
Moving the assingments to A User Keyword is
no useful solution, because there will be many keywords with
different amount of parameters using the same functionality

Yes, it is possible. You can write your own keywords that call other keywords which are passed in as arguments
It would look something like this:
*** Keywords ***
A User Keyword
[Arguments] ${keyword1} ${var1} ${keyword2} ${var2}
${var1}= Run keyword ${keyword1} ${var1}
${var2}= Run keyword ${keyword2} ${var2}
log ${var1}
log ${var2}
You would use this keyword exactly like in your example:
A User Keyword Some Conversion ${VAR_1} Some Conversion ${VAR_2}

The argument value assignment of a keyword can not be the return value of another keyword.
As highlighted by #Bryan Oakly it is possible to mimic the appearance with some clever usage of Run keyword, as you highlighted this will not work as the assignment may not always be using keywords or even keywords with the same number of arguments.
So, the best approach is what you've already been doing, assigning the value to a variable and then the variable to the keyword argument.

Related

Build a Robot Framework list from several keyword calls

I have several Robot Framework keywords that return a basic string.
#keyword
def keyword_one():
return 'one'
#keyword
def keyword_two():
return 'two'
In a robot test case, I try to build a list with this items, but I can't figure out how to do that is one line.
*** Test Cases ***
Test Case List
#{my_list}= Create List Keyword One Keywork Two
I tried several syntax but can't make it work.
Of course, something like below works (hardcoded values).
*** Test Cases ***
Test Case List
#{my_list}= Create List one two
Thanks for your help.
At the time that I write this, robot doesn't have the ability to call keywords inline, so what you want isn't directly possible.
You could write your own keyword to do this, however. The keyword can accept multiple keywords as arguments, and use run keyword from the built-in library to run the keyword.
For example, the following keyword definition creates a keyword that creates a list of results from multiple keywords:
Keyword written in python
If you want to try this out, name the file example.py
from robot.libraries.BuiltIn import BuiltIn
builtin = BuiltIn()
def create_list_from_keywords(*keywords):
result = []
for keyword in keywords:
result.append(builtin.run_keyword(keyword))
return result
Example test
*** Settings ***
Library example.py
*** Keywords ***
Keyword one
return from keyword one
Keyword two
return from keyword two
*** Test cases ***
Example
#{actual}= create list from keywords Keyword one Keyword two
#{expected}= create list one two
Should be equal ${actual} ${expected}
Robot-based keyword definition
If you're uncomfortable with python, here's a robot-based keyword definition:
Create list from keywords
[Arguments] #{keywords}
[Return] #{result}
#{result}= create list
FOR ${keyword} IN #{keywords}
${keyword result}= Run keyword ${keyword}
Append to list ${result} ${keyword result}
END
At the moment you are adding the keywords to the list, not the values returned from running those keywords
You would need to call the keywords to get the returned values and add them to the list e.g.
*** Test Cases ***
Test Case List
${keyword_one_val} Keyword One
${keyword_two_val} Keyword Two
#{my_list}= Create List ${keyword_one_val} ${keyword_two_val}
log to console ${my_list}
which outputs:
['one', 'two']

In robot framework is there a method to print the variable value of the embedded argument in keyword rather than the variable name?

*** keywords ***
Do something on ${var1} and ${var2}
Log ${var1}
Log ${var2}
*** Test Cases ***
Testing
${id1} Set Variable variable1
${id2} Set Variable variable2
Do something on ${id1} and ${id2}
When we run the above robot testcase the logs display ${id1} and ${id2} as variables in the keyword.
KEYWORD ${id1} = BuiltIn . Set Variable variable1
KEYWORD ${id2} = BuiltIn . Set Variable variable2
KEYWORD Do something on ${id1} and ${id2}
Is it possible in robot to have the log print the value of the variable instead of the variable itself in such use case, where the embedded arguments are variables?
For example can we have log print
KEYWORD Do something on variable1 and variable2
Sometimes its worthy to read the official documentation.
Log To Console - https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Log%20To%20Console
Also, you have Log ${var1} and it should be Log ${var1} (more than one space acts as separator)

How to perform defered variable substitution in Robot Framework?

I have this code snippet
*** Keywords ***
My Keyword
[Arguments] ${arg}
${xpath} Set Variable really long dynamic xpath using ${arg}
Do Something With ${xpath}
etc.
My Other Keyword
[Arguments] ${arg}
${xpath} Set Variable really long dynamic xpath using ${arg}
Do Something Totally Different With ${xpath}
etc.
To follow programming best practices, I want to have the xpath (the same in both keywords) defined at single place only. I tried to modify it like this
*** Variables ***
${xpath_template} really long dynamic xpath using \${arg}
*** Keywords ***
My Keyword
[Arguments] ${arg}
${xpath} Evaluate ${xpath_template}
Do Something With ${xpath}
etc.
My Other Keyword
[Arguments] ${arg}
${xpath} Evaluate ${xpath_template}
Do Something Totally Different With ${xpath}
etc.
In other words, I need the substitution of ${arg} in ${xpath_template} be defered until the moment ${arg} is defined. However, the code above does not perform it, nor my similar experiments when I tried to force Set Variable or ${{...}} to do it for me... Can you help me, please, to have the xpath be only once in my code?
The solution is to use the Replace Variables keyword.
*** Variables ***
${xpath_template} blah blah \${arg}
*** Keywords ***
My Keyword
[Arguments] ${arg}
${xpath} Replace variables ${xpath_template}
Return from keyword ${xpath}
*** Test Cases ***
Example
${xpath}= My keyword Hello, world
Should be equal ${xpath} blah blah Hello, world

Running test case In loop instead of copy the code

I have several tests in robot. the Idea of all the tests is identical:
- load some parameter to module
- run
- compare expected to actual results
The only thing is different from test to test, is the input and the expected results.
I would like to run the test repeatedly but with different inputs- and each iteration will be considered as different test case - Instead of copy the same code for all of the test cases and change the inputs.
each iteration will have its own test case tag \ documentation \ name (lets say the iteration number)
for example:
FOR ${TC} IN #{TCS} #TCS is array of inputs and expected output
*** Test Cases ***
# edit the tag \ documentation \ test name
module.load ${TC['input']}
${output} = module.run
isValid ${output} ${TC['expectedOutput']}
END
Is it possible in robot?
Thanks:)
The easyest way is to define a Keyword with Arguments and Return Values.
You can call this Keyword in every Tescase (with specified tag/documentation etc), with specified Arguments for the Test and check the Reuturn Values.
You could use the test template feature of the framework, especially the template tests with for loop.
Using it would give the following advantages:
No code duplication. You need one keyword with the test logic, that will be invoked with all list elements.
Each iteration will be independent from the other. So if one iteration fails, the next will be still executed.
It is flexible. The number of iterations is dynamic, you can create an input list in a test or suite setup phase.
Example, note that I am providing my inputs from a variable file.
*** Settings ***
Variables VariableFile.py
*** Test Cases ***
Math test
[Template] Multiplication by 2
FOR ${TC} IN #{TCS}
input=${TC.input} output=${TC.output}
END
*** Keywords ***
Multiplication by 2
[arguments] ${input} ${output}
${result}= Evaluate ${input}*2
Should Be Equal As Integers ${output} ${result} Calculated result:${result} is not equal with the expected:${output}
Variable file:
class DataSet(object):
def __init__(self, input, output):
self.input = input
self.output = output
def __str__(self):
return f'i:{self.input} - o:{self.output}'
TCS = [DataSet(1,2), DataSet(2,4), DataSet(3,6), DataSet(3,7), DataSet(4,8)]
This is the output:

How can I check if a variable is inside a list during a for loop in robot framework?

first time posting here. I've a feeling that this is a really dumb question, but for some reason my code keeps failing and I just can't put my finger on what's wrong.
Here's is what I have:
*** Settings ***
Library Selenium2Library
Library OperatingSystem
Library String
Library Collections
*** Test Cases ***
Test Robot Framework Logging
#{ALLOWED}= Create List /page1 /page2 /page3
${ControllersList}= Get File ${EXEC_DIR}/Resources/controllers.txt
#{PAGES}= Split to lines ${ControllersList}
:FOR ${PAGE} IN #{PAGES}
\ Run Keyword If '${PAGE} IN #{ALLOWED}' Log Testing WARN
[Teardown] Close Browser
This is the output:
Evaluating expression ''/page1 IN [u'/page1', u'/page2', u'/page3']'' failed: SyntaxError: invalid syntax (<string>, line 1)
If I change the condition to something like this it works:
'${PAGE} == /page1'
I checked the documentation and it seems that the IN condition could be used. I'm totally lost here. Any hints? Thanks!
This is the proper way to do the expression:
Run Keyword If $PAGE in $ALLOWED Log Testing WARN
By removing the quotes and the curly braces, robot is able to treat PAGE and ALLOWED as python variables when evaluating the expression.
From the section Evaluating Expressions in the documentation for the BuiltIn library:
Starting from Robot Framework 2.9, variables themselves are automatically available in the evaluation namespace. They can be accessed using special variable syntax without the curly braces like $variable. These variables should never be quoted, and in fact they are not even replaced inside strings.
Also, when using keywords like Run Keyword If, the expression must be a valid python expression after variable substitution. Therefore, you must use the operator in rather than IN. The latter is used only by the robot :FOR statement.
Example
*** Variables ***
#{ALLOWED} /page1 /page2 /page3
*** Test Cases ***
Example that passes
${PAGE}= set variable /page1
Run Keyword If $PAGE in $ALLOWED
... pass execution ${PAGE} is allowed
fail ${PAGE} is not allowed
Example that fails
${PAGE}= set variable /page99
Run Keyword If $PAGE in $ALLOWED
... pass execution ${PAGE} is allowed
fail ${PAGE} is not allowed
The check was almost right, but you've effectively turned the Boolean expression into a String by surrounding it with the quotes. Here's the syntax that'll do it:
\ Run Keyword If $PAGE in $ALLOWED Log Testing WARN
Mind there are no curly brackets {} around the variable names - thus the check is (almost) the straight python's variable in another_variable
The documentation Evaluating Expressions does indeed specify that in construction used in the evaluation itself. An alternative approach is to use the Collections library keyword Get Match Count. This will return 0 when no results are found, and not generate a test failure.
*** Settings ***
Library Collections
*** Test Cases ***
Test Robot Framework Logging
#{ALLOWED}= Create List /page1 /page2 /page3
#{PAGES}= Create List /page1 /page3 /page5
:FOR ${PAGE} IN #{PAGES}
\ ${InList}= Get Match Count ${ALLOWED} ${PAGE}
\ Run Keyword If ('${InList}' == '0') Log To Console Page Not Allowed
\ ... ELSE IF ('${InList}' == '1') Log To Console 1 Page Found
\ ... ELSE Log To Console More pages found.

Resources