cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the script return code

SOLVED
Occasional Contributor

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: How to get the script return code

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.

 

2019-03-20 08_23_44-Clipboard.png

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.

 

 2019-03-20 08_24_50-TestComplete - C__MySource_Sandbox_Sandbox.pjs.png

 

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.
2019-03-20 08_38_29-TestComplete - C__MySource_Sandbox_Sandbox.pjs.png

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?


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
6 REPLIES 6
Community Hero

Re: How to get the script return code

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";
    }
}

Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”


Extension Available

Occasional Contributor

Re: How to get the script return code

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.

 

 

 

Community Hero

Re: How to get the script return code

If you want OnStartTest to fire on each of your tests then you have configured a TestItem for each test.

 

from the Smartbear doc:

Description

The OnStartTest event occurs when TestComplete starts a test run, namely --

  • When you run any test in your project. For example, the event fires when you run a keyword test or script routine by selecting Run from TestComplete toolbars or menus, or by passing command-line parameters to TestComplete.
  • When a project test item starts running (regardless of whether you run individual test items or the entire project). If the tests are organized into batches, the event will be fired for each test item.

You can create an OnStartTest event handler to perform desired actions after the test starts, for example, to set initial conditions for the test.

 


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”


Extension Available

Occasional Contributor

Re: How to get the script return code

Good morning,

                          I appreciate your efforts to explain things to me but I've decided it's too hard, too obscure and difficult to understand.  Quoting text from the help isn't helpful 'cos I've spent hours reading it and trying to follow it and still can't find how to do what I need.  It's all very well to read "If the tests are organized into batches" but what does that mean?  How do you do that?  What is a batch and what goes into it?  

Help text states "To access handler parameters in script routines, simply call them by name" but if I have the event as a script, I'm duplicating the event code and effectively doing what the event should be doing.

 

Thank you for your time, I'm going to proceed without events and call my own routines.

 

Highlighted
Community Hero

Re: How to get the script return code

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.

 

2019-03-20 08_23_44-Clipboard.png

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.

 

 2019-03-20 08_24_50-TestComplete - C__MySource_Sandbox_Sandbox.pjs.png

 

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.
2019-03-20 08_38_29-TestComplete - C__MySource_Sandbox_Sandbox.pjs.png

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?


Robert Martin
[Community Expert Group]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Extensions available
Occasional Contributor

Re: How to get the script return code

Hello Robert,

                       Thank you for translating the help explanation.

 

I *thought* I had to create Keyword Items then convert then to Script items. I had put code in the Keyword event, converted it to a script then add code, as per this statement "To access handler parameters in script routines, simply call them by name:".  Seemed to me to be duplicating and doing what the event was supposed to do.

 

I've now created Script items and it is indeed doing what you demonstrated.

 

Thank you.

 

Steven.

New Here?
Join us and watch the welcome video:
SeptemberNews
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win