Forum Discussion

psaxton's avatar
psaxton
Occasional Contributor
14 years ago

How do I catch if an unexpected error has occurred?

Hello



Sorry if this if this is a really stupid question, or has already been asked somewhere else



If I have application where an error message may be displayed before a screen is loaded how do I do a test to cover this and ensure that the test fails?



For example I have an order screen with a button that says view items.



I click the button and the order items screen comes up perfectly.



Then if a change is made to the code which results in an error, thats what I want to catch



So now when I click on the view order items button I will get an error "Error Occurred".  I need that to cause a test to fail



I am using TestComplete 7



Paul

14 Replies

  • psaxton's avatar
    psaxton
    Occasional Contributor
    Hello



    This still does not work



    Can you please show me an example that works?



    I am doing something that should be very simple




    function TestException()


    TestException()

    {


    //Runs the "test" tested application.


    TestedApps.test.Run();


    //Clicks at point (181, 15) of the 'Form1' object.


    Aliases.test.Form1.Click(181, 15);



    try



    {


     


    //Clicks the 'button1' button.


    Aliases.test.Form1.button1.PerformClick();


    }


    catch (e)


    {


    Log["Error"](e["description"]) ************Does not get here


    }


    //Clicks the 'button1' button.


    Aliases.test.Form1.button1.ClickButton();


    //Runs the "test" tested application.


    TestedApps.test.Run();


    //Checks whether the 'Text' property of the Aliases.test.Form1.button1 object equals 'Test'.


    aqObject.CheckProperty(Aliases.test.Form1.button1, "Text", 0, "Test", false);


    //Runs the "test" tested application.


    TestedApps.test.Run();


    //Closes the 'Form1' window.


    Aliases.test.Form1.Close();


    }



    Paul

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Paul,



    The try/except block handles only script code exceptions. I.e. v=1/0; exception will be caught, but window.clickButton(...) will succeed as there is no script runtime exception. The fact that some exception occurred in the tested application is the whole problem of this application. :)

    Usually, if such an exception occurs, the tested application (or the OS itself) shows some error window. In almost all cases this window is the modal one, i.e. it blocks any further UI interactions with tested application (button clicks, window activation, etc.) until closed. In this case you should go through the 'Handling unexpected windows' and 'OnUnexpectedWindow event handler' help topics and create the handler for such unexpected window. Then, in the handler, you can make some analysis and decide whether to fail the test, or recover it and proceed further.



    Hope this will help.
  • psaxton's avatar
    psaxton
    Occasional Contributor
    ok thanks that makes a bit more sense



    I want my test to fail, but at least to have a screenshot of the error and for my other tests to carry on

  • Hi Paul,





    You can use the Runner.Stop method to stop the current test only (see the "Runner.Stop" help topic (http://www.automatedqa.com/support/viewarticle/12335/) for more information). This can be done in your OnUnexpectedWindow event handler. To learn how to post an image to the test log, see the "Posting Images to the Log" (http://www.automatedqa.com/support/viewarticle/12254/) help topic. To learn how to create an event handler for the event see the "Creating an Event Handler for the OnUnexpectedWindow Event" (http://www.automatedqa.com/support/viewarticle/11455/) help topic.