Forum Discussion

gita3632's avatar
gita3632
Contributor
14 years ago

Calling Method from different PROJECTS in the same SUITE

Within the same SUITE, can I call methods from project A that was defined in Project B?



Please show the syntax ....



Thanks
  • sbkeenan's avatar
    sbkeenan
    Frequent Contributor
    Hi Leon



    What scripting language are you using?



    In VBScript, for example, you can do this:





    ----- Unit1 -----



    'USEUNIT Unit2



    sub main

      dim x, y, z

      x = 3

      y = 4



      z = unit2.mySum(x, y)



      log.message("Sum is " & z)

    end sub





    ----- Unit2 -----



    function mySum(num1, num2)

      mySum = num1 + num2

    end function







    If you are using a class, say in unit 2, you'll need to write a global function, i.e. outside the class,  so that you can create an instance of it, for example:



    ----- Unit1 -----



    'USEUNIT Unit2



    dim mc = Unit2.newMyClass()



    sub main

      dim x, y, z

      x = 3

      y = 4



      z = mc.mySum(x, y)



      log.message("Sum is " & z)

    end sub





    ----- Unit2 -----



    function newMyClass()

      set newMyClass = new myClass

    end function



    class myClass

      function mySum(num1, num2)

        mySum = num1 + num2

      end function



    end class







    As far as I know, if you're using JScript, you will not need to create the global function outside the class, you will, instead, simply be able to create a new instance in Unit1 by using





    var mc = new Unit2.myClass();





    Hope this helps.



    Regards

    Stephen.





  • leon_jin's avatar
    leon_jin
    Occasional Contributor
    Hi Jared,



    After I added link of the project following the step you recommended, how can I call the method defined in project B from project A?
  • leon_jin's avatar
    leon_jin
    Occasional Contributor
    Hi Stephen,



    I am using VBScript and not use class. When I execute the sample you gave, I saw error message say "The USEUNIT section contains a reference to the 'Unit2' unit that was not found in the project." Details please refer to attached screenshot. Could you please tell me is there somethings I need to do?
  • Hi Leon.



    You may add (not copy) existing item into your current project from another project (you'll see "shortcut arrow" on the unit symbol indicating link to "external" module). This mechanism allows shared use of modules across different projects (which is not limited to the same suite).



    If you need common library built from your modules, I think the best approach in using script extensions.



  • Sorry I forgot to attach screenshot.



    You may see that FormTesting and Notifier are local units (from current project) while Actions and Screens are links to "external" units (from another project or location).



    You may use USEUNIT with this linked units as if they were created in your project locally.
  • leon_jin's avatar
    leon_jin
    Occasional Contributor
    thank for the reply. But this scenario can't work for me. I have to add an existing project into another project under the project suite, because there are some files have same name in those 2 projects. So I have to call a rountine from another project like the structure I posted in the screenshot. Is there any other suggestion for me?
  • sbkeenan's avatar
    sbkeenan
    Frequent Contributor
    Hi Leon



    Is it possible that you can create a new project, call it 'Globals' and place the routines that you want to share in a script in that project.  Perhaps call the script 'SharedRoutines'



    You should then add this to any project in which you want to use them by adding a  reference to it.  Do this by right-clicking on the script section of these projects and select 'Add Existing Item'.  from the window, browse to the location of the 'SharedRoutines' script and select it.  This will place what looks like a shortcut in your project and it should then be usable by implementing the techniques decsribed in the earlier post.



    You may also need to merge your name mapping file if you use one.  you can do this by right-clicking on the NameMapping item to be updated and select 'Merge With...'  Browse to the name mapping item of the project you want to merge with and select it.



    Everything should then work for you.



    Please let me know how you get on.



    Regards

    Stephen.
  • leon_jin's avatar
    leon_jin
    Occasional Contributor
    Finally, I have to renamed one project files and shared these files to the target project. It seems impossible to access a routine directly from another project in TestComplete even though those 2 projects in 1 project suite.



    High appreciate your helps on this question.
    • vik33's avatar
      vik33
      Occasional Contributor

      i have a realted issue:

       

      I have tried both the methods to call a function from another project. Call goes fine but script execution fails due to object not found error. How would i make use of the NameMapping objects that belong to Project 2.

      To make it simple:

      ' Unit A has a reference for unit B
      'Unit A is in Project A
      'call is from unit A to --> sub B of unit B

       

      Unit A
      'calling sub B from unit A (below statement)
      unit B.Sub B

      -------------------------------

      'Unit B is in a Project B and has its own Namemapping.
      Unit B

      Sub B
      ' here obj_B is getting used.
      end sub

       

      Here is the issue:
      NameMapping for Project A has something like:
      Aliases.Process_A.obj_Parent.obj_A

       

      NameMapping for Project B has something like:
      Aliases.Process_A.obj_Parent.obj_B

       

      The script tries to find obj_B in the NameMapping for Project A which is what causing it to fail.

      How would i make sure that obj_B should be found in NameMapping of Project B?

       

      Please note that if i execute Sub B from project B on its own (without calling it from Proj A) then it passes without any issues.

      Please help.

      Many thanks.

      -vik

      • m_essaid's avatar
        m_essaid
        Valued Contributor

        Hi,

         

        As you could share scripts, you could share mapping within a same suite.

         

        It is useful for me in the following case : I test almost the same scenarii on two executables (an alpha and a beta version of the same software). Thus, 95% of the mapping is the same.