Improving Traceability in TestComplete Logs

Aaron Frost’s ideas about improving log output go beyond just making logs more readable. They also provide improved traceability (What did my script do, and more importantly, can I prove it?) and aid in debugging (Where did my script go wrong?).

One of our coding standards requires that all methods produce some output. This ensures that there is a record of the method actually running. A good start to this is to create some sort of “method enter” log entry to show that the method was called and that it received the correct parameters. Even if the method generates an exception and exits unexpectedly, using this message, you at least know where the failure occurred and where to start debugging.

function abc (xyz)
{
Log.Message ('Starting abc. xyz=' + xyz)
// rest of the method goes here.
}

For methods that have multiple parameters, this gets to be rather tedious and repetitive and if there is one thing that programmers can’t stand, it is anything
that is tedious and repetitive. As a result, I created the following bit of code that outputs all of the parameter names, types, and values.

var functionName = eval ('arguments.callee.toString()');
var parameters = eval ('arguments.callee.toString()');
var message = '';

functionName = functionName.substr('function '.length);
functionName = functionName.substr(0, functionName.indexOf('('));
parameters = parameters.substr (parameters.indexOf ('(') + 1);
parameters = parameters.substr (0, parameters.indexOf (')'));
parameters = parameters.replace (/

|
/gm, ' ');
parameters = parameters.split(/,s?/);

for (var i = 0; i < parameters.length; i++)
{
message += 'Parameter [' + parameters[i] + '] : ';
message += typeof arguments[i] + '
';
if (arguments[i] === undefined)
{
message += '
';
}
else if (typeof arguments[i] === "string")
{
message += 't"' + arguments[i] + '"

';
}
else
{
message += 't' + arguments[i] + '

';
}
}
Log.Message ('Entering ' + functionName, message);

JScript doesn’t have some sort of macro expand or #include feature to aid in calling this code, but we can use a feature that TestComplete does have—project variables. To get the above code to run, simply create a project variable and set the Default and Local values to the above text. Then, use it in your scripts as follows:

function xyz ()
{
eval (Project.Variables.functionEnter);
abc ('123', 123, true, Sys.Desktop.Picture());
}

function abc (one, two, three, four)
{
eval (Project.Variables.functionEnter);
}

When you run function xyz (), function abc () will log two messages. One message
simply says “Entering xyz.” The second message is ‘Entering abc’ with the following text as additional information:

Parameter [one] : string
"123"

Parameter [two] : number
123

Parameter [three] : boolean
true

Parameter [four] : object

The practical value of this technique is that one line of code in each method generates enough output to ensure that our logs provide traceability. At a minimum, I can verify that the method received the correct values for parameters. We’ve even included it as part of the function code template, so we don’t even have to type it anymore.

For those of you looking to extend this technique, adding support for Log.AppendFolder would be
pretty easy. One thing I would like to do is provide better handling of objects. This is because
toString() is rather useless for most objects. Moreover, objects can have any number of properties
with any type of value—being practical about what to output can be a tricky problem.

Happy Testing,

Glen Accardo

Test Automation Lead

Schlumberger

See also:

 


Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox