cancel
Showing results for 
Search instead for 
Did you mean: 

Store data in an object in script (Jscript)

SOLVED
Vec84
Contributor

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();

}
1 ACCEPTED SOLUTION

Accepted Solutions
tristaanogre
Community Hero

Re: Store data in an object in script (Jscript)

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.  


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

7 REPLIES 7
tristaanogre
Community Hero

Re: Store data in an object in script (Jscript)

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.  


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

View solution in original post

Vec84
Contributor

Re: Store data in an object in script (Jscript)

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 Smiley Very Happy

Vec84
Contributor

Re: Store data in an object in script (Jscript)

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
Community Hero

Re: Store data in an object in script (Jscript)

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'.

 

 


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Vec84
Contributor

Re: Store data in an object in script (Jscript)

Thanks Again Rob.

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

This is exactly what I need.
joseph_michaud
Moderator

Re: Store data in an object in script (Jscript)

I think you can still use a Project Variable of the Table type.  To add rows, change the RowCount property.  These articles have a bit more info:

 

Variables of the Table Type - Creating Table Variables

TableVariable Object

-----
Joseph
Vec84
Contributor

Re: Store data in an object in script (Jscript)

Thanks Joseph.

Going to consider the best option now..

Cheers
New Here?
Join us and watch the welcome video:
Top Kudoed Authors