Instantiate empty list in Robot Framework? - robotframework

In the Robot Framework user guide, it states a variable value is a list or list-like, it is also possible to use it as a list variable like #{EXAMPLE} and There is also an empty list variable #{EMPTY} but I am not able to create an empty list. In the minimally reproducible example below, no matter how I set it up I get the error, TypeError: Expected argument 1 to be a list or list-like, got string instead in my test case. How can I create an empty list that doesn't re-assign itself to be a string?
*** Settings ***
Library Collections
Suite Setup Re-instantiate List
*** Variables ***
#{shouldBeList}= #{EMPTY}
*** Test Cases ***
Add something to the list
Collections.Append to list #{shouldBeList} Test
*** Keywords ***
Re-instantiate List
#{shouldBeList}= Create List ${EMPTY}
Set Suite Variable #{shouldBeList} #{shouldBeList}
# Results:
# Add something to the list | FAIL |
# TypeError: Expected argument 1 to be a list or list-like, got string instead.

I should have read the user guide more closely.
All variables should be assigned using the ${var} syntax, including lists and dicts. The #{var} and &{var} syntax should be used when you want to unpack the ${var}, using the # symbol if it's list-like and & if it's dict-like.
My above code works when written as:
*** Settings ***
Library Collections
Suite Setup Re-instantiate List
*** Variables ***
*** Test Cases ***
Add something to the list
Collections.Append to list ${shouldBeList} Test
*** Keywords ***
Re-instantiate List
${shouldBeList}= Create List
Set Suite Variable #{shouldBeList} #{shouldBeList}

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']

Test Template with Robot Framework cannot use variables

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 |
------------------------------------------------------------------------------

Why I couldn't work with a dictionary in different keywords in the Robot framework?

I want to work with the dictionary in different keywords. But I get the error when I try to work with the dictionary in another keyword:
AttributeError: 'str' object has no attribute 'keys'
This is my sample code:
*** Settings ***
Library Collections
*** Variables ***
&{PRODUCTS}
*** Keywords ***
Set the Dictionary
&{PRODUCTS} = create dictionary OrderN=R0
... OrderPersonal=No_Account OrderPosition=EmptyPosition
Get the Dictionary
log dictionary \&{PRODUCTS}
*** Test Cases ***
Using the dictionary type of variables
Set the Dictionary
Get the Dictionary
set to dictionary &{PRODUCTS} OrderPosition TestPosition
Get the Dictionary
Why is the variable not accessible in another keyword? How can I resolve the issue?
The problem you are facing is because of Robot Framework variable scope. This is explained in the following Robot Framework guide section: Variable priorities and scopes.
In this case I think you should use one of the Set Test/Suite/Global Variable keywords to store the variable outside the keyword scope.
*** Keywords ***
Set the Dictionary
&{PRODUCTS} = create dictionary OrderN=R0
... OrderPersonal=No_Account OrderPosition=EmptyPosition
Set Test Variable ${PRODUCTS} &{PRODUCTS}

How can I access value of nested lists in robot framework

I want to access values of a nested list [ICMPDU,[0,1,2]] in robot framework. I want to access the element 2 in the list in robot framework. Below is my code.Below code gives me output as 'Yes'[2]. Any suggestions?
*** Settings ***
Library SeleniumLibrary
Library Collections
*** Variables ***
#{ICMPDU_Val} 'Yes' 1 2
#{ICMPDU} ICMPDU #{ICMPDU_Val}
*** Test Cases ***
Network_web_page
Log To Console #{ICMPDU}[1][2]
The first problem is that you are not creating a list inside of a list.
Consider this line:
#{ICMPDU} ICMPDU #{ICMPDU_Val}
This will create a list composed of four values: ICMPDU, Yes, 1, and 2. That is because when # is used on the right hand side of a keyword, the list is expanded and each element becomes an argument to the keyword.
From the robot framework users guide (emphasis added):
When a variable is used as a scalar like ${EXAMPLE}, its value will be used as-is. If a variable value is a list or list-like, it is also possible to use as a list variable like #{EXAMPLE}. In this case individual list items are passed in as arguments separately.
If you want the list to be the second element of the list, you must use $ instead of # when defining the variable:
#{ICMPDU} ICMPDU ${ICMPDU_Val}
The second problem is that the syntax for accessing array elements only works outside the braces if you have a single index. If you need somethime more complex, such as [1][2] you need to use extended variable syntax and move the indexes inside of the curly braces.
For example, ${ICMPDU[1][2]}
Using the Collections Library:
*** Test Cases ***
Network_web_page
${li}= Get From List ${ICMPDU} 1
${res}= Get From List ${li} 2
Gives the output:
${li} = [u"'Yes'", u'1', u'2']
${res} = 2
and the variables as :
*** Variables ***
#{ICMPDU_Val} 'Yes' 1 2
#{ICMPDU} ICMPDU ${ICMPDU_Val}

Variable Declaration and Initialization in Robot Framework within a Test Cases block

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}

Resources