Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying lists of lists in Robot Framework

I have a nested list that I am using in Robot Framework. I would like to change one item in a sublist at the Robot Framework level.

My list looks like this:

[ bob, mary, [june, july, august]]

I want to change "july" to something else, say "september"

Robot Framework will let me change 'bob' or 'mary', but if I try to insert a list, it is converted into strings.

(Oh, I have tried to use the "Insert Into List keyword to insert a new sub list, and other List keywords, no luck with any.)

like image 224
Skip Huffman Avatar asked Jun 02 '11 18:06

Skip Huffman


People also ask

How do you append to list in Robot Framework?

Use Append To List to add items to the end of the list. If the absolute value of the index is greater than the length of the list, the value is added at the end (positive index) or the beginning (negative index). An index can be given either as an integer or a string that can be converted to an integer.

How do you access list variables in Robot Framework?

Clicking on New Scalar will open the following screen to create the variable and the value we need to replace with when the variable in used inside test cases. We get ${} for the Name field. The name of the variable is ${url}. The value is − http://localhost/robotframework/login.html.


2 Answers

I was able to achieve the modification with Collections library keywords like this

*** settings ***                                                                       
Library   Collections                                                                

*** test cases ***                                                                     
test    ${l1}=  Create List  1  2  3                                        
        ${l2}=  Create List  foo  bar  ${l1}                                              
        ${sub}=  Get From List  ${l2}  2 
        Set List Value   ${sub}   2   400 
        Set List Value   ${l2}  2  ${sub}  
        Log  ${l2} 

I was not able to find a way to directly alter the sublist, it has to be first extracted, then modified and finally put back in place.

like image 166
janne Avatar answered Oct 03 '22 01:10

janne


I am guessing from the lack of response that there isn't a neat clean solution to this. Here is what I have done:

I created a utility thusly:

class Pybot_Utilities:
    def sublistReplace(self, processList, item, SublistIndex, ItemIndex):
        '''
        Replaces an item in a sublist
        Takes a list, an object, an index to the sublist, and an index to a location in the sublist inserts the object into a sublist of the list at the location specified. 
        So if the list STUFF is (X, Y, (A,B,C)) and you want to change B to FOO give these parameters: [STUFF, FOO, 2, 1]
        '''

        SublistIndex=int(SublistIndex)
        ItemIndex=int(ItemIndex)
        processList[SublistIndex][ItemIndex] = str(item)
        return processList

I then put this entry in my robot framework test suite file:

|    | ${ListWithSublist} = | sublistReplace    | ${ListWithSublist]}  | NewItem | 1 | 1 |

(Importing my utility library, of course)

After this runs, the second item (index 1) in the sublist at index 1 of the list will be "NewItem"

Perhaps not the most elegant or flexible, but it will do the job for now

like image 29
Skip Huffman Avatar answered Oct 03 '22 01:10

Skip Huffman