我在下面有这个代码,我通过${txnExcel}
来循环Excel文件中的值来搜索并比较该值。但有时系统中不存在${txnExcel}
,所以在这种情况下我想忽略失败并且不需要比较值并通过跳过erorr继续到下一个${txnExcel}
。
open excel ${PATH_EXCEL}
${exp_row_count} get row count Sheet1
:For ${i} in range 1 ${exp_row_count}
\ ${excel_index} evaluate ${i}+1
\ ${txnExcel} Read Cell Data By Name Sheet1 B${excel_index}
\ ${ServiceTypeExcel} Read Cell Data By Name Sheet1 C${excel_index}
\ ${TransferAmountExcelOrigin} Read Cell Data By Name Sheet1 D${excel_index}
\ ${CurrencyExcel} Read Cell Data By Name Sheet1 E${excel_index}
\ ${TransferAmountExcel} set variable ${TransferAmountExcelOrigin} ${CurrencyExcel}
#=======================================================================================
\ input text name=id_or_tsn ${txnExcel}
\ click button name=Submit
\ wait until element is visible xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2] 5s
#===========================Compare Transaction Element================================================
\ ${txnCompare} get text xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2]
\ ${ServiceTypeCompare} get text xpath=/html/body/div[2]/div[2]/div/div/div/div[3]/div[2]/span
\ ${TransferAmountCompare} get text xpath=/html/body/div[2]/div[2]/div/div/div/div[5]/div[2]/span
\ ${CurrencyCompare} get text xpath=/html/body/div[2]/div[2]/div/div/div/div[4]/div[2]
\ should be equal as strings ${txnExcel} ${txnCompare} invalid TXN comparison[${i}]
\ should be equal as strings ${ServiceTypeExcel} ${ServiceTypeCompare} invalid ServiceType comparison[${i}]
\ should be equal as strings ${TransferAmountExcel} ${TransferAmountCompare} invalid Transfer Amount comparison[${i}]
\ should be equal as strings ${CurrencyExcel} ${CurrencyCompare} invalid Currency comparison[${i}]
#============================End Compare Transaction Element====================================================
在此先感谢您的回答,如果您不清楚我的问题,请告诉我。
答案 0 :(得分:2)
我理解你的问题的方法是循环浏览从Excel获取的项目。有时,Excel中的项目不存在于应用程序中。如果是这种情况,则跳过该项目的检查并从excel中获取下一个项目。
正如@pankaj mishra正确引用的那样,应该使用关键字Run Keyword and Ignore Error
。此关键字输出2个值(这就是为什么在它之前看到2个变量)。第一个包含状态,第二个包含实际值。
然后关键字Continue For Loop
使用它,当关键字返回错误(FAIL)时会关闭此循环。然后跳过所有后续检查(由记录步骤表示)。
*** Test Cases ***
TC
@{list} Create List ${3} ${6} ${15} ${21}
Log To Console \n
:FOR ${id} IN @{list}
\ ${status} ${result}
\ ... Run Keyword And Ignore Error Mock Element Exists ${id}
\
\ Continue For Loop If '${status}' == 'FAIL'
\ Log To Console No Failure for ${id}
*** Keywords ***
Mock Element Exists
[Arguments] ${id}
&{dic} Create Dictionary 3=1 6=2 18=6 21=7
[Return] ${dic['${id}']}
答案 1 :(得分:0)
在处理$ {txnExcel}时,您可以在代码上添加if条件。
只需按照您想要的语言进行检查。下面的代码只是一个逻辑。用你想要的语言写下来。
if(${txnExcel} != NULL){
// put your compare code inside this if statement.
}
答案 2 :(得分:0)
您可以使用Run关键字并忽略错误或运行关键字并继续失败。 有关这些关键字的详细信息,请访问
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html
此处有关于这些关键字的好例子
How to ignore Get Table Text from Cell, if xpath of cell not match
在你的代码中,你可以使用下面的行
\ ${status} ${result}
\ ... Run Keyword And Ignore Error input text name=id_or_tsn ${txnExcel}
\ Continue For Loop If '${status}' == 'FAIL'
还有一次在$ {txnexcel}
中跳过错误Run Keyword And Continue On Failure Should Be Equal As Strings
${txnExcel} ${txnCompare} invalid TXN comparison[${i}]
即使没有提供值或变量$ {txnExcel},它也会继续下一个KW。
答案 3 :(得分:0)
这是现在适合我的解决方案。
open excel ${PATH_EXCEL}
${exp_row_count} get row count Sheet1
:For ${i} in range 1 ${exp_row_count}
\ ${excel_index} evaluate ${i}+1
\ ${txnExcel} Read Cell Data By Name Sheet1 B${excel_index}
\ ${ServiceTypeExcel} Read Cell Data By Name Sheet1 C${excel_index}
\ ${TransferAmountExcelOrigin} Read Cell Data By Name Sheet1 D${excel_index}
\ ${CurrencyExcel} Read Cell Data By Name Sheet1 E${excel_index}
\ ${TransferAmountExcel} set variable ${TransferAmountExcelOrigin} ${CurrencyExcel}
#=======================================================================================
\ input text name=id_or_tsn ${txnExcel}
\ click button name=Submit
\ ${found_txn} run keyword and return status wait until element is visible xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2] 5s
\ ${error_message} run keyword unless ${found_txn} get text xpath=/html/body/div[2]/div[2]
\ run keyword and continue on failure element should be visible xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2]
\ run keyword unless ${found_txn} continue for loop
#===========================Compare Transaction Element================================================
\ ${txnCompare} get text xpath=/html/body/div[2]/div[2]/div/div/div/div[1]/div[2]
\ ${ServiceTypeCompare} get text xpath=/html/body/div[2]/div[2]/div/div/div/div[3]/div[2]/span
\ ${TransferAmountCompare} get text xpath=/html/body/div[2]/div[2]/div/div/div/div[5]/div[2]/span
\ ${CurrencyCompare} get text xpath=/html/body/div[2]/div[2]/div/div/div/div[4]/div[2]
\ should be equal as strings ${txnExcel} ${txnCompare} invalid TXN comparison[${i}]
\ should be equal as strings ${ServiceTypeExcel} ${ServiceTypeCompare} invalid ServiceType comparison[${i}]
\ should be equal as strings ${TransferAmountExcel} ${TransferAmountCompare} invalid Transfer Amount comparison[${i}]
\ should be equal as strings ${CurrencyExcel} ${CurrencyCompare} invalid Currency comparison[${i}]