cunderw
9 years agoCommunity Hero
That's actually very simlar to what I did. It's just a pain that it has to be done this way IMO
Here is how I did it. Note, it's more specfic to our needs.
var runCount = 0; var totalTests = getNumEnabledTests(Project.TestItems); function GeneralEvents_OnStopTest(Sender) { if (Project.Variables.postRunEnabled) { // exports and emails log summary runCount++; try { BuildAndSendLogs(); } catch(err) { Log.Warning(err); } } else { Log.Message("PostRun Disabled"); } } function BuildAndSendLogs() { // checks if a test passed or failed and sets status for email subject var testStatus = GetTestStatus(); if (testStatus == "FAILED") { projectStatus = "FAILED"; } // builds email body emailBody += "\n\n" + GetLogItems(); // if all tests have ran, send the email if (runCount == totalTests) { emailBody += "\n\nEnd Time: " + aqDateTime.Now(); emailSubject += " " + projectStatus; try { if (SendMail(emailAddressTo, emailServer, emailWho, emailAddressSender, emailSubject, emailBody)) Log.Message("Mail was sent"); else Log.Warning("Mail was not sent"); } catch(err) { Log.Warning("Error building email and sending: " + err); } } } function GetLogItems() { // creates a random temp folder to export the logs too var tempFolder = aqEnvironment.GetEnvironmentVariable("temp") + "\\" + GetTickCount() + "\\"; if (0 != aqFileSystem.CreateFolder(tempFolder)) { Log.Error("The " + tempFolder + " temp folder was not created"); return ""; } // exports the log to the created folder if (!Log.SaveResultsAs(tempFolder, lsHTML, false, 2)) { Log.Error("Log was not exported to the " + tempFolder + " temp folder"); return ""; } // loads the xml from the xpoerted log var xDoc = Sys.OleObject("MSXML2.DOMDocument.4.0"); xDoc.load(tempFolder + "root.xml"); // calls logdatatext with the needed node from the xml file var result = LogDataToText(xDoc.childNodes.item(1), 0, " "); // deletes temp folder aqFileSystem.DeleteFolder(tempFolder, true); return result; } function LogDataToText(logData, indentIndex, indentSymbol) { // if this doesn't match it means something was not exported correct so it returns empty string if ("LogData" != logData.nodeName) { return ""; } var result = ""; for(var i = 0; i < indentIndex; i++) { result += indentSymbol; } result = result + "Name: " + logData.getAttribute("name") + indentSymbol + "Status:" + indentSymbol + GetTextOfStatus(logData.getAttribute("status")) + "\r\n"; for(var i = 0; i < logData.childNodes.length; i++) { result += LogDataToText(logData.childNodes.item(i), indentIndex + 1, indentSymbol); } return result; } function GetTextOfStatus(statusIndex) { // returns text for a status code switch(statusIndex) { case "0": return "PASSED"; case "1": return "WARNING"; case "2": return "FAILED"; default: return "UNDEFINED"; } } function GetTestStatus() { // returns status code var result; var tempFolder = aqEnvironment.GetEnvironmentVariable("temp") + "\\" + GetTickCount() + "\\"; if (0 != aqFileSystem.CreateFolder(tempFolder)) { Log.Error("The " + tempFolder + " temp folder was not created"); return ""; } if (!Log.SaveResultsAs(tempFolder, lsHTML, false, 2)) { Log.Error("Log was not exported to the " + tempFolder + " temp folder"); return ""; } var xDoc = Sys.OleObject("MSXML2.DOMDocument.4.0"); xDoc.load(tempFolder + "root.xml"); var logData = xDoc.childNodes.item(1); result = GetTextOfStatus(logData.getAttribute("status")); aqFileSystem.DeleteFolder(tempFolder, true); return result; } function getNumEnabledTests(testItems) { if(getNumEnabledTests.enabledCount == undefined) { getNumEnabledTests.enabledCount = 0; } var itemCount = testItems.ItemCount; // buids total tests based on if they are enabled. // note this is super hacky, but I dunno how else to get it for(var i = 0; i < itemCount; i++) { if(testItems.TestItem(i).Enabled) { if(testItems.TestItem(i).ElementToBeRun != null) { getNumEnabledTests.enabledCount++; } if(testItems.TestItem(i).ItemCount != 0) { getNumEnabledTests(testItems.TestItem(i)); } } } return getNumEnabledTests.enabledCount; }
Related Content
- 9 years ago