Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix NUnit Console.Error and friends to appear in test output #3027

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
fail-fast: false
matrix:
browser: [chromium, firefox, webkit]
os: [windows-latest, ubuntu-latest, macos-13]
os: [windows-latest, ubuntu-latest, macos-latest]
steps:
- uses: actions/checkout@v3
- name: Setup .NET Core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<ItemGroup>
<Folder Include="assets\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NUnit" Version="4.2.2" />
</ItemGroup>
<Target Name="CheckAssetsFolderExists" BeforeTargets="Build">
<Error Text="assets folder prerequisites are missing. Ensure you've ran `.\build.sh --init` from the root of the solution." Condition="!Exists('$(MSBuildProjectDirectory)\assets\empty.html')" />
</Target>
Expand Down
18 changes: 16 additions & 2 deletions src/Playwright.Tests.TestServer/SimpleServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.IO.Compression;
using System.Net;
using System.Net.WebSockets;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
Expand All @@ -41,6 +42,8 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NUnit.Framework.Internal;

namespace Microsoft.Playwright.Tests.TestServer;

Expand Down Expand Up @@ -80,6 +83,7 @@ public SimpleServer(int port, string contentRoot, bool isHttps)

EmptyPage = $"{Prefix}/empty.html";

var currentExecutionContext = TestExecutionContext.CurrentContext;
_requestWaits = new ConcurrentDictionary<string, Action<HttpContext>>();
_waitForWebSocketConnectionRequestsWaits = [];
_routes = new ConcurrentDictionary<string, Func<HttpContext, Task>>();
Expand All @@ -89,11 +93,21 @@ public SimpleServer(int port, string contentRoot, bool isHttps)
_contentRoot = contentRoot;

_webHost = new WebHostBuilder()
.ConfigureLogging(logging =>
{
// Allow seeing exceptions in the console output.
logging.AddConsole();
logging.SetMinimumLevel(LogLevel.Error);
})
.Configure((app) => app
.UseWebSockets()
.UseDeveloperExceptionPage()
.Use(middleware: async (HttpContext context, Func<Task> next) =>
{
{
// This hack allows us to have Console.WriteLine etc. appear in the test output.
var currentContext = typeof(TestExecutionContext).GetField("AsyncLocalCurrentContext", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as AsyncLocal<TestExecutionContext>;
currentContext.Value = currentExecutionContext;
}
if (context.Request.Path == "/ws")
{
if (context.WebSockets.IsWebSocketRequest)
Expand Down Expand Up @@ -236,7 +250,7 @@ private string GetContentType(string fileName)

public void SetCSP(string path, string csp) => _csp.Add(path, csp);

public Task StartAsync() => _webHost.StartAsync();
public Task StartAsync(CancellationToken cancellationToken) => _webHost.StartAsync(cancellationToken);

public Task StopAsync()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright.Tests/BaseTests/HttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static Task<HttpService> Register(WorkerAwareTest test)
Server = SimpleServer.Create(8907 + workerIndex * 2, assetDir),
HttpsServer = SimpleServer.CreateHttps(8907 + workerIndex * 2 + 1, assetDir)
};
await Task.WhenAll(http.Server.StartAsync(), http.HttpsServer.StartAsync());
await Task.WhenAll(http.Server.StartAsync(TestContext.CurrentContext.CancellationToken), http.HttpsServer.StartAsync(TestContext.CurrentContext.CancellationToken));
return http;
});
}
Expand Down
1 change: 0 additions & 1 deletion src/Playwright.Tests/BaseTests/PlaywrightAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

using System.Text.Json;
using NUnit.Framework.Constraints;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

Expand Down
3 changes: 1 addition & 2 deletions src/Playwright.Tests/BrowserContextViewportMobileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public async Task ShouldBeDetectableByModernizr()
{
await using var context = await Browser.NewContextAsync(Playwright.Devices["iPhone 6"]);
var page = await context.NewPageAsync();
await page.GotoAsync(Server.Prefix + "/detect-touch.html");
Assert.AreEqual("YES", await page.EvaluateAsync<string>("document.body.textContent.trim()"));
Assert.AreEqual(true, await page.EvaluateAsync<bool>("'ontouchstart' in window || !!window.TouchEvent"));
}

[PlaywrightTest("browsercontext-viewport-mobile.spec.ts", "should detect touch when applying viewport with touches")]
Expand Down
3 changes: 1 addition & 2 deletions src/Playwright.Tests/BrowserContextViewportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public async Task ShouldNotHaveTouchByDefault()
{
await Page.GotoAsync(Server.Prefix + "/mobile.html");
Assert.False(await Page.EvaluateAsync<bool>("'ontouchstart' in window"));
await Page.GotoAsync(Server.Prefix + "/detect-touch.html");
Assert.AreEqual("NO", await Page.EvaluateAsync<string>("document.body.textContent.trim()"));
Assert.AreEqual(false, await Page.EvaluateAsync<bool>("'ontouchstart' in window"));
}

[PlaywrightTest("browsercontext-viewport.spec.ts", "should support touch with null viewport")]
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright.Tests/BrowserTypeConnectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public async Task ShouldRecordContextTraceWithNoDirectoryName()
}

[PlaywrightTest("browsertype-connect.spec.ts", "should upload large file")]
[Timeout(TestConstants.SlowTestTimeout)]
[CancelAfter(TestConstants.SlowTestTimeout)]
public async Task ShouldUploadLargeFile()
{
var browser = await BrowserType.ConnectAsync(_remoteServer.WSEndpoint);
Expand Down
29 changes: 29 additions & 0 deletions src/Playwright.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* MIT License
*
* Copyright (c) Microsoft Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

// We should eventually migrate to the constrained assertions:
// https://docs.nunit.org/articles/nunit/release-notes/Nunit4.0-MigrationGuide.html#use-global-using-aliases
global using Assert = NUnit.Framework.Legacy.ClassicAssert;
global using CollectionAssert = NUnit.Framework.Legacy.CollectionAssert;
global using StringAssert = NUnit.Framework.Legacy.StringAssert;
2 changes: 1 addition & 1 deletion src/Playwright.Tests/PageGotoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public async Task ShouldFailWhenMainResourcesFailedToLoad()
}
else if (TestConstants.IsWebKit && TestConstants.IsWindows)
{
StringAssert.Contains("Couldn't connect to server", exception.Message);
StringAssert.Contains("Could not connect to server", exception.Message);
}
else if (TestConstants.IsWebKit)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright.Tests/PageSetInputFilesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public async Task ShouldWorkForWebkitdirectory()
}

[PlaywrightTest("page-set-input-files.spec.ts", "should upload large file")]
[Timeout(TestConstants.SlowTestTimeout)]
[CancelAfter(TestConstants.SlowTestTimeout)]
public async Task ShouldUploadLargeFile()
{
await Page.GotoAsync(Server.Prefix + "/input/fileupload.html");
Expand Down
4 changes: 2 additions & 2 deletions src/Playwright.Tests/PageWaitForNavigationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ public async Task ShouldWorkOnFrame()
}

[PlaywrightTest]
[Timeout(45_000)]
[CancelAfter(45_000)]
public async Task ShouldHaveADefaultTimeout()
{
await Page.GotoAsync(Server.Prefix + "/frames/one-frame.html");
await PlaywrightAssert.ThrowsAsync<TimeoutException>(async () => await Page.RunAndWaitForNavigationAsync(() => Task.CompletedTask));
}

[PlaywrightTest]
[Timeout(5_000)]
[CancelAfter(5_000)]
public async Task ShouldTakeTimeoutIntoAccount()
{
await Page.GotoAsync(Server.Prefix + "/frames/one-frame.html");
Expand Down
6 changes: 3 additions & 3 deletions src/Playwright.Tests/Playwright.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Playwright.Tests/ScreencastTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public async Task ShouldCaptureStaticPageInPersistentContext()


[PlaywrightTest("screencast.spec.ts", "video.path()/saveAs() does not hang immediately after launchPersistentContext and context.close()")]
[Timeout(30_000)]
[CancelAfter(30_000)]
public async Task VideoPathSaveAsDoesNotHangImmediatelyAfterLaunchPersistentContextAndContextClose()
{
using var userDirectory = new TempDirectory();
Expand Down
Loading