Forum Discussion

Wamboo's avatar
Wamboo
Community Hero
5 years ago
Solved

Get Unit name in script

Does anyone know the solution to this problem?

 

https://community.smartbear.com/t5/TestComplete-General-Discussions/How-to-find-the-script-unit-name/td-p/160177

 

How do I get the name of the file you were running the script from?
Having this name as the data to be used in the current script.

  • Wamboo's avatar
    Wamboo
    5 years ago

    Hi

    Thank you for your answers. 

     

    TanyaYatskovska  this code refers to the name of the running function and not to the name of the file in which the function is stored.

     

    I found a solution how to find the name of the file from which the function is run.

     

     

    function retrievedNameRelatedUnit() {
         let testItemNameFull = Project.TestItems.Current.ElementToBeRun.Caption;
         var formatTestItemNameFull = testItemNameFull.split(/\Name-Full.split(/\Name-- /));
         return formatTestItemNameFull[1];
    }

     

    there is an array with the file name in the variable.

     

    You can use the file name (Unitu) to dynamically run the file by e.g. aqObject Object

     

    TY for all your help!

     

3 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Actually, solution for JScript (and, hopefully, JavaScript) is provided in the thread that you referenced.

    For VBScript and DelphiScript this is not possible.

    Don't know for Python.

     

    • TanyaYatskovska's avatar
      TanyaYatskovska
      SmartBear Alumni (Retired)

      Hi Wamboo,

      For JavaScript you can use this code:

      function Afunction()
      {
        var theName = arguments.callee.toString().match(/function (\w*)/)[1];
      }

       

      • Wamboo's avatar
        Wamboo
        Community Hero

        Hi

        Thank you for your answers. 

         

        TanyaYatskovska  this code refers to the name of the running function and not to the name of the file in which the function is stored.

         

        I found a solution how to find the name of the file from which the function is run.

         

         

        function retrievedNameRelatedUnit() {
             let testItemNameFull = Project.TestItems.Current.ElementToBeRun.Caption;
             var formatTestItemNameFull = testItemNameFull.split(/\Name-Full.split(/\Name-- /));
             return formatTestItemNameFull[1];
        }

         

        there is an array with the file name in the variable.

         

        You can use the file name (Unitu) to dynamically run the file by e.g. aqObject Object

         

        TY for all your help!