Forum Discussion

AlexKaras's avatar
AlexKaras
Champion Level 3
8 years ago

Script Extension: When does the 'extension loading into memory' event occurs?

Hi,

 

From https://support.smartbear.com/testcomplete/docs/working-with/extending/script/creating/initialization-and-finalization-routines.html : "The initialization routine of a script is executed when TestComplete loads the script extension in memory".

 

The question : does anybody know for sure (from one's experience) when the above event occurs? Does it occur when TestComplete starts and loads the given script extension into memory or when test execution starts or at some other moment of time?

 

The reason for the question : I am considering script extension that needs to have some internal variable to be initialized to zero on test execution start and keep changed value during test items execution within this test run.

Already existing regular script code uses (temporary) project variable that is assigned default value exactly on test run start.

I have some doubts that the same behavior is implemented for script extensions. Can anybody confirm/disapprove please ?

(Robert, Helen ?)

  • As a test, I created a script unit for a script extension with just the following:

     

    var mySomething;
    
    function getMySomething() {
        return mySomething;
    }
    
    function setMySomething(value) {
        mySomething = value;
    }
    
    function Initialize() {
        mySomething = 'this is the initial value';    
    }

    I encapsulated this into an extension with the following Description.XML

     

    <ScriptExtensionGroup Name="TestThis">
        <Category Name="TestThat">
            <ScriptExtension Name="TestInitialization" Author="Robert Martin" Version="1.0">
                <Script Name="testExtensionInit.sj" InitRoutine="Initialize">
                    <RuntimeObject Name="TestInit">
                        <Property Name="MySomething" GetRoutine="getMySomething" SetRoutine="setMySomething">Property description</Property>
                        <Description>Runtime object description</Description>
                    </RuntimeObject>
                </Script>
                <Description>Script extension description</Description>
            </ScriptExtension>
        </Category>
    </ScriptExtensionGroup>

    I then wrote this code:

     

    function blah() {
        Log.Message(TestInit.MySomething);
        TestInit.MySomething = 'New Value';
        Log.Message(TestInit.MySomething);
    }

    The first time I run this code after starting TestComplete I get a log that looks like this:

     

     

    If I run it again without restarting TestComplete, I get the following:

     

     

    So...  the Initialization is executed at loading of TestComplete. The value in the variable persists beyond the test run so long as TestComplete has not been restarted.

    If you want a variable value to be initialized in a script extension at the start of a test run, I believe you'll still need to call specific code to initialize the value.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    As a test, I created a script unit for a script extension with just the following:

     

    var mySomething;
    
    function getMySomething() {
        return mySomething;
    }
    
    function setMySomething(value) {
        mySomething = value;
    }
    
    function Initialize() {
        mySomething = 'this is the initial value';    
    }

    I encapsulated this into an extension with the following Description.XML

     

    <ScriptExtensionGroup Name="TestThis">
        <Category Name="TestThat">
            <ScriptExtension Name="TestInitialization" Author="Robert Martin" Version="1.0">
                <Script Name="testExtensionInit.sj" InitRoutine="Initialize">
                    <RuntimeObject Name="TestInit">
                        <Property Name="MySomething" GetRoutine="getMySomething" SetRoutine="setMySomething">Property description</Property>
                        <Description>Runtime object description</Description>
                    </RuntimeObject>
                </Script>
                <Description>Script extension description</Description>
            </ScriptExtension>
        </Category>
    </ScriptExtensionGroup>

    I then wrote this code:

     

    function blah() {
        Log.Message(TestInit.MySomething);
        TestInit.MySomething = 'New Value';
        Log.Message(TestInit.MySomething);
    }

    The first time I run this code after starting TestComplete I get a log that looks like this:

     

     

    If I run it again without restarting TestComplete, I get the following:

     

     

    So...  the Initialization is executed at loading of TestComplete. The value in the variable persists beyond the test run so long as TestComplete has not been restarted.

    If you want a variable value to be initialized in a script extension at the start of a test run, I believe you'll still need to call specific code to initialize the value.

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi Robert,

       

      > the Initialization is executed at loading of TestComplete.

      As I expected it to be... Not good...

      Anyway, thank you a lot for all your efforts and provided evidence! Your reply answers my question exactly as I would like the answer to be. Thank you again.

      Excuse me that you had to play with custom extension... - I hoped that you already have known the answer as you have dealt with script extensions a lot. (Maybe this is something that can be used in your upcoming training if you consider to compare properties in script extension with project variables.)

       

      HKosova: Helen, could you please talk to help writers and discuss with them whether the mentioned help topic can be made more clear using the input from Robert?

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        AlexKaras, I've passed your comments to the team. Thanks for the feedback!

         


        tristaanogre wrote:

        If you want a variable value to be initialized in a script extension at the start of a test run, I believe you'll still need to call specific code to initialize the value.


         ^^ This, optionally coupled with OnStartTest.