Modifying lists of lists in Robot Framework

老子叫甜甜 提交于 2019-12-04 07:44:13

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.

Skip Huffman

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

The normal method "Set List Value" in the Collections library does work on the embedded list - and its changed inplace, w/o recreating the objects; here's the POC:

${listy}=   Create List     a   b
${inner}=   Create List     1   2
Append To List      ${listy}       ${inner}
Log To Console      ${listy}      # prints "[u'a', u'b', [u'1', u'2']]", as expected

Set List Value      ${listy[2]}    0       4
# ^ changes the 1st element of the embedded list to "4" - both the listy's index (2), and the kw argument (0) can be variables

Log To Console      ${listy}      # prints "[u'a', u'b', [u'4', u'2']]" - i.e. updated
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!