Forum Discussion

asdfasdfasdfasd's avatar
asdfasdfasdfasd
Contributor
15 years ago

Is there a way to get the current script name?

I'm looking for a way to get the current script name that is being executed.  I can't seem to figure this out in the help file nor in the forum search.  Any guidance to this problem is appreciated.





Steve

7 Replies

  • Hello Steve,



    As far as I understand, you want to get the name of the routine that is currently running. There's no built-in functionality to do this. However, you can do this by using native JScript mechanisms. Here's the getStack function that returns the routine name by using the current call stack:

    function test1()


    {

      Log.Error("Error X in " + getStack()); 

    }



    function getStack()


    {

      var cur = getStack.caller;

      var callers = new Array();



      while(cur)

      {

        callers.push(cur.toString().match(/function (\w*)/)[1]);

        cur = cur.caller;

      }



      return callers.join("\r\n"); 

    }


    BTW, when using the Log.Error method, TestComplete posts call stack information on the Call Stack page of the test log. You can find more information in the Log.Error help topic. 
  • Thanks for the reply David.  I'm actually looking to find the current script name (e.g., test_columns.sj).  Is that possible?
  • Hello Steve,



    There's no way to do this. I suggest that you add a global variable to a script unit and assign the file name of the current unit to the variable. After that, you can use this variable in the unit as you want. Here's an example:

    var ScriptName = "test_columns.sj";



    function Main()


    {


      Log.Message("The name of the currently running script file is '" + ScriptName + "'");


      ...


    }
  • Sadly, I tried getting the name from the RootLogData.dat, but I found out later, it does not appear in the Log Folder until you either pause, abort, or finish the script execution.  It seems to hold onto the log in memory and dump them to files.
  • Critter's avatar
    Critter
    New Contributor
    I know this is an older thread, but I am using this to grab the script name and append it to my log files...



    var strFullPathToScript = Project["FileName"];

     var strScriptName = Utilities.StringReplace(Utilities.ExtractFileName(strFullPathToScript),".mds","",1);
    • meinTest's avatar
      meinTest
      Occasional Contributor

      For JavaScript, I found the following workaround. Maybe it will help others.

      I create a try-catch block. Within the try I provoke an exception, this one I catch-up with the catch and read-out the function name in the stack.

       

      Note: this also works across several units -> USEUNIT

      function getCaller(){
        let arrStackLines = [];
      
        try{
          throw new Error("throw a error to get the stack");
        }catch(objE){
          arrStackLines = objE.stack.split("\n");
        }
        let strLine = arrStackLines[arrStackLines.length -1];
        let intStart = strLine.indexOf("at ") + 3;
        let intEnd = strLine.indexOf("(<");
        Log.Message(strLine.substring(intStart, intEnd));
      }

       

      Best regards 

       

      sign.png

        meinTest GmbH

        www.meinTest.software

       

       SmartBear preferred value-added Service

       Provider in the DACH region