From 2f9d07ed6de28f307af698ff778f89727f9d9889 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Thu, 2 Nov 2023 11:06:51 +0100 Subject: [PATCH] fixup! Make Playwright throw exceptions for 500s and test --- backend/Testing/Browser/Base/PageTest.cs | 19 ++++++++----------- backend/Testing/Browser/SandboxPageTests.cs | 8 ++++---- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/backend/Testing/Browser/Base/PageTest.cs b/backend/Testing/Browser/Base/PageTest.cs index 0f66dc83c..009a3c7b3 100644 --- a/backend/Testing/Browser/Base/PageTest.cs +++ b/backend/Testing/Browser/Base/PageTest.cs @@ -21,7 +21,7 @@ public class PageTest : IAsyncLifetime /// Exceptions that are deferred until the end of the test, because they can't /// be cleanly thrown in sub-threads. /// - private List DeferredExceptions { get; } = new(); + private List DeferredExceptions { get; } = new(); public PageTest() { @@ -32,16 +32,18 @@ public PageTest() public IPageAssertions Expect(IPage page) => Assertions.Expect(page); public IAPIResponseAssertions Expect(IAPIResponse response) => Assertions.Expect(response); /// - /// Consumes a defferred exception that was "thrown" in a sub-thread, and returns it + /// Consumes a deferred exception that was "thrown" in a sub-thread, and returns it /// or throws if no exception of the given type is found. /// - public Exception ExpectDeferredException() where TException : Exception + public UnexpectedResponseException ExpectDeferredException() { - var exception = DeferredExceptions.FirstOrDefault(e => e is TException); - exception.ShouldNotBeNull($"Expected deferred exception of type {typeof(TException).FullName} was not found."); - DeferredExceptions.Remove(exception); + DeferredExceptions.Count.ShouldBeGreaterThan(0, "Expected a deferred exception, but none were found."); + DeferredExceptions.Count.ShouldBe(1, $"Expected a deferred exception, but {DeferredExceptions.Count} were found: {new AggregateException(DeferredExceptions)}."); + var exception = DeferredExceptions.First(); + DeferredExceptions.Clear(); return exception; } + public void ExpectNoDeferredExceptions() { DeferredExceptions.ShouldBeEmpty(); @@ -67,11 +69,6 @@ await Context.Tracing.StartAsync(new() DeferredExceptions.Add(new UnexpectedResponseException(response)); } }; - - Context.Request += (_, request) => - { - Console.WriteLine(request.IsNavigationRequest); - }; } public virtual async Task DisposeAsync() diff --git a/backend/Testing/Browser/SandboxPageTests.cs b/backend/Testing/Browser/SandboxPageTests.cs index b59b44d24..3b08ac0b5 100644 --- a/backend/Testing/Browser/SandboxPageTests.cs +++ b/backend/Testing/Browser/SandboxPageTests.cs @@ -16,7 +16,7 @@ await Page.RunAndWaitForResponseAsync(async () => { await Page.GetByText("Goto 500 page").ClickAsync(); }, "/api/testing/test500NoException"); - ExpectDeferredException(); + ExpectDeferredException(); } [Fact] @@ -27,7 +27,7 @@ await Context.RunAndWaitForPageAsync(async () => { await Page.GetByText("goto 500 new tab").ClickAsync(); }); - ExpectDeferredException(); + ExpectDeferredException(); } [Fact(Skip = "Playwright doesn't catch the document load request of pages opened with Ctrl+Click")] @@ -41,7 +41,7 @@ await Page.GetByText("Goto 500 page").ClickAsync(new() Modifiers = new[] { KeyboardModifier.Control }, }); }); - ExpectDeferredException(); + ExpectDeferredException(); } [Fact] @@ -52,7 +52,7 @@ await Page.RunAndWaitForResponseAsync(async () => { await Page.GetByText("Fetch 500").ClickAsync(); }, "/api/testing/test500NoException"); - ExpectDeferredException(); + ExpectDeferredException()); await Expect(Page.Locator(".modal-box.bg-error:has-text('Internal Server Error (500)')")).ToBeVisibleAsync(); } }