【问题标题】:Switch statement in robot Framework机器人框架中的switch语句
【发布时间】:2020-09-04 10:08:27
【问题描述】:

我实际上在机器人框架中使用了很多 if 语句,这些语句很容易成为 switch 语句。 我在机器人框架中找不到任何 switch 语句的例子(它甚至存在吗?)。 这是我的代码的一部分:(在我的代码中,像这样的 if 语句有 50 多种可能性,它非常庞大,日志太大而无法找到信息片段(所有 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}

【问题讨论】:

  • 机器人框架中没有switch语句。

标签: python if-statement switch-statement robotframework


【解决方案1】:

这是我所做的。

我的日志是易读的

test.robot
*** Settings ***
Library    myLib.py

*** Test Case ***
TC_run
     Switch Keyword    ${Type}



myLib.py
def    Calculate_funct(var1):
                 BuiltIn().run_keyword('CalculateKeyword', '${el1}', '${el2}','${el3}')
                                                                

def ValidateSite_funct(var1):
       if Id != "" :
             BuiltIn().run_keyword('ValidateKeyword', '${el1}', '${el2}')
             else : 
                    return "error"


def switch_keyword(keyword, **kwargs):
       """select the correct function to apply in function of the given keyword.\n
       *Args:*\n
             keyword: keyword in the first column of the file.\n
             arguments: columns content.\n
             "Calculate"\n
             "Validate"\n
       *Returns:*\n
             does the call to the correct api.\n
       *Example:*\n
       | Switch Keyword | ${Type} |
       """
       switcher = {
             "Calculate": Calculate_funct,
             "Validate" : Validate_funct,   
       }
       func = switcher.get(keyword, lambda: "invalid keywork")
       returned_thing = func(**kwargs)
       return returned_thing
       

                        

【讨论】:

    【解决方案2】:

    如果在所有情况下您都根据单个值(即${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
    

    和日志:

    【讨论】:

      猜你喜欢
      • 2016-04-07
      • 2015-09-10
      • 2014-10-06
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 2018-04-25
      相关资源
      最近更新 更多