Search code examples
user-interfaceautomated-testsrobotframeworkui-automationsikuli

How to handle try catch exception in ride robot framework?


I'm using RIDE robotframework, i want to handle an exception when the application is crashed i close it then open a new instance of it.

*** Settings ***
Library           SikuliLibrary

*** Variables ***
${openProject}    sikuli_captured\\Emna\\openProject.png
${DataBaseSTProject}    sikuli_captured\\Emna\\DataBaseSTProject.png
${testSession}    sikuli_captured\\Emna\\testSession.png
${menu}           sikuli_captured\\menu.png
${fileName}       sikuli_captured\\Emna\\fileName.png
${save}           sikuli_captured\\Emna\\save.png

*** Test Cases ***
createNewProject
    Click    ${menu}
    Click    ${testSession}
    Input Text    ${fileName}    FirstProjecT3
    Click    ${save}

openTestProject
    Click    ${openProject}
    Double Click    ${DataBaseSTProject}

Any suggestion would be appreciated.

Thanks for your help :)

enter image description here


Solution

  • In Robot Framework the concept of Try/Catch/Finally is not present. In essence your Test Case body is the Try part of this trifecta and the other two are combined into the [Teardown] keywords of the respective Test Suite, Test Case or Keyword sections.

    Within this Teardown keyword it is possible to recognize if a Test Case has Passed or Failed through the automatic variables of Robot Framework itself or the Run Keyword If ... family of keywords. This would allow you to create a separate section for the Catch, and finally. In the below section of code an example is given of a pass and fail test case, each using the same Teardown.

    This construct should allow you to check if a step in a test case failed, verify if the application has crashed (through the Sikuli image test of the popup) and then close and restart the application.

    *** Test Cases ***
    
    Open Application and fail
        Log to Console    About to Fail
        Fail
        Log to Console    Will never trigger.
        [Teardown]    Generic Test Case Teardown
    
    Open Application and Pass
        Log to Console    About to Pass
        No Operation
        Log to Console    Will trigger.
        [Teardown]    Generic Test Case Teardown
    
    *** Keywords ***
    Generic Test Case Teardown
        # Catch of Try Catch Finally
        Run Keyword If Test Failed    Test Case Catch
    
        # Finally of Try Catch Finally
        #  RKITS is only executed when test passed.
        Run Keyword If Test Passed    Test Case Finally
        #  Always executed regardless of test execution status.
        Log To Console     I am always executed.
    
    Test Case Catch
        Log To Console    Test Case Catch
    
    Test Case Finally
        Log To Console    Test Case Finally