Forum Discussion

StevenJ61's avatar
StevenJ61
Occasional Contributor
6 years ago
Solved

How to get the script return code

I am executing a sequence of (JavaScript) test scripts and need to trap the return code of each script as it finishes.

Is there a project or global variable that contains the current test status/return code?

For example, if a Log.Warning or Log.Error statement is executed, the script finishes with a warning/error return code. 

Is it possible to obtain this return code value and if so, how do you do it?

  • OK, I set up a quick scenario to demonstrate the OnStartTest and OnStopTest event handlers.  

     

    First, I set up 5 tests.  All these do is just write a quick message to the log.

     

    Next, I set up the Events item in my project.  If it's not already there, you can right click on your project node and select "Add Item -> New Item" to add the Events item.

     

     

     

    Double click on the OnStartTest event to add a handler.  You can create a new unit or use an existing unit.  When you have the unit selected, it will link the event to the handler script.  You can see in the picture that, when this is done, the event has an electric bolt icon next to it.

     

    When you add a handler, it creates a function in your chosen script language that is empty.  I created two handlers, one for OnStartTest and one for OnStopTest.  My handlers don't do much of anything except log that I'm starting a test and stopping a test.  Here's the code.

     

    function GeneralEvents_OnStartTest(Sender){
        Log.Message('Test started');
    }
    
    function GeneralEvents_OnStopTest(Sender){
        Log.Message('Test Stopped');
    }

    So... I have the script tests set up to run as test items, I have my events added to my project, I have my event handlers created.   Now, when I run my project, this is what I get in my log.


    Each test item has it's own node in the log and each test item reflects the OnStartTest and OnStopTest handlers with the test execution running in the middle.

     

    Does this help you understand better what we're talking about?

6 Replies

  • shankar_r's avatar
    shankar_r
    Community Hero

    Create OnStopTest event handler and use Log.ErrCount, Log.WrnCount to get the count.

     

    Sample would be

    function GeneralEvents_OnStopTest(Sender) {
        if(Log.ErrCount > 0){
            Project.Variables.curTestCaseStatus = "Error";
        }else if(Log.WrnCount > 0){
            Project.Variables.curTestCaseStatus = "Warning";
        }else if(Log.CheckpointCount > 0){
            Project.Variables.curTestCaseStatus = "Pass";
        }else{
            Project.Variables.curTestCaseStatus = "No checkpoints created";
        }
    }
    • StevenJ61's avatar
      StevenJ61
      Occasional Contributor

      Hello, thanks for the response but I am not sure it completely answers my query.

       

      I have ony just started with TestComplete so please excuse my lack of knowledge.

       

      I set up a sequence several scripts and added OnStartTest() and OnStoptTest() methods to each, added the scripts to the Organize Scripts list and ran the project.

       

      Results show the OnStartTest() method was executed once when the first script ran and the OnStopTest() was executed after the last script BUT I wanted these methods to fire for each script.

       

      Is this possible or  am I not understanding how to execute a sequence of tests?

       

      I am eventually going to run this sequence via the command line and need to extract each script's result.

       

       

       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        OK, I set up a quick scenario to demonstrate the OnStartTest and OnStopTest event handlers.  

         

        First, I set up 5 tests.  All these do is just write a quick message to the log.

         

        Next, I set up the Events item in my project.  If it's not already there, you can right click on your project node and select "Add Item -> New Item" to add the Events item.

         

         

         

        Double click on the OnStartTest event to add a handler.  You can create a new unit or use an existing unit.  When you have the unit selected, it will link the event to the handler script.  You can see in the picture that, when this is done, the event has an electric bolt icon next to it.

         

        When you add a handler, it creates a function in your chosen script language that is empty.  I created two handlers, one for OnStartTest and one for OnStopTest.  My handlers don't do much of anything except log that I'm starting a test and stopping a test.  Here's the code.

         

        function GeneralEvents_OnStartTest(Sender){
            Log.Message('Test started');
        }
        
        function GeneralEvents_OnStopTest(Sender){
            Log.Message('Test Stopped');
        }

        So... I have the script tests set up to run as test items, I have my events added to my project, I have my event handlers created.   Now, when I run my project, this is what I get in my log.


        Each test item has it's own node in the log and each test item reflects the OnStartTest and OnStopTest handlers with the test execution running in the middle.

         

        Does this help you understand better what we're talking about?