Is there a way in Robot framework to log if keywork only if there are True?

Viewed 777

I do know that there are no switch statements in RF. I do have 50 if-keywords (that I use because no switch exists). My log file is very long because literally every 50 if statements are logged (even those who are not true). I would like to know if there is a way to log only the statements that are true?

here is how my code is written (there are 50 keywords like these) :

# Access Apply ImportExportParams
\    Run Keyword If    '${Type}' == 'ImportExportParams' and '${DealId}' != 'None' and '${ScenarioId}' != 'None'    Call_API_ImportExportParams    ${DealId}    ${ScenarioId}    ${ProductId}
# Access bulk apply cheapest quote
\    Run Keyword If    '${Type}' == 'BulkApplyCheapest' and '${DealId}' != 'None' and '${ScenarioId}' != 'None'    Call_API_BulkApplyCheapest    ${DealId}    ${ScenarioId}    ${ProductId}
# SiteSelection
\    Run Keyword If    '${Type}' == 'SiteSelection' and '${DealId}' != 'None' and '${ScenarioId}' != 'None'     Call_API_SiteSelection    ${ProductId}    ${DealId}    ${ScenarioId}    ${Name}
# SiteSelectionFile
\    Run Keyword If    '${Type}' == 'SiteSelectionFile' and '${DealId}' != 'None' and '${ScenarioId}' != 'None'     Call_API_SiteSelectionFile    ${ProductId}    ${DealId}    ${ScenarioId}
\    Run Keyword If    '${Type}' == 'SiteSelectionFile2' and '${DealId}' != 'None' and '${ScenarioId}' != 'None'     Call_API_SiteSelectionFile2    ${ProductId}    ${DealId}    ${ScenarioId}
# SiteSelectionMultiple
\    Run Keyword If    '${Type}' == 'SiteSelectionMultiple' and '${DealId}' != 'None' and '${ScenarioId}' != 'None'     Call_API_SiteSelectionMultiple    ${ProductId}    ${DealId}    ${ScenarioId}

here is a screenshot of a part of my actual log

thanks for your help :)

2 Answers

I do not know about such feature in the framework, but I can think of an alternative. You could write a small library which is recommended by the user guide for such cases.

In general, it is not recommended to have conditional logic in test cases, or even in user keywords, because it can make them hard to understand and maintain. Instead, this kind of logic should be in test libraries, where it can be implemented using natural programming language constructs. However, some conditional logic can be useful at times, and even though Robot Framework does not have an actual if/else construct, there are several ways to get the same effect.

Here is an example to get you started.

Library code (lib.py):

from robot.libraries.BuiltIn import BuiltIn

def switch(type):
    type = int(type)
    if type == 1:
        BuiltIn().run_keyword('Log Many', 'arg1 - case1', 'arg2 - case1') # call any keyword like it was called from Robot Framework
    elif type == 11:
        BuiltIn().fail('Fail as it was 11') # or throw exception, that will fail the test execution as well
        
    if type == 2:
        BuiltIn().run_keyword('Log', 'arg1  - case2') # call any keyword like it was called from Robot Framework
    elif type == 22:
        BuiltIn().fail('Fail as it was 22') # or throw exception, that will fail the test execution as well

    if type == 3:
        BuiltIn().run_keyword('No operation') # call any keyword like it was called from Robot Framework
    elif type == 33:
        BuiltIn().fail('Fail as it was 33') # or throw exception, that will fail the test execution as well

    if type == 4:
        BuiltIn().run_keyword('Log To Console', 'arg1  - case4') # call any keyword like it was called from Robot Framework
    elif type == 44:
        BuiltIn().fail('Fail as it was 44') # or throw exception, that will fail the test execution as well     

Usage in test.robot (execute robot test.robot, Python and robot file should be in the same directory. enter image description here

Also the --pythonpath argument could be used or PYTHONPATH env variable could be updated with the path of the library if the library must be at another location.):

*** Settings ***
Library    ${CURDIR}/lib.py

*** Test Cases ***
A test 1
    switch    1
    
A test 2
    switch    2
    
A test 3
    switch    3
    
A test 4
    switch    4
    
A test 5
    switch    11
    
A test 6
    switch    22
    
A test 7
    switch    33
    
A test 8
    switch    44
    
A test 9
    switch    5

This is the output where you can see only the executed branch.

enter image description here

May be you are looking for --removekeywords and --flattenkeywords command line options For more details have a look at Removing and flattening keyword

your code suggests that all this conditions are looping under for loop from \ an older syntax of FOR loop. So by using

robot --removekeywords FOR testuitefilename.robot will produce output something like in the below screenshot. In most of the cases passed steps does not required and I think this suffice to serve your requirement.

FOR - This mode remove all passed iterations from for loops except the last one.

enter image description here

Other possibility is approaching to this problem in other way instead of looking for the option to not to log the false condition keywords in the logs. for example -

  • From the code I could see '${DealId}' != 'None' and '${ScenarioId}' != 'None' this condition is common. So instead of checking it all over check it once.
  • Instead of looping all over use the IN & List or Dictionary to check the ${type} variable value exist in the one of collection. And get that value using keywords.
  • Check this condition before hand '${DealId}' != 'None' and '${ScenarioId}' != 'None' and then use variable as part of keyword syntax to call the specific keyword

This I could reduce to -

*Test cases
Run Keyword If  '${DealId}' != 'None' and '${ScenarioId}' !='None'    Execute the type of Keyword   SiteSelection

*Keywords
Execute the type of Keyword      
    [Arguments]       ${type}

    ${Type_list}=   Create List     ImportExportParams  BulkApplyCheapest   SiteSelection   SiteSelectionFile   
        ...         SiteSelectionFile2  SiteSelectionMultiple

    ${Status}  ${index}  Run Keyword And Ignore Error      Get Index From List     ${Type_list}        ${type}
    ${Keyword_type}       Get From List       ${Type_list}    ${index}
    log     ${Status} ${index}
    Run Keyword     Call_API_${Keyword_type}

Output

enter image description here

Related