Forum Discussion

ch5082's avatar
ch5082
Occasional Contributor
15 years ago

function library

I have looked at the help files concerning calling functions from other projects/units but I'm still not sure how they work.  For example I'll have a common "Logon" function that takes Username & Password.  How do I build the file that contains this function?  How do I call that function from my "Main" script?




6 Replies

  • giriprasasd100's avatar
    giriprasasd100
    Occasional Contributor
    Hi Chuck Hunt,



    The functions from other scripts units can be referenced based on where it is created !



    Let me assume you have your "Logon" function in "LogonScript" and "Main" function in "MainScript"



    1. If Both "LogonScript" and "MainScript" scripts are available in same project :

                In the "MainScript" script unit refer the "LogonScript" script unit just by adding "USEUNIT" statement as below -

                For JScript ---> //USEUNIT LogonScript

                Don't forget to comment it and it should be in the first line of "MainScript" unit.

                (This is similar to using #include in c language)



    2. If "LogonScript" and "MainScript" scripts are available in different projects :

               i. Add the "Logon" file by right clicking "Script" project item in the project where "MainScript" script unit is present  and "Add => Existing Item"

               ii. Now browse for "LogonScript" script unit and add it.

               iii. Now your "LogonScript" unit is available as reference only. It is not duplicated.



                In the "MainScript" script unit refer the "LogonScript" script unit just by adding "USEUNIT" statement as below -

                For JScript ---> //USEUNIT LogonScript

                Don't forget to comment it and it should be in the first line of "Main" script.

                (This is similar to using #include in c language)



    After adding the "USEUNIT" statement, call the function as ==> LogonScriipt.Logon()



    Hope it helps you :) Have a great day !







  • giriprasasd100's avatar
    giriprasasd100
    Occasional Contributor
    Also you do it as simple as this :

                =====>  Right-click the desired scripting unit in the Project Explorer, and select the
    Add Unit References menu item. Now select the units to be referenced and start you function calling !



  • parinita's avatar
    parinita
    New Contributor
    so do we create a function in the same unit or we have saperate function liberary to create that function
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Just like with regular application code, you'd create library units that contain your commonly used functions.  What we do, then, is put those units in a "common" folder within our source control.  then, when we create a new project and need a function, we just add that unit to the project and then add the unit to the "uses" clause of the test unit.


  • royd's avatar
    royd
    Regular Contributor

    Hi Alexey

     

    I understand the scope of the script extension is huge, but is it possible to create a tutorial or video on just the issue of creating an extension and managing common scripts and using them in real life? I looked around the link you provided and I am lost! I am very new to TestComplete as well as jscript (couple of months). Lot to learn, as well as produce some work as well, I am sure you understand. I am sending more time looking for answers than actual work. Documentation is not very helpful for people like me, who has very little or no experience in scripting (manual QA).

     

    I truly believe, that there are too many of us who will greatly benefit from the tutorial.

     

    Thanks in advance.

     

    Dave