Ask a Question

Is there a way to return the name of the current sub routine or function?

adrian_g
Occasional Contributor

Is there a way to return the name of the current sub routine or function?

Hi All,



Is there a way to return the name of the current sub routine or function that is being run. It would be nice to enter into the log.



I am currently coding in VB Script.



I assume there is no way to do it using VB script but is there a TestComplete inbuilt function?



Thanks  heaps,

Adrian
29 REPLIES 29
YMinaev
Staff

Hi,



There is no built-in way to do this. However, we have an appropriate suggestion to implement such an ability, and your post has increased its rating.
------
Yuri
TestComplete Customer Care Engineer

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
kdu
Not applicable

I add my voice for this feature.
We make the world a better place!
YMinaev
Staff

Hi,



I've increased the suggestion's rating.
------
Yuri
TestComplete Customer Care Engineer

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
ory_z
Contributor

Can I add another vote?



Thanks.
YMinaev
Staff

Hi,



Your vote is added.
------
Yuri
TestComplete Customer Care Engineer

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
gangashe
Not applicable

my vote too 😞
tristaanogre
Esteemed Contributor

ACTUALLY...  if you're using JScript, you can use the technique discussed in the following article:



http://blog.smartbear.com/post/11-02-16/improving-traceability-in-testcomplete-logs/



Every function you create, then, add in the code as the first line of the function



eval(Project.Variables.functionEnter)




You then make sure you have a project variable set as a string type with the default value set to



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 (/\r\n|\n/gm, ' ');
parameters = parameters.split(/,\s?/);

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


This works like a charm and I've started using this extensively in my own code work.





Robert Martin
[Hall of Fame]
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
Vegas Thrill Rider
Extensions available

Hi Regalo,



Done. The suggestion's rating is increased.

Best regards,
Alexey

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
alevans4
Contributor

ACTUALLY...  if you're using JScript, you can use the technique discussed in the following article:



http://blog.smartbear.com/post/11-02-16/improving-traceability-in-testcomplete-logs/




I'm a little confused with that blog post. Specifically where he says, "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."  While true, Jscript doesn't have includes, TestComplete definitely does (USEUNIT), unless you took it away in v8 since we're still on v7.  And if you're able to use project variables, you're definitely also able to use includes since you're running in TestComplete.  So, just have standard include script, or multiple depending on your needs.



Add this somewhere in your script (or like I do in a standard include):



Function.prototype.name = function() {

  var m = this.toString().match(/^\s*function\s+([^\s\(]+)/);

  return m ? m[1] : "";

}




then in whatever test you run:



function test()

{

  Log.Message(argument.callee.name());

}




If you're squirrelly about modifying the Function prototype, could just drop a generic function:



function getFunctionName(fn)

{

  var m = fn.toString().match(/^\s*function\s+([^\s\(]+)/);

  return m ? m[1] : "";

}




and then



function test()

{

  Log.Message(getFunctionName(argument.callee));

}




Adding in the extra parameter info would be fairly trivial if you wanted to drop in something like a Function.prototype.logFunctionCall or something.
cancel
Showing results for 
Search instead for 
Did you mean: