Vec84
8 years agoContributor
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.