Forum Discussion

SarahEdwards's avatar
SarahEdwards
Contributor
8 years ago
Solved

How do I instantiate a Project Variable at the Test level?

I'm calling a Project Variable in a Keyword Test, but the variable isn't instantiated when I run that test alone. Is there a code snippet I could run to instantiate that variable when I run the test on it's own? I want to use the project variables to determine the URL for each test run, differentiating between our test server and our prod server.

  • tristaanogre's avatar
    tristaanogre
    8 years ago

    Actually, I think I might know what's up...

    What I did is created a new persistant project variable called "foo" with a default value of "bar"

     

     

    Notice that the local value is blank.

    If I create a Keyword test now with out first setting the value of foo and tell it to log a message using the "foo" variable, I get a blank message.

     

    If I first populate "foo" with a Local Value, then run the same keyword test, the message now contains that local value... 

     

    Now... this is different than if I create a TEMPORARY project variable with a default value. Then, no matter what, if I don't first assign a value to the variable, I'll get the default value.

     

    That's the difference between persistent and temporary. Persistant variables store the values assigned to them during a test and persist those values even after the test run is complete. Temporary variables values are initialized by the default but will vary during the test run and will not persist after the run is complete.

    So... the question is... which are you using? If you're using a Persistant variable and are not setting a local value before you try to use it, you will get nothing unless you explicitly set the value using some sort of expression (as per bbi).  However, if you are using a temporary variable with a default value, then you will not need to use some special assignment to first initialize the variable before you use it.


5 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I'm not 100% certain what you are asking (difference in terminology, perhaps).

    If I am correct, you are trying to use a project variable in a keyword test. However, when you do so, the variable is returning a blank value. Is that correct?

    If so, when you edit the variable, there is a "Default value" you can define. When you do so, no matter where the test is called, it will use the default value unless you change it.

    • bbi's avatar
      bbi
      Contributor

      In addition you can define and set/get at test execution level with this snippet:

       

        /**
         * Définir une variable d'une ProjectSuite TestComplete<br>
         * Si la variable n'existe pas elle est créée
         * @function
         * @param {string} VarName - Nom de la variable
         * @param {variant} Value - Valeur de la variable
         * @param {string} Type - Valeur TestComplete du type de la variable (<b>"String"</b>, <b>"Boolean"</b>, <b>"Double"</b>, <b>"Integer"</b>)
         */
        function setProjectSuiteVariable(VarName, Value, Type) {
          if (!ProjectSuite.Variables.VariableExists(VarName)) {
            ProjectSuite.Variables.AddVariable(VarName, Type);
          }
          ProjectSuite.Variables[VarName] = Value;
        };
      
        /**
         * Lire une variable d'une ProjectSuite TestComplete<br>
         * Si la variable n'existe pas une valeur par défaut est retournée
         * @function
         * @param {string} VarName - Nom de la variable
         * @param {variant} DefaultValue - Valeur par défaut de la variable
         * @returns {variant} Renvoie la valeur de la variable ou <i>DefaultValue</i> si elle n'existe pas
         */
        function getProjectSuiteVariable(VarName, DefaultValue) {
          return (ProjectSuite.Variables.VariableExists(VarName) ? ProjectSuite.Variables[VarName] : DefaultValue);
        };
    • SarahEdwards's avatar
      SarahEdwards
      Contributor
      Thanks for your reply. Either there's a bug or you are misinformed, because I have set the default value for the variable already. I will try bbi's snippet; I think that's probably what I need.
      tristaanogre wrote:

      I'm not 100% certain what you are asking (difference in terminology, perhaps).

      If I am correct, you are trying to use a project variable in a keyword test. However, when you do so, the variable is returning a blank value. Is that correct?

      If so, when you edit the variable, there is a "Default value" you can define. When you do so, no matter where the test is called, it will use the default value unless you change it.


       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Actually, I think I might know what's up...

        What I did is created a new persistant project variable called "foo" with a default value of "bar"

         

         

        Notice that the local value is blank.

        If I create a Keyword test now with out first setting the value of foo and tell it to log a message using the "foo" variable, I get a blank message.

         

        If I first populate "foo" with a Local Value, then run the same keyword test, the message now contains that local value... 

         

        Now... this is different than if I create a TEMPORARY project variable with a default value. Then, no matter what, if I don't first assign a value to the variable, I'll get the default value.

         

        That's the difference between persistent and temporary. Persistant variables store the values assigned to them during a test and persist those values even after the test run is complete. Temporary variables values are initialized by the default but will vary during the test run and will not persist after the run is complete.

        So... the question is... which are you using? If you're using a Persistant variable and are not setting a local value before you try to use it, you will get nothing unless you explicitly set the value using some sort of expression (as per bbi).  However, if you are using a temporary variable with a default value, then you will not need to use some special assignment to first initialize the variable before you use it.