public static ITestRun Update(this ITestRun target, ITestRun run)
 {
     if (target.TestInfo.Guid.Equals(Guid.Empty))
     {
         target.TestInfo.Guid = GuidConverter.ToMd5HashGuid(target.FullName);
     }
     target.Screenshots.AddRange(run.Screenshots.Where(s => !target.Screenshots.Any(ts => ts.Name.Equals(s.Name))));
     target.Events.AddRange(run.Events.Where(e => !target.Events.Any(te => te.Name.Equals(e.Name))));
     return(target);
 }
        public void AfterTest(ITest test)
        {
            lock (_syncRoot)
            {
                _finish = DateTime.Now;
                _guid   = _guid.Equals(Guid.Empty)
                    ? (Report.TestGuid.Equals(Guid.Empty) ? GuidConverter.ToMd5HashGuid(test.FullName) : Report.TestGuid)
                    : _guid;
                _testOutput = TestContext.Out.ToString();
                _testName   = _testName.Equals("") ? Report.TestName : _testName;

                var context          = TestContext.CurrentContext;
                var relativeTestHref = "Attachments" + @"/" + _guid + @"/" + Output.Files.GetTestHtmlName(_finish);

                _test = new TestInformations
                {
                    DateTimeStart    = _start,
                    DateTimeFinish   = _finish,
                    TestDuration     = (_finish - _start).TotalSeconds,
                    FullName         = test.FullName,
                    ProjectName      = _projectName.Equals("") ? test.FullName.Split('.').First() : _projectName,
                    ClassName        = _className.Equals("") ? test.FullName.Split('.').Skip(1).First() : _className,
                    Name             = _testName.Equals("") ? test.Name : _testName,
                    TestStackTrace   = context.Result.StackTrace ?? "",
                    TestMessage      = context.Result.Message ?? "",
                    Result           = context.Result.Outcome?.ToString() ?? "Unknown",
                    Guid             = _guid,
                    HasOutput        = !_testOutput.Equals(string.Empty),
                    AttachmentsPath  = _attachmentsPath + _guid + @"\",
                    TestHrefRelative = relativeTestHref,
                    TestHrefAbsolute = _configuration.ServerLink + relativeTestHref,
                    Events           = Report.GetEvents()
                };

                TakeScreenshotIfFailed();
                AddScreenshots();
                CleanUpTestFiles();
                SaveTestFiles();
                SendEmails(_test.IsSuccess());
                SendEmailsForEvents();
                GenerateReport();
                Flush();
            }
        }
        public static void SaveScreenshot(byte[] screenBytes)
        {
            var guid     = TestContext.CurrentContext.Test.Properties.Get("TestGuid")?.ToString();
            var fullName = TestContext.CurrentContext.Test.FullName;
            var testGuid = guid != null?Guid.Parse(guid) : GuidConverter.ToMd5HashGuid(fullName);

            var fullPath       = Path.Combine(GhprEventListener.OutputPath, Paths.Folders.Tests, testGuid.ToString(), Paths.Folders.Img);
            var screenshotName = ScreenshotHelper.SaveScreenshot(fullPath, screenBytes, DateTime.Now);
            var count          = 0;
            var screenKey      = GetScreenKey(count);

            while (TestContext.CurrentContext.Test.Properties.Get(screenKey) != null)
            {
                count++;
                screenKey = GetScreenKey(count);
            }

            TestContext.CurrentContext.Test.Properties.Add(screenKey, screenshotName);
        }
Beispiel #4
0
        public static ITestRun GetTestRun(XmlNode testNode)
        {
            try
            {
                var now         = DateTime.Now;
                var guid        = testNode.SelectSingleNode("properties/property[@name='TestGuid']")?.GetAttribute("value");
                var testType    = testNode.SelectSingleNode("properties/property[@name='TestType']")?.GetAttribute("value");
                var priority    = testNode.SelectSingleNode("properties/property[@name='Priority']")?.GetAttribute("value");
                var description = testNode.SelectSingleNode("properties/property[@name='Description']")?.GetAttribute("value");
                var categories  = testNode.SelectNodes("properties/property[@name='Category']")?.Cast <XmlNode>()
                                  .Select(n => n.GetAttribute("value")).ToArray();

                var screenNames = testNode.SelectNodes(
                    $"properties/property[contains(@name,'{Paths.Names.ScreenshotKeyTemplate}')]")?
                                  .Cast <XmlNode>()
                                  .Select(n => n.GetAttribute("value")).ToArray();
                var screens = screenNames?.Select(screenName => new TestScreenshot(screenName))
                              .Cast <ITestScreenshot>().ToList();

                var testDataDateTimes = testNode.SelectNodes(
                    $"properties/property[contains(@name,'{Paths.Names.TestDataDateTimeKeyTemplate}')]")?
                                        .Cast <XmlNode>()
                                        .Select(n => n.GetAttribute("value")).ToList();
                var testDataActuals = testNode.SelectNodes(
                    $"properties/property[contains(@name,'{Paths.Names.TestDataActualKeyTemplate}')]")?
                                      .Cast <XmlNode>()
                                      .Select(n => n.GetAttribute("value")).ToArray();
                var testDataExpecteds = testNode.SelectNodes(
                    $"properties/property[contains(@name,'{Paths.Names.TestDataExpectedKeyTemplate}')]")?
                                        .Cast <XmlNode>()
                                        .Select(n => n.GetAttribute("value")).ToArray();
                var testDataComments = testNode.SelectNodes(
                    $"properties/property[contains(@name,'{Paths.Names.TestDataCommentKeyTemplate}')]")?
                                       .Cast <XmlNode>()
                                       .Select(n => n.GetAttribute("value")).ToArray();
                var testData = new List <ITestData>();
                for (var i = 0; i < testDataDateTimes?.Count; i++)
                {
                    testData.Add(new TestData
                    {
                        Date     = DateTime.ParseExact(testDataDateTimes[i], "yyyyMMdd_HHmmssfff", CultureInfo.InvariantCulture),
                        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 name     = testNode.GetAttribute("name");
                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 ItemInfo
                {
                    Guid                           = guid != null?Guid.Parse(guid) : GuidConverter.ToMd5HashGuid(fullName),
                                            Start  = testNode.GetAttribute("start-time", now),
                                            Finish = testNode.GetAttribute("end-time", now)
                };
                var test = new TestRun
                {
                    Name           = name,
                    FullName       = fullName,
                    Description    = description,
                    TestInfo       = ti,
                    TestType       = testType,
                    Priority       = priority,
                    Categories     = categories,
                    Result         = r != null ? (l != null ? $"{r}: {l}" : r) : "Unknown",
                    TestDuration   = testNode.GetAttribute("duration", 0.0),
                    Output         = testNode.SelectSingleNode(".//output")?.InnerText ?? "",
                    TestMessage    = testNode.SelectSingleNode(".//message")?.InnerText ?? "",
                    TestStackTrace = testNode.SelectSingleNode(".//stack-trace")?.InnerText ?? "",
                    Screenshots    = screens ?? new List <ITestScreenshot>(),
                    TestData       = testData ?? new List <ITestData>()
                };
                return(test);
            }
            catch (Exception ex)
            {
                var log = new Core.Utils.Log(GhprEventListener.OutputPath);
                log.Exception(ex, "Exception in GetTestRun");
                return(new TestRun());
            }
        }