public static GhprTestCase GetTestRun(XmlNode testNode, ILogger logger) { try { var now = DateTime.UtcNow; var testType = testNode.SelectSingleNode("properties/property[@name='TestType']")?.Val(); var priority = testNode.SelectSingleNode("properties/property[@name='Priority']")?.Val(); var description = testNode.SelectSingleNode("properties/property[@name='Description']")?.Val(); var categories = testNode.SelectNodes("properties/property[@name='Category']")?.Cast <XmlNode>() .Select(n => n.Val()).ToArray(); var testDataDateTimes = testNode.SelectNodes( $"properties/property[contains(@name,'{Paths.Names.TestDataDateTimeKeyTemplate}')]")? .Cast <XmlNode>() .Select(n => n.Val()).ToList(); var testDataActuals = testNode.SelectNodes( $"properties/property[contains(@name,'{Paths.Names.TestDataActualKeyTemplate}')]")? .Cast <XmlNode>() .Select(n => n.Val()).ToArray(); var testDataExpecteds = testNode.SelectNodes( $"properties/property[contains(@name,'{Paths.Names.TestDataExpectedKeyTemplate}')]")? .Cast <XmlNode>() .Select(n => n.Val()).ToArray(); var testDataComments = testNode.SelectNodes( $"properties/property[contains(@name,'{Paths.Names.TestDataCommentKeyTemplate}')]")? .Cast <XmlNode>() .Select(n => n.Val()).ToArray(); var testData = new List <TestDataDto>(); for (var i = 0; i < testDataDateTimes?.Count; i++) { testData.Add(new TestDataDto { TestDataInfo = new SimpleItemInfoDto { Date = DateTime.ParseExact(testDataDateTimes?[i], "yyyyMMdd_HHmmssfff", CultureInfo.InvariantCulture), ItemName = "Test Data" }, Actual = testDataActuals?[i], Expected = testDataExpecteds?[i], Comment = testDataComments?[i] }); } var r = testNode.GetAttribute("result"); var l = testNode.GetAttribute("label"); var fullName = testNode.GetAttribute("fullname"); var testGuid = GetTestGuid(testNode); var name = testNode.GetAttribute("name"); var duration = double.Parse(testNode.GetAttribute("duration") ?? "0.0", CultureInfo.InvariantCulture); var id = testNode.GetAttribute("id") ?? ""; var parentId = testNode.GetAttribute("parentId") ?? ""; if (fullName.Contains(name)) { var ns = fullName.Substring(0, fullName.LastIndexOf(name, StringComparison.Ordinal) - 1); if (ns.Contains("(") && ns.Contains(")")) { var i1 = ns.IndexOf("(", StringComparison.Ordinal); var i2 = ns.IndexOf(")", StringComparison.Ordinal); ns = ns.Substring(0, i1) + ns.Substring(i2 + 1); fullName = ns + "." + name; } } var ti = new ItemInfoDto { Guid = testGuid, Start = testNode.GetAttribute("start-time", now), Finish = testNode.GetAttribute("end-time", now) }; var test = new TestRunDto { Name = name, FullName = fullName, Description = description == "null" ? "" : description, Duration = duration, TestInfo = ti, TestType = testType, Priority = priority, Categories = categories, Result = r != null ? (l != null ? $"{r}: {l}" : r) : "Unknown", Output = new SimpleItemInfoDto { Date = ti.Finish, ItemName = "Test Output" }, TestMessage = testNode.SelectSingleNode("./failure/message")?.InnerText ?? "", TestStackTrace = testNode.SelectSingleNode("./failure/stack-trace")?.InnerText ?? "", Screenshots = new List <SimpleItemInfoDto>(), TestData = testData ?? new List <TestDataDto>() }; var imageAttachments = testNode.SelectNodes(".//attachments/attachment/filePath")? .Cast <XmlNode>().Select(n => n.InnerText).Where(t => t.EndsWithImgExtension()).ToList(); var testScreenshots = new List <TestScreenshotDto>(); foreach (var imageAttachment in imageAttachments.Where(File.Exists)) { var ext = Path.GetExtension(imageAttachment); var fileInfo = new FileInfo(imageAttachment); var bytes = File.ReadAllBytes(imageAttachment); var base64 = Convert.ToBase64String(bytes); var screenInfo = new SimpleItemInfoDto { Date = fileInfo.CreationTimeUtc, ItemName = "" }; var testScreenshotDto = new TestScreenshotDto { Format = ext.Replace(".", ""), TestGuid = testGuid, TestScreenshotInfo = screenInfo, Base64Data = base64 }; testScreenshots.Add(testScreenshotDto); test.Screenshots.Add(testScreenshotDto.TestScreenshotInfo); } var ghprTestCase = new GhprTestCase { Id = id, ParentId = parentId, GhprTestRun = test, GhprTestOutput = GetTestOutput(testNode, test.TestInfo.Finish, logger), GhprTestScreenshots = testScreenshots }; return(ghprTestCase); } catch (Exception ex) { logger.Exception($"Exception in GetTestRun: {ex.Message}{Environment.NewLine}{ex.StackTrace}", ex); return(new GhprTestCase()); } }
public List <GhprTestCase> GetTestRuns() { var testRuns = new List <GhprTestCase>(); var deploymentFolder = _xml.GetNode("TestSettings")?.GetNode("Deployment")?.GetAttrVal("runDeploymentRoot"); var utrs = _xml.GetNodesList("UnitTestResult"); var uts = _xml.GetNode("TestDefinitions")?.GetNodesList("UnitTest"); if (utrs == null) { Console.WriteLine("No tests found!"); return(testRuns); } foreach (var utr in utrs) { try { var executionId = utr.GetAttrVal("executionId"); var start = utr.GetDateTimeVal("startTime"); var finish = utr.GetDateTimeVal("endTime"); var duration = utr.GetAttrVal("duration"); var durationTimeSpan = new TimeSpan(0); TimeSpan.TryParse(duration, out durationTimeSpan); var internalTestGuid = utr.GetAttrVal("testId") ?? Guid.NewGuid().ToString(); var testName = utr.GetAttrVal("testName"); var ut = uts?.FirstOrDefault(node => (node.GetAttrVal("id") ?? "").Equals(internalTestGuid)); if (utr.FirstChild != null && utr.FirstChild.Name.Equals("InnerResults")) { continue; } var tm = ut?.GetNode("TestMethod"); var testDesc = ut?.GetNode("Description")?.InnerText; var testFullName = (tm?.GetAttrVal("className") ?? "").Split(',')[0] + "." + testName; var testGuid = testFullName.ToMd5HashGuid(); var testInfo = new ItemInfoDto { Start = start, Finish = finish, Guid = testGuid }; var result = utr.GetAttrVal("outcome"); var outputNode = utr.GetNode("Output"); var output = outputNode?.GetNode("StdOut")?.InnerText ?? ""; var msg = outputNode?.GetNode("ErrorInfo")?.GetNode("Message")?.InnerText ?? ""; var sTrace = outputNode?.GetNode("ErrorInfo")?.GetNode("StackTrace")?.InnerText ?? ""; var testOutputInfo = new SimpleItemInfoDto { Date = finish, ItemName = "Test output" }; var testRun = new TestRunDto { TestInfo = testInfo, Name = testName, Description = testDesc, Duration = durationTimeSpan == new TimeSpan(0) ? .0 : durationTimeSpan.TotalSeconds, FullName = testFullName, Result = result, Output = testOutputInfo, TestMessage = msg, TestStackTrace = sTrace }; var testOutput = new TestOutputDto { TestOutputInfo = testOutputInfo, Output = output, SuiteOutput = "" }; var testScreenshots = new List <TestScreenshotDto>(); var resFiles = utr.GetNode("ResultFiles")?.GetNodesList("ResultFile") ?? new List <XmlNode>(); foreach (var resFile in resFiles) { var relativePath = resFile.GetAttrVal("path"); var fullResFilePath = Path.Combine( Path.GetDirectoryName(_trxFullPath), deploymentFolder, "In", executionId, relativePath); if (File.Exists(fullResFilePath)) { try { var ext = Path.GetExtension(fullResFilePath); if (new[] { "png", "jpg", "jpeg", "bmp" }.Contains(ext.Replace(".", "").ToLower())) { var fileInfo = new FileInfo(fullResFilePath); var bytes = File.ReadAllBytes(fullResFilePath); var base64 = Convert.ToBase64String(bytes); var screenInfo = new SimpleItemInfoDto { Date = fileInfo.CreationTimeUtc, ItemName = "" }; var testScreenshotDto = new TestScreenshotDto { Format = ext.Replace(".", ""), TestGuid = testGuid, TestScreenshotInfo = screenInfo, Base64Data = base64 }; testScreenshots.Add(testScreenshotDto); testRun.Screenshots.Add(screenInfo); } } catch (Exception e) { Console.WriteLine($"Error when trying to add test attachment: {e.Message}{Environment.NewLine}" + $"{e.StackTrace}{Environment.NewLine}" + $"The test XML node is:{Environment.NewLine}" + $"{utr.OuterXml}" + $"The file path is:{Environment.NewLine}" + $"{fullResFilePath}"); } } } var ghprTestCase = new GhprTestCase { Id = testGuid.ToString(), ParentId = "", GhprTestRun = testRun, GhprTestOutput = testOutput, GhprTestScreenshots = testScreenshots }; testRuns.Add(ghprTestCase); } catch (Exception e) { Console.WriteLine($"Error when trying to parse the test: {e.Message}{Environment.NewLine}" + $"{e.StackTrace}{Environment.NewLine}" + $"The test XML node is:{Environment.NewLine}" + $"{utr.OuterXml}"); } } return(testRuns); }