我在Robot Framework中使用了一个嵌套列表。我想在Robot Framework级别的子列表中更改一项。
我的清单如下所示:
[鲍勃,玛丽,[六月,七月,八月]]
我想将“七月”更改为其他内容,例如“九月”
Robot Framework将让我更改“ bob”或“ mary”,但是如果我尝试插入列表,它将转换为字符串。
(哦,我尝试使用“插入列表关键字”来插入一个新的子列表,以及其他列表关键字,但运气不好。)
小智 5
我能够使用这样的Collections库关键字实现修改
*** 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}
Run Code Online (Sandbox Code Playgroud)
我无法找到直接更改子列表的方法,必须先将其提取,然后进行修改,最后将其放回原处。
由于缺乏回应,我猜测对此没有一个简洁干净的解决方案。这是我所做的:
我这样创建了一个实用程序:
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
Run Code Online (Sandbox Code Playgroud)
然后我将此条目放入我的机器人框架测试套件文件中:
| | ${ListWithSublist} = | sublistReplace | ${ListWithSublist]} | NewItem | 1 | 1 |
Run Code Online (Sandbox Code Playgroud)
(当然,导入我的实用程序库)
运行后,列表索引 1 处子列表中的第二项(索引 1)将为“NewItem”
也许不是最优雅或最灵活的,但它现在可以完成工作