Robot Framework dynamic list variable - robotframework

I have a for loop in which I would like to create list and add values to there.
FOR ${i} IN RANGE 3
*Create list List_${i}
Add values to list
END
How could I do it?
So that after exit the for loop, I would have list_1 & list_2 and list_3
Meaning I can create dynamic variable like this:
FOR ${idx} IN RANGE 3
${var_name} = Catenate SEPARATOR=_ var ${idx}
Set Suite Variable ${${var_name}} ${idx}
END
Maybe it is some easy way to do the same with #list ?
This does not work:
FOR ${i} IN RANGE 3
Log ${i}
${var_name}= Catenate SEPARATOR=_ TEST_NAME ${i}
Log ${var_name}
#{${${var_name}}}= Create List data
END

I think this is the answer:
FOR ${i} IN RANGE 3
Log ${i}
Set Suite Variable #{List${i}} #{EMPTY}
Append To List ${List${i}} ${i}
END

Note sure if this is exactly what you are looking for; but, I'll try to help.
First, lists cannot be empty; so you will need at least an initial value which you can then remove (or ignore) when you iterate thru the list. That said, I can suggest you create a For-Loop which returns is List-of-Lists since your "loop range may change".
Test-Dynamic-Lists-Creation
${listOfLists} Create List initial-value
FOR ${index} IN RANGE 3
${List_$index} Create List initial-value
Append To List ${listOfLists} ${List_$index}
END
Comment Removes the first value from the list.
Remove From List ${listOfLists} ${0}
Log List ${listOfLists}
Return From Keyword ${listOfLists}

Related

How to put a variable value with semicolons and forwardslashes inside a list and get read by it

Here's my list
#{referentiel} N1(exemple)/N2(exemple) N2(exemple)/exemple
where i use it
Visca barca
[Arguments] ${index}
Click Element Using Javascript //div[#id='mat-select-value-5']
Click element of long page //span[#class='mat-option-text'][contains(.,'#{referentiel}[${index}]')]
And my testcase
we are the best
Visca barca 1
Error :
Value of variable '#{referentiel}[${index}]' is not list or list-like.
The issue you are describing here has nothing to do with the semicolons or slashes. The issue is actually that you are attempting to return the list item with wrong decorator.
To get a single item from a list you should use $ instead of #. When you use # Robot will attempt to return you a list which it cannot since the item on ${index} is a string.
See below:
#{referentiel} # == ['N1(exemple)/N2(exemple)', 'N2(exemple)/exemple']
or
${referentiel[0]} # == N1(exemple)/N2(exemple)

How to use if-else statements after picking a random number

I'm able to pick a random number from a list but now I don't know how to use the if-else statement so that it should match to condition to get further details with that number.
I tried doing it with "Run Keyword If" command but it didn't work out for me. Also tried using "For loop" as well and it didn't work out as well.
*** Variables ***
#{PARTY} ABC XYZ RTY DOG
*** Test Cases ***
View all Details
${value} = Evaluate random.choice($PARTY) random
input text ${SEARCH_BAR} ${value}
log to console \nvalue: ${value}
click element ${SEARCH_BUTTON}
:for ${ELEMENT} IN #{PARTY}
\ log ${ELEMENT}
\ run keyword if '#{ELEMENT}' == 'ABC'
\ ... else if '#{ELEMENT}' == 'XYZ'
\ ... else if '#{ELEMENT}' == 'RTY'
\ ... else '#{ELEMENT}' == 'DOG'
Expected Result: Now when the random value is picked, the value should be compared with the list and when the condition is true, then I should be able to click that element to get further details of that party.
Actual Result: Random value is picked & FOR loop contains no keywords.
You have several issues.
You use a list varibale #{ELEMENT for comparison instead of a scalar one.
In your Run Keyword If statement you have no keyword to run if the condition is true. The same for ELSE IF lines.
else if should be capitalized since it is a reserved keyword.
ELSE needs no condition at all, it handles all case which are not handled by the previous conditions.
You pick a random ${value} but don't use it. Iterating over a list will give you every condition true once.
If you want to use a random value, you don't need a for-loop at all. The code should look like this:
View all Details
${ELEMENT} = Evaluate random.choice($PARTY) random
log ${ELEMENT}
Run Keyword If '${ELEMENT}' == 'ABC' Your Keyword One Here
... ELSE IF '${ELEMENT}' == 'XYZ' Your Keyword Two Here
... ELSE IF '${ELEMENT}' == 'RTY' Your Keyword Three Here
... ELSE Your Keyword Four Here

How to sort a list and return the value in Robotframework

I have a use case for which I have to automate the following steps:
Create an empty list
Push data into the empty list
Keep/save the original order in a variable
Sort the original order
Save the sorted list
Reverse the sorted list and return value
*** Settings ***
Library SeleniumLibrary
Library Collections
*** Keywords ***
Sort order verification
#{username_list}= Create List //creates an empty list
#{get_name}= Get WebElements css=#userTable > tbody > tr> td:nth-child(1)
:FOR ${each} IN #{get_name}
\ ${get_username}= Get Text ${each}
\ Append To List ${username_list} ${get_username} //pushes data into list in iteration
${original_order}= Copy list ${username_list} //returns original order
${sorted_list}= Sort List ${original_order} //sorts the list but returns none(nothing is saved in the variable
${reverse_sorted_list}= Reverse List ${sorted_list} //returns AttributeError: 'NoneType' object has no attribute 'reverse'
The Sort List and Reverse List keywords modify the list in-place, e.g. they change the value of the target variable.
They also don't return anything - thus on the lines you've used them, you have assigned the value None to the variables, which led to the error.
You can read about this behavior in the Collections library documentation

Robot framework: comparing if a list value is on another list

I am generating random strings seen in list 1 and I seeing to dB via UI. List2 is the query results from the corresponding table. I want to make sure if the values of List 1 is a present in the List 2
${Masterlist} values is [('SVAARtBKECJILJP', None, 'ONFSxMKP', None, 'xFRFAWNI', 'JOxTVMNW')]
${childlist} value is ['xFRFAWNI', 'JOxTVMNW', 'SVAARtBKECJILJP', 'ONFSxMKP']
list should contain sub list ${master} ${randomlist}
Test result : Following values were not found from first list: xFRFAWNI, JOxTVMNW, SVAARtBKECJILJP, ONFSxMKP
Any help will be appreciated!! Thanks in advance
It looks like ${Masterlist} contains a single tuple.
You could try:
List Should Contain Sub List ${MasterList[0]} ${childlist}
In your example, the values that you are attempting to access are contained in a tuple, which is inside a list.
[ # Start of a list
( # Start of a tuple
'SVAARtBKECJILJP',
None,
'ONFSxMKP',
None,
'xFRFAWNI',
'JOxTVMNW'
)
To properly verify ${Masterlist} contains ${childlist}, you will need to reference the values within the tuple, rather than the list.
Should Be True Evaluate ${Masterlist[0]} == ('SVAARtBKECJILJP', None, 'ONFSxMKP', None, 'xFRFAWNI', 'JOxTVMNW')
If ${Masterlist} contains multiple tuples, you can flatten them using some python magic, via the Evaluate keyword:
# Say ${Masterlist} equals [('some', 'thing'), ('another', 'thing')]
${Masterlist} Evaluate [item for tup in $Masterlist for item in tup]
Should Be True Evaluate ${Masterlist} == ['some', 'thing', 'another', 'thing']
For more information on flattening tuples within lists check out this thread that I stole the snippet from
Granted, this kind of logic is usually better off in a library keyword.

Robot framework, generic function to give name to a list

I'm trying to create a keyword that will export a list as a suite variable, but I can't figure out how to pass a name to turn into a variable name.
*** Test Cases ***
Get Ref
${list} = Create List k l m n e
Rename List myName ${list}
log #{myName}
*** Keywords ***
Rename List
[Arguments] ${name} ${values}
log first: ${values[1]}
#{name}= Create List ${values[1]} ${values[3]}
set suite variable #{name}
The keyword takes a string and a list, creates a smaller list and exports it with the name string provided.
As a use-case, you want a generic function that can take values from a dropdown list on a webpage and return you just item 1, 3, and 5 as a new list with the name you provide. This way you could call it multiple times with different names, exporting different lists that you could use later.
Is there any way to make this work?
This seems to do what you want, if I understand the question correctly:
*** keywords ***
rename list
[Arguments] ${name} ${values}
${new}= create list #{values}
set suite variable ${${name}} ${new}

Resources