Forum Discussion

TestQA1's avatar
TestQA1
Frequent Contributor
3 years ago

Creating array with Project variable

Hi,

I have a func that repeats in different Scenarios of feature.

function test() {
If (a> b) {
a = project.variable.temp
}
}
Now I need to have an array variable inside the function that stores the values of 'a' each time the function is called in Scenarios and then finally return itself from the function as a normal array. Something like when scenario1 runs this function, 'a' would be 3, then 2,6,0 in rest of the scenaris and the final array would be finalarray = {3,2,6,0} . So when I need to get its value in another function i can simply call by index.

Any help is appreciated.

Thankyou.
  • You can initialise the object type at the very beginning of the script (global variable),

    // Temporary project (or suite) variable of type Object
    Project.Variables.TempArray = new Array();
    
    function test1()
    {
        // Add
        Project.Variables.TempArray.push("3");
    }
    
    function test2()
    {
        // Add
        Project.Variables.TempArray.push("2");   
    }
    
    function test3()
    {
        // Print
        for (var i = 0; i < Project.Variables.TempArray.length; i++) {
            Log.Message(Project.Variables.TempArray[i]);
        }
    }

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Here's a simple example,

    function test1()
    {
        // Temporary project (or suite) variable of type Object
        Project.Variables.TempArray = new Array();
    
        // Add
        Project.Variables.TempArray.push("3");
        Project.Variables.TempArray.push("2");
        Project.Variables.TempArray.push("6");
        Project.Variables.TempArray.push("0");
        
        // Print
        for (var i = 0; i < Project.Variables.TempArray.length; i++) {
            Log.Message(Project.Variables.TempArray[i]);
        }
    }

     

    • TestQA1's avatar
      TestQA1
      Frequent Contributor

      Thanks. How do you intialise the object type array in this case. If we don't it remains unassigned in Variables page?

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You can initialise the object type at the very beginning of the script (global variable),

    // Temporary project (or suite) variable of type Object
    Project.Variables.TempArray = new Array();
    
    function test1()
    {
        // Add
        Project.Variables.TempArray.push("3");
    }
    
    function test2()
    {
        // Add
        Project.Variables.TempArray.push("2");   
    }
    
    function test3()
    {
        // Print
        for (var i = 0; i < Project.Variables.TempArray.length; i++) {
            Log.Message(Project.Variables.TempArray[i]);
        }
    }