diff --git a/Lombiq.Tests.UI.Samples/Lombiq.Tests.UI.Samples.csproj b/Lombiq.Tests.UI.Samples/Lombiq.Tests.UI.Samples.csproj index b1ff640c7..ba5e691f7 100644 --- a/Lombiq.Tests.UI.Samples/Lombiq.Tests.UI.Samples.csproj +++ b/Lombiq.Tests.UI.Samples/Lombiq.Tests.UI.Samples.csproj @@ -28,6 +28,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Lombiq.Tests.UI.Samples/Tests/BasicTests.cs b/Lombiq.Tests.UI.Samples/Tests/BasicTests.cs index 1acbbdf84..24ae49bd2 100644 --- a/Lombiq.Tests.UI.Samples/Tests/BasicTests.cs +++ b/Lombiq.Tests.UI.Samples/Tests/BasicTests.cs @@ -30,7 +30,7 @@ public Task AnonymousHomePageShouldExist(Browser browser) => { // Is the title correct? context - .Get(By.ClassName("navbar-brand")) + .Get(By.ClassName("navbar-brandsdfds")) .Text .ShouldBe("Lombiq's OSOCE - UI Testing"); diff --git a/Lombiq.Tests.UI/Extensions/AccessibilityCheckingUITestContextExtensions.cs b/Lombiq.Tests.UI/Extensions/AccessibilityCheckingUITestContextExtensions.cs index 80a447dea..04f7c4cfa 100644 --- a/Lombiq.Tests.UI/Extensions/AccessibilityCheckingUITestContextExtensions.cs +++ b/Lombiq.Tests.UI/Extensions/AccessibilityCheckingUITestContextExtensions.cs @@ -46,7 +46,7 @@ public static void AssertAccessibility( axeResult, Path.Combine( accessibilityConfiguration.AlwaysCreatedAccessibilityReportsDirectoryPath, - context.TestName.MakeFileSystemFriendly() + ".html")); + context.TestManifest.Name.MakeFileSystemFriendly() + ".html")); } } diff --git a/Lombiq.Tests.UI/Models/UITestManifest.cs b/Lombiq.Tests.UI/Models/UITestManifest.cs index 985c90da4..60492de05 100644 --- a/Lombiq.Tests.UI/Models/UITestManifest.cs +++ b/Lombiq.Tests.UI/Models/UITestManifest.cs @@ -1,11 +1,29 @@ using Lombiq.Tests.UI.Services; using System; +using System.Linq; +using System.Reflection; using System.Threading.Tasks; +using Xunit.Abstractions; namespace Lombiq.Tests.UI.Models; +/// +/// Provides data about the currently executing test. +/// public class UITestManifest { - public string Name { get; set; } + public ITestOutputHelper TestOutputHelper { get; } + public ITest XunitTest { get; } + public string Name => XunitTest.DisplayName; public Func TestAsync { get; set; } + + public UITestManifest(ITestOutputHelper testOutputHelper) + { + TestOutputHelper = testOutputHelper; + + XunitTest = testOutputHelper.GetType() + .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) + .FirstOrDefault(field => field.FieldType == typeof(ITest)) + ?.GetValue(testOutputHelper) as ITest; + } } diff --git a/Lombiq.Tests.UI/OrchardCoreUITestBase.cs b/Lombiq.Tests.UI/OrchardCoreUITestBase.cs index 0e4be249a..fe8519077 100644 --- a/Lombiq.Tests.UI/OrchardCoreUITestBase.cs +++ b/Lombiq.Tests.UI/OrchardCoreUITestBase.cs @@ -6,8 +6,6 @@ using Lombiq.Tests.UI.Services; using System; using System.Drawing; -using System.Linq; -using System.Reflection; using System.Threading.Tasks; using Xunit.Abstractions; @@ -240,13 +238,8 @@ protected virtual async Task ExecuteTestAsync( Func> setupOperation, Func changeConfigurationAsync) { - var testManifest = new UITestManifest + var testManifest = new UITestManifest(_testOutputHelper) { - Name = (_testOutputHelper.GetType() - .GetFields(BindingFlags.NonPublic | BindingFlags.Instance) - .FirstOrDefault(field => field.FieldType == typeof(ITest)) - ?.GetValue(_testOutputHelper) as ITest) - ?.DisplayName, TestAsync = testAsync, }; diff --git a/Lombiq.Tests.UI/Services/TeamCityMetadataReporter.cs b/Lombiq.Tests.UI/Services/TeamCityMetadataReporter.cs index 6539312fb..0f2ecbfce 100644 --- a/Lombiq.Tests.UI/Services/TeamCityMetadataReporter.cs +++ b/Lombiq.Tests.UI/Services/TeamCityMetadataReporter.cs @@ -1,3 +1,4 @@ +using Lombiq.Tests.UI.Models; using System; using System.IO; @@ -15,32 +16,31 @@ namespace Lombiq.Tests.UI.Services; /// public static class TeamCityMetadataReporter { - public static void ReportInt(string testName, string name, int number) => - ReportNumber(testName, name, number.ToTechnicalString()); + public static void ReportInt(UITestManifest uiTestManifest, string name, int number) => + ReportNumber(uiTestManifest, name, number.ToTechnicalString()); - public static void ReportNumber(string testName, string name, string number) => - Report(testName, name, "number", number); + public static void ReportNumber(UITestManifest uiTestManifest, string name, string number) => + Report(uiTestManifest, name, "number", number); - public static void ReportText(string testName, string name, string text) => - Report(testName, name, "text", text); + public static void ReportText(UITestManifest uiTestManifest, string name, string text) => + Report(uiTestManifest, name, "text", text); - public static void ReportExternalLink(string testName, string name, string url) => - Report(testName, name, "link", url); + public static void ReportExternalLink(UITestManifest uiTestManifest, string name, string url) => + Report(uiTestManifest, name, "link", url); - public static void ReportArtifactLink(string testName, string name, string artifactPath) => - Report(testName, name, "artifact", PreparePath(artifactPath)); + public static void ReportArtifactLink(UITestManifest uiTestManifest, string name, string artifactPath) => + Report(uiTestManifest, name, "artifact", PreparePath(artifactPath)); - public static void ReportImage(string testName, string name, string imageArtifactPath) => - Report(testName, name, "image", PreparePath(imageArtifactPath)); + public static void ReportImage(UITestManifest uiTestManifest, string name, string imageArtifactPath) => + Report(uiTestManifest, name, "image", PreparePath(imageArtifactPath)); - public static void ReportVideo(string testName, string name, string videoArtifactPath) => - Report(testName, name, "video", PreparePath(videoArtifactPath)); + public static void ReportVideo(UITestManifest uiTestManifest, string name, string videoArtifactPath) => + Report(uiTestManifest, name, "video", PreparePath(videoArtifactPath)); - public static void Report(string testName, string name, string type, string value) => - // Starting with a line break is sometimes necessary not to mix up these messages in the build output. - Console.WriteLine( - Environment.NewLine + - $"##teamcity[testMetadata testName='{Escape(testName)}' name='{Escape(name)}' type='{type}' value='{Escape(value)}']"); + public static void Report(UITestManifest uiTestManifest, string name, string type, string value) => + uiTestManifest.TestOutputHelper.WriteLine( + $"##teamcity[testMetadata testName='Lombiq.Tests.UI.Samples: {Escape(uiTestManifest.Name)}' " + + $"name='{Escape(name)}' type='{type}' value='{Escape(value)}']"); // TeamCity needs forward slashes to replacing backslashes if the platform uses that. private static string PreparePath(string artifactPath) => artifactPath.Replace(Path.DirectorySeparatorChar, '/'); @@ -49,7 +49,7 @@ public static void Report(string testName, string name, string type, string valu private static string Escape(string value) => value .Replace("|", "||", StringComparison.Ordinal) .Replace("'", "|'", StringComparison.Ordinal) - .Replace("\n", "n", StringComparison.Ordinal) + .Replace("\n", "|n", StringComparison.Ordinal) .Replace("\r", "|r", StringComparison.Ordinal) .Replace(@"\uNNNN", "|0xNNNN", StringComparison.Ordinal) .Replace("[", "|[", StringComparison.Ordinal) diff --git a/Lombiq.Tests.UI/Services/UITestContext.cs b/Lombiq.Tests.UI/Services/UITestContext.cs index 47398335b..767ab7dda 100644 --- a/Lombiq.Tests.UI/Services/UITestContext.cs +++ b/Lombiq.Tests.UI/Services/UITestContext.cs @@ -1,6 +1,7 @@ using Lombiq.HelpfulLibraries.OrchardCore.Mvc; using Lombiq.Tests.UI.Exceptions; using Lombiq.Tests.UI.Extensions; +using Lombiq.Tests.UI.Models; using OpenQA.Selenium; using OpenQA.Selenium.Remote; using System; @@ -16,9 +17,9 @@ public class UITestContext private readonly List _historicBrowserLog = new(); /// - /// Gets the technical name of the current test. + /// Gets data about the currently executing test. /// - public string TestName { get; } + public UITestManifest TestManifest { get; } /// /// Gets the configuration of the test execution. @@ -78,7 +79,7 @@ public class UITestContext public string TenantName { get; set; } = "Default"; public UITestContext( - string testName, + UITestManifest testManifest, OrchardCoreUITestExecutorConfiguration configuration, SqlServerRunningContext sqlServerContext, IWebApplicationInstance application, @@ -86,7 +87,7 @@ public UITestContext( SmtpServiceRunningContext smtpContext, AzureBlobStorageRunningContext blobStorageContext) { - TestName = testName; + TestManifest = testManifest; Configuration = configuration; SqlServerRunningContext = sqlServerContext; Application = application; diff --git a/Lombiq.Tests.UI/Services/UITestExecutionSession.cs b/Lombiq.Tests.UI/Services/UITestExecutionSession.cs index 06aa60ef4..a6d1f81d2 100644 --- a/Lombiq.Tests.UI/Services/UITestExecutionSession.cs +++ b/Lombiq.Tests.UI/Services/UITestExecutionSession.cs @@ -217,7 +217,7 @@ string GetScreenshotPath(int index) => if (_configuration.ReportTeamCityMetadata) { TeamCityMetadataReporter.ReportImage( - _testManifest.Name, "FailureScreenshot", GetScreenshotPath(_screenshots.Count - 1)); + _testManifest, "FailureScreenshot", GetScreenshotPath(_screenshots.Count - 1)); } } @@ -228,7 +228,7 @@ string GetScreenshotPath(int index) => if (_configuration.ReportTeamCityMetadata) { - TeamCityMetadataReporter.ReportArtifactLink(_testManifest.Name, "PageSource", htmlPath); + TeamCityMetadataReporter.ReportArtifactLink(_testManifest, "PageSource", htmlPath); } } @@ -242,7 +242,7 @@ await File.WriteAllLinesAsync( if (_configuration.ReportTeamCityMetadata) { - TeamCityMetadataReporter.ReportArtifactLink(_testManifest.Name, "BrowserLog", browserLogPath); + TeamCityMetadataReporter.ReportArtifactLink(_testManifest, "BrowserLog", browserLogPath); } } @@ -275,7 +275,7 @@ private async Task SaveTestOutputAsync(string debugInformationPath) if (_configuration.ReportTeamCityMetadata) { - TeamCityMetadataReporter.ReportArtifactLink(_testManifest.Name, "TestOutput", testOutputPath); + TeamCityMetadataReporter.ReportArtifactLink(_testManifest, "TestOutput", testOutputPath); } } } @@ -338,7 +338,7 @@ private void CaptureMarkupValidationResults(Exception ex, string debugInformatio if (_configuration.ReportTeamCityMetadata) { - TeamCityMetadataReporter.ReportArtifactLink(_testManifest.Name, "AccessibilityReport", accessbilityReportPath); + TeamCityMetadataReporter.ReportArtifactLink(_testManifest, "AccessibilityReport", accessbilityReportPath); } } @@ -353,7 +353,7 @@ private void CaptureMarkupValidationResults(Exception ex, string debugInformatio if (_configuration.ReportTeamCityMetadata) { - TeamCityMetadataReporter.ReportArtifactLink(_testManifest.Name, "HtmlValidationReport", htmlValidationReportPath); + TeamCityMetadataReporter.ReportArtifactLink(_testManifest, "HtmlValidationReport", htmlValidationReportPath); } } else @@ -566,7 +566,7 @@ Task UITestingBeforeAppStartHandlerAsync(string contentRootPath, ArgumentsBuilde _configuration); return new UITestContext( - _testManifest.Name, + _testManifest, _configuration, sqlServerContext, _applicationInstance, diff --git a/Lombiq.Tests.UI/Services/UITestExecutor.cs b/Lombiq.Tests.UI/Services/UITestExecutor.cs index 5f3be655a..77e8aaca4 100644 --- a/Lombiq.Tests.UI/Services/UITestExecutor.cs +++ b/Lombiq.Tests.UI/Services/UITestExecutor.cs @@ -69,7 +69,7 @@ private static async Task ExecuteOrchardCoreTestInnerAsync(UITestManifest testMa { if (configuration.ReportTeamCityMetadata && (passed || retryCount == configuration.MaxRetryCount)) { - TeamCityMetadataReporter.ReportInt(testManifest.Name, "TryCount", retryCount + 1); + TeamCityMetadataReporter.ReportInt(testManifest, "TryCount", retryCount + 1); } }