Forum Discussion

ali_jafer's avatar
ali_jafer
Occasional Contributor
11 years ago

Checking for existence of a Script Unit and functions/subs defined in it

Suppose I am getteing name of Script Unit and any function defined in it as input from an external data source, if I want  to determine, whether this particular Script Unit and function is part of current TestComplete project or not.... is it possible via any scripting techinque (any TestComplet feature or language feature)

I want to acheive something like below

Example:

---

strScriptUnit= get name of function from excel test data sheet

strFunction= get name of function from excel test data sheet

 IF strScriptUnit belongs to current Project and strFunction bleongs to strScriptUnit THEN

    Runner.CallMethod(strScriptUnit &"."&strFunction,[parmlist...])

 ELSE

    Log.Message "Non existent Script Unit or Function"

 END IF

----

Can we achieve Bold check applied in Pseude Code?





thanks






  • Hi Ali,


     


    You can use the following function:




    //JScript


    function FunctionExists(UnitName, FunctionName)


    {


     var oTC = Sys.OleObject("TestComplete.TestCompleteApplication");


     var strProjectName = /\\([^\\]+).mds$/.exec(Project.FileName)[1];


     var colFunctions = oTC.Integration.ProjectRoutinesIterator(strProjectName);


     colFunctions.Reset();


     


     var oFunc;


     while (colFunctions.HasNext())


     {


        oFunc = colFunctions.Next;


        if ((oFunc.UnitName == UnitName) && (oFunc.Name == FunctionName))


          return true;


     }


     return false;


    }




     

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Ali,


     


    You can use the following function:




    //JScript


    function FunctionExists(UnitName, FunctionName)


    {


     var oTC = Sys.OleObject("TestComplete.TestCompleteApplication");


     var strProjectName = /\\([^\\]+).mds$/.exec(Project.FileName)[1];


     var colFunctions = oTC.Integration.ProjectRoutinesIterator(strProjectName);


     colFunctions.Reset();


     


     var oFunc;


     while (colFunctions.HasNext())


     {


        oFunc = colFunctions.Next;


        if ((oFunc.UnitName == UnitName) && (oFunc.Name == FunctionName))


          return true;


     }


     return false;


    }




     

  • dsstrainer's avatar
    dsstrainer
    Occasional Contributor
    Here it is converted to vbscript





    //VBScript




    Function FunctionExists (UnitName, FunctionName)


        


        Dim oTC, objRE, colFunctions, oFunc, objMatch


     


        set oTC = Sys.OleObject("TestComplete.TestCompleteApplication")


     


        Set objRE = New RegExp


     


        With objRE


            .Pattern    = "\\([^\\]+).mds$"


            .IgnoreCase = True


            .Global     = False


        End With


     


        If objRE.Test( Project.FileName ) Then


            Set objMatch = objRE.Execute( Project.FileName )


            strProjectName = objMatch.Item(0).SubMatches(0)


        Else


            strProjectName = "" 


        End If


     


        Set objRE = Nothing


     


        FunctionExists = false


     


        if (strProjectName <> "") Then 


     


            set colFunctions = oTC.Integration.ProjectRoutinesIterator(strProjectName)


     


            colFunctions.Reset()


     


            while (colFunctions.HasNext())


                set oFunc = colFunctions.Next


                if ((oFunc.UnitName = UnitName) AND (oFunc.Name = FunctionName)) then


                    FunctionExists = true


                end if


            Wend


     


        End if


     


    End Function







    It should be noted that this checks for the existence of the unit/function as an added source unit to the project. As long as the the library is loaded in the project, it will show. even if you don't have it added to the executing code with USEUNIT. .. so that makes it somewhat limited in practical use. I don't know of a way to only check for the existing of loaded units in the active project at runtime.




  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi Everybody,

     


    Based on this conversation, we've created a How To article on our Support portal - Check if a function is declared.


     


    Dss Trainer, You've got +30 points to your reputation! Thanks for your VBScript sample - we have published it in the article. 


     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    I use similar techniques for executing methods within script units but I don't do a check to see if it exists first.  Instead, I wrap the code in a try/catch exception handler so that, if the method fails, I use that as the indication that something didn't function correctly.  You could probably adapt a similar technique to determine whether or not a method exists by testing it at the exception level.



    Other than that, I don't know of any specific functions or tools within TestComplete to do that sort of detection.  
  • ali_jafer's avatar
    ali_jafer
    Occasional Contributor
    Thank you Tanya,

    Once I thought there could not be any solution, can you please have a look at follwoing requests


    1. Provide a bit of commentry especially on code before "While" block, so that I understand what is going on


    2. Will this work for Script Unit referenced into a Project?


    3. I am translating you given Code into VBScript as follows but at bold line TC is flagging error, can you please help to fix it? 





    Function FunctionExists(UnitName, FunctionName)


      var oTC = Sys.OleObject("TestComplete.TestCompleteApplication")


      var strProjectName = /\\([^\\]+).mds$/.exec(Project.FileName)[1]


      var colFunctions = oTC.Integration.ProjectRoutinesIterator(strProjectName)


     


     colFunctions.Reset()


     


     var oFunc


     


      while (colFunctions.HasNext())


        oFunc = colFunctions.Next


        if ((oFunc.UnitName = UnitName) && (oFunc.Name = FunctionName)) then


          FunctionExists= true


        end if


      Wend 


    FunctionExists= false


     


    End Function