Forum Discussion

Lagencie's avatar
Lagencie
Frequent Contributor
5 years ago

Script only gets called once

Hello,

 

I have a problem with one of my scripts

 

import time
import traceback

dosomething
connected = obj.Connect(login)
  
def factory_reset(mode):
  dosomething
def nextfunction():
  dosomething

This is my script and when running a single test it works always - but the login data changes between 2 tests and when I run those 2 tests, which both call the function factory_reset, after another, it takes the same login data for both tests.

 

I included a Log.Warning("warning") before the obj.connect and it does not even write out this Warning in the second test, so it seems like it cached the run of the script in the first test somehow.

Is it possible to "close" this script file / kick it out of the cache so it initializes again

4 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    That's not exactly the best way to write code in TestComplete.  Everything you want to execute should be part of a function and called explicitly.  Implicit calls like that are only done when the code unit is first loaded.

    • Lagencie's avatar
      Lagencie
      Frequent Contributor

      My problem is, when I write it in functions it always says obj is used before being initialized ... 

       

      even though when I debug i see that it gets set and it has a value - but the moment I am using it and step over the error occurs ...

       

      Is there a way to "throw away" a script during runtime, so it gets actively reloaded

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    I am not using Python and might miss something, but according to my knowledge and as Robert has said:

    a) I never heard about any scripting language runtime that allows dynamic code load/unload;

    b) All scripting language runtimes load script code, execute all statements that are out of any function and then execute the 'main' function.

     

    Considering the above,

    dosomething
    connected = obj.Connect(login)

    lines are executed on test start before any test method is executed (i.e. even before your first test starts its execution).

    connected variable in this case is a global one for the whole test run, it keeps the value returned by the obj.Connect(login) call and exists even between different tests until test run ends.

     

    • Lagencie's avatar
      Lagencie
      Frequent Contributor

      I have managed the problem and also found out what I did wrong.

       

      for python i need the keyword

      global obj
      global login

      in the function where i initialize them, this way I can work even between multiple calls with the variables and make multiple connections.