我实际上在机器人框架中使用了很多if语句,这些语句很容易成为switch语句。我在Robot Framework中找不到switch语句的任何示例(它甚至存在吗?)。这是我的代码的一部分:(我的代码中有50种以上的if语句,它非常大,日志太大,无法找到信息(所有if都写在日志文件中,即使那些假)。感谢您的帮助
# 1st posibility
\ ${varA} Run Keyword And Continue On Failure Run Keyword If '${Type}' == 'Deal' keyword1 ${Name}
# 2d posibility
\ ${varB} Run Keyword And Continue On Failure Run Keyword If '${Type}' == 'Scenario' and '${varA}' != 'None' keyword2 ${Name}
# 3rd posibility
\ ${varC} Run Keyword And Continue On Failure Run Keyword If '${Type}' == 'Site' and '${varA}' != 'None' keyword3 ${Name}
# 4th posibility
\ Run Keyword And Continue On Failure Run Keyword If '${Type}' == 'SiteFile' and '${varA}' != 'None' keyword4 ${varA}
[如果在所有情况下您都基于单个值(即${Type}
)决定要执行哪个关键字,则可以尝试使用type=keyword
这样的映射创建字典并动态找出要执行的关键字。下面是简单的示例:
*** Settings ***
Library Collections
*** Variables ***
&{TYPE_MAPPING} deal=keyword1 scenario=keyword2 site=keyword3 sitefile=keyword4
*** Test Cases ***
Dynamic-Keyword-Name
Take Action Based On Type deal
Take Action Based On Type scenario
Take Action Based On Type site
Take Action Based On Type sitefile
Take Action Based On Type xyz
*** Keywords ***
Take Action Based On Type
[Arguments] ${type}
${kw_name} Map Type To Keyword Name ${type}
Run Keyword And Continue On Failure ${kw_name}
Map Type To Keyword Name
[Arguments] ${type}
${result} ${value} Run Keyword And Ignore Error Get From Dictionary ${TYPE_MAPPING} ${type.lower()}
Run Keyword If '${result}' == 'FAIL' Fail msg=Was not able to map type "${type}" to keyword name
Return From Keyword ${value}
keyword1
Log Deal
keyword2
Log Scenario
keyword3
Log Site
keyword4
Log SiteFile