Forum Discussion

Vec84's avatar
Vec84
Contributor
7 years ago
Solved

Store data in an object in script (Jscript)

Hello,

 

I have a function that writes in to excel with certain results i obtain from the test i am running. After every loop of the data driven test this excel function is called and writes to the worksheet. This isn't very efficient and sometimes causes errors due to the amount of times it is called.

 

I would like to store the results into an object and then write them out to excel once the data loop has been completed(Sample of my function)

 

function WriteDataToExcel(fname, sheetName,testTime, errorCount, warnCount){

var app = Sys.OleObject("Excel.Application");
var book = app.Workbooks.Open(fname);
var sheet = book.Sheets.Item(sheetName);
var rowCount = sheet.UsedRange.Rows.Count+1;
var columnCount = sheet.UsedRange.Columns.Count;

var Logs;
var Count;

// Obtains the object that holds the list of project logs
Logs = Project.Logs;
Count = Logs.LogItemsCount;
var TestResult = Logs.LogItem(Count-1).Status;

switch (TestResult) //previously it is like Status which is undefined
{
case 0:
TestResult = "Passed"
break;

case 1:
TestResult = "Warnings"
break;

case 2:
TestResult = "Failed"
break;

default :
TestResult = "Error"
break;
}


var arrayLogData = [aqConvert.DateTimeToFormatStr(aqDateTime.Now(), "%d/%m/%y %H:%M"),
Logs.LogItem(Count-1).Name,
TestResult,
testTime,
errorCount,
warnCount]
// When you need to add additional data, just add the new element here

sheet.Range("A" + rowCount).Value = alignData(arrayLogData[0]);
sheet.Range("B" + rowCount).Value = alignData(arrayLogData[1]);
sheet.Range("C" + rowCount).Value = alignData(arrayLogData[2]);
sheet.Range("D" + rowCount).Value = alignData(arrayLogData[3]);
sheet.Range("E" + rowCount).Value = alignData(arrayLogData[4]);
sheet.Range("F" + rowCount).Value = alignData(arrayLogData[5]);
// If you want add more columns then you can add like below
//sheet.Range("G" + inputRow).Value = alignData(arrayLogData[6]);

book.Save();
app.Quit();

}
  • What I would do is create a global variable of some sort as an array... then, during the course of your testing, add rows to that array containing the desired information.  Then, at the end of the test case, you loop through that array and write your values out to your Excel file.  

7 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    What I would do is create a global variable of some sort as an array... then, during the course of your testing, add rows to that array containing the desired information.  Then, at the end of the test case, you loop through that array and write your values out to your Excel file.  

    • Vec84's avatar
      Vec84
      Contributor

      Hello Robert,

       

      Thanks for the your reply.

       

      One of our developers mentioned a data table object however i couldn't find one in the jscript language. I didn't even think of adding it to a Table Variable.

       

      You save the day again...Not all hero's wear capes :smileyvery-happy:

    • Vec84's avatar
      Vec84
      Contributor

      The answer showed different on my emails so i guess you edited the response sorry.

       

      Would i be able to create a multi-dimensional Array to save the data, i couldn't find this on the Msdn site.

       

      Even so, i could just add a project table variable and add columns and rows every time and then write them out to excel after.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        The reason for the edit is that, when I looked it up, I couldn't find a way to add rows to a Project variable.  

         

        As for making multi-dimensional arrays... it's just a matter of making an array of arrays... or an array of objects.  Using JavaScript syntax (which is similar to JScript):

         

        var testArray = []
        testArray.push({RESULT: 'Error']});

        This will push an element to the array that is an object with a property of RESULT and a value of 'Error'.  If you access testArray[0].RESULT, that will return the value 'Error'.

         

         

  • Vec84's avatar
    Vec84
    Contributor
    Thanks Again Rob.

    Ah ok, so maybe the project variable is not an option.

    This is exactly what I need.
  • Vec84's avatar
    Vec84
    Contributor
    Thanks Joseph.

    Going to consider the best option now..

    Cheers