Forum Discussion

Rajesh2's avatar
Rajesh2
Contributor
8 years ago

How to import whole script from another script?

Hi Team,

 

Greetings,

 

    I have created Unit scripts under Project Suite>Project>Advanced>Script in my project. I have 2 scripts, Script1 and Script2 which has 10 def blocks in each.

Now i need to import whole script 'Script1' in 'Script2' wherein i should be able to use the def blocks of Script1 in Script2. How to go about it?

 

example:

Program which is working

#Script2:

 

from Script1 import test1(), test2()

def callScript1Methods():

  test1()

  test2()

 

==========================

 

Program which isn't working

#Script2:

 

import Script1

def callScript1Methods():

  test1()

  test2()

 

The program which is working will be complex when there are 100 def blocks in a single script and you need to import all 100 def blocks separately. Can anyone plese let me know is there any alternative way of importing scripts? 

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    You can do either this

     

    from Script1 import *
    def callScript1Methods():
      test1()
      test2()

    or this

     

    import Script1
    def callScript1Methods():
      Script1.test1()
      Script1.test2()

    Notice in the second example, you need to explicitly indicate the unit from which you are getting the routine.  If you are using the syntax of from <unit> import * you don't need to specify the unit name in the method calls.

    See https://support.smartbear.com/testcomplete/docs/scripting/calling-routines/declared-in-another-unit/python.html

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      Unless my syntax is incorrect karthick7, USEUNIT is not supported for Python in TestComplete.  I tried the following:

       

      #[Unit1]
      def test1():
          Log.Message('this is test 1')
      
      #[Unit2]
      #USEUNIT Unit1
      def test2():
          Unit1.test1()

       

      I'm not really a Python guy, but as far as I can tell, USEUNIT won't work. You must use either of the two methods that I indicated in my earlier post
  • Hi ,

     

    Please Use USEUNIT Method.To Access the any function throughtout the scripts.