Lou Gyde
Lou Gyde

Reputation: 115

Switch statement in robot Framework

I'm actually using a lot of if statements in robot framework that could easily be a switch statement. I can't find any example of a switch statement in robot Framework (does it even exist ?). here is a part of my code : (i have more than 50 possibilities in if statements like this in my code, it's very huge and logs are too big to find pieces of informations (all the ifs are written in the log file, even those false). thanks for your help

# 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}

Upvotes: 1

Views: 10925

Answers (2)

Lou Gyde
Lou Gyde

Reputation: 115

Here is what I've done.

My log are lisible

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
       

                        

Upvotes: 1

JaPyR
JaPyR

Reputation: 1569

If in all cases you decide what keyword to execute based on single value i.e ${Type} than you could try to create dictionary with mapping like type=keyword and dynamically find out which keyword to execute. Below is simple example:

*** 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

and logs: screenshot of logs

Upvotes: 3

Related Questions