I have a resources file containing a global variable which is dependent on another global variable.
${VAR1} ${EMPTY}
${VAR2} some_value/${VAR1}.json
In my testcase I set the value for ${VAR1} using the set global variable keyword
set global variable ${VAR1} foo
log ${VAR1}
log ${VAR2}
I am expecting that the update to ${VAR1} in turn updates ${VAR2} as well so the output should be:
foo
some_value/foo.json
and not
foo
some_value/${EMPTY}.json
${Var2} is passed from command line , which has the highest priority.
In order to override it , you need to define ${var2} in your Keyword file again, as you did for ${var1}
Example
set Suite variable ${VAR1} foo
set Suite variable ${VAR2} some_value/${VAR1}.json
You can also refer this answer
Command Line Varaible is not overriding Suite Level Variable in Robot Framework
In this case, you don't need to assign anything to ${VAR1}
just declare ${VAR1} with no value inside variables section.
e.g
*** variables ***
${VAR1}
${VAR2} some_value/${VAR1}.json
In the test suite you need to declare the VAR2 as a global variable. Once VAR2 declared in the test suite the value of VAR2 will be updated everytime the VAR1 is updated.
Sample Test
log to console ${VAR1}
log to console ${VAR2}
set global variable ${VAR1} foo
${VAR2}= set variable test/ ${VAR1}
set global variable ${VAR2}
log to console ${VAR1}
log to console ${VAR2}
set global variable ${VAR1} foo2
Value of VAR2 will be now test/ foo2
log to console ${VAR1}
log to console ${VAR2}
Related
*** 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)
I have some problems using the robotframework template,how to get variables passed to the template?
Here is my code:
*** Keywords ***
re-random-value
${random int} = Evaluate random.randint(1, 5)
[Return] ${random-num}
*** Test Cases *** test1
[Template] test template
re-random-value # random-num is return value from keywords re-random-value
*** Keywords ***
test template
[Arguments] ${random-num}
log ${random-num}
when I run the test case test1, The result is re-random-value, not the number of values returned by the keyword re-random-value I expected
The documentation on Robot Framework Test Templates states the following:
... test cases with template contain only the arguments for the
template keyword.
Within the context of a test template an argument can never be a keyword, the approach in the example won't work as is.
Assuming for a moment that the keyword may change per line/test case an intermediate keyword can be constructed that takes the name of the keyword as an argument and executes it. In the below updated version of your example this is what is done. Using the FOR loop to generate multiple values.
*** Test Cases ***
test1
[Template] test template
FOR ${index} IN RANGE 1 5
re-random-value # random-num is return value from keywords re-random-value
END
*** Keywords ***
test template
[Arguments] ${keyword}
${value} Run Keyword ${keyword}
Log To Console ${value}
re-random-value
${random int} = Evaluate random.randint(1, 5) modules=random
[Return] ${random int}
Results in (bear in mind the randomness of the values):
==============================================================================
test1 1
3
2
3
| PASS |
------------------------------------------------------------------------------
Acc.to Variable scopes, we have Global Scope, Test Suite Scope, Test Case scope and Local Scope.
Referring to the Set Suite Variable documentation:
If a variable already exists within the new scope, its value will be overwritten.
My goal is to write a simple code snippet, which demonstrates this sentence in practice. This is the technical problem.
To be able to fully implement the technical goal, the new scope and current scope need an explanation. For example, how does current/new scope relate to Global Scope, Test Suite Scope, Test Case scope and Local Scope?
It's a bit hard to understand exactly what you're asking, but I think the following tests might illustrate the sentence "If a variable already exists within the new scope, its value will be overwritten."
First, this test creates a variable named ${var} in the test scope of the first test. A second test verifies that the variable isn't visible outside of the original scope.
The third test starts the same as the first test, creating a test-scoped variable. It then calls Set suite variable which overwrites the variable that is in the test scope of the third test.
Finally, a fourth test shows that the variable is now available to every test in the suite since the variable exists in the suite scope.
*** Test cases ***
Example 1
# Set a variable in a test scope
Set test variable ${var} test
Should be equal ${var} test
Example 2
# Verifies that the variable from the first test
# is not available in this new test scope
Variable should not exist ${var}
Example 3
# Set a variable in the test scope
Set test variable ${var} test
Should be equal ${var} test
# Set a suite variable that has the same name as the local variable
Set suite variable ${var} suite
# Even though we set it as a suite scope, it overwrites the local
# variable of the same name
Should be equal ${var} suite
Example 4
# Verify the suite variable from the previous test is visible
# in this new test scope
Should be equal ${var} suite
I tried to Declare and Initialize a variable in Robot Framework using Selenium platform. But I'm getting an Error Keyword name cannot be empty.
I tried the following code
Integer:
*** Test Cases ***
Test Case 1
${item} ${0} # ${}
Boolean:
*** Test Cases ***
Test Case 2
${item} ${true} #${}
String:
*** Test Cases ***
Test Case 3
${item} Stackoverflow
Kindly assist me how to declare and initialize a variable within a Test Cases block in Robot Framework.
Reply for #Goralight
I'm getting an error
You need to use the Set Variable Keyword to assign values to Variables outside the Variable Header:
*** Test Cases ***
Test Case 1
${item} Set Variable ${0} #${}
${item} Set Variable ${true} #${}
${item} Set Variable Stackoverflow
The above assigns the variable you have given in your test cases to the correct value. (This will overwrite ${item} every time of course however) But this will assign the value, to the var ${item}.
Read the Docs about it here
Any questions please ask :)
In my opinion, the following is a more readable way to do it:
*** Test Cases ***
Test Case 1
${item} = Set Variable ${0} #${}
${item} = Set Variable ${true} #${}
${item} = Set Variable Stackoverflow
You will get an error if you do the following:
${item} = Stackoverflow
The reason is that this assignment is expecting a keyword Stackoverflow on right hand side.
Here is a working example of such assignment.
*** Test Cases ***
Test Case 1
${item} = Get My Value
Log ${item}
*** Keywords ***
Get My Value
${my text} = Set Variable Stackoverflow
[return] ${my text}
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.