如何将含斜杠的变量存入列表并调用?Robot Framework报错解决
Robot Framework含特殊字符列表变量调用报错解决方案
问题场景
你当前的代码实现如下:
列表定义
@{referentiel} N1(exemple)/N2(exemple) N2(exemple)/exemple
关键字实现
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}]')]
测试用例
we are the best Visca barca 1
报错信息
Value of variable '@{referentiel}[${index}]' is not list or list-like.
报错原因
Robot Framework中,@{referentiel}的作用是展开列表所有元素(用空格分隔),在字符串中直接写@{referentiel}[${index}]会被解析成“所有列表元素 + [索引]”的拼接字符串,而非从列表中取出指定索引的元素,因此触发“不是列表或类列表对象”的错误。
解决方法
核心是正确区分列表变量的两种引用方式,以下两种方案均可解决问题:
方法1:先通过关键字取出列表元素
使用Get From List关键字先取出指定索引的元素,存入临时变量后再在XPath中使用:
Visca barca [Arguments] ${index} # ${referentiel}代表整个列表对象,取出指定索引的元素 ${target_item}= Get From List ${referentiel} ${index} Click Element Using Javascript //div[@id='mat-select-value-5'] Click element of long page //span[@class='mat-option-text'][contains(.,'${target_item}')]
方法2:直接在字符串中正确引用列表元素
用${referentiel[${index}]}直接引用列表的指定元素(${referentiel}代表整个列表对象,而非展开元素):
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}]}')]
注意事项
Robot Framework的列表索引从0开始计数,你的测试用例传参1会取出列表的第二个元素N2(exemple)/exemple,若要取第一个元素需传0。
内容的提问来源于stack exchange,提问作者Mohamed Kaabi
相关产品推荐
相关产品推荐

