Skip to content

Commit

Permalink
fixup! Make Playwright throw exceptions for 500s and test
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Nov 2, 2023
1 parent 6a7f17e commit 2f9d07e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
19 changes: 8 additions & 11 deletions backend/Testing/Browser/Base/PageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
private List<Exception> DeferredExceptions { get; } = new();
private List<UnexpectedResponseException> DeferredExceptions { get; } = new();

public PageTest()
{
Expand All @@ -32,16 +32,18 @@ public PageTest()
public IPageAssertions Expect(IPage page) => Assertions.Expect(page);
public IAPIResponseAssertions Expect(IAPIResponse response) => Assertions.Expect(response);
/// <summary>
/// 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.
/// </summary>
public Exception ExpectDeferredException<TException>() 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();
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions backend/Testing/Browser/SandboxPageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ await Page.RunAndWaitForResponseAsync(async () =>
{
await Page.GetByText("Goto 500 page").ClickAsync();
}, "/api/testing/test500NoException");
ExpectDeferredException<UnexpectedResponseException>();
ExpectDeferredException();
}

[Fact]
Expand All @@ -27,7 +27,7 @@ await Context.RunAndWaitForPageAsync(async () =>
{
await Page.GetByText("goto 500 new tab").ClickAsync();
});
ExpectDeferredException<UnexpectedResponseException>();
ExpectDeferredException();
}

[Fact(Skip = "Playwright doesn't catch the document load request of pages opened with Ctrl+Click")]
Expand All @@ -41,7 +41,7 @@ await Page.GetByText("Goto 500 page").ClickAsync(new()
Modifiers = new[] { KeyboardModifier.Control },
});
});
ExpectDeferredException<UnexpectedResponseException>();
ExpectDeferredException();
}

[Fact]
Expand All @@ -52,7 +52,7 @@ await Page.RunAndWaitForResponseAsync(async () =>
{
await Page.GetByText("Fetch 500").ClickAsync();
}, "/api/testing/test500NoException");
ExpectDeferredException<UnexpectedResponseException>();
ExpectDeferredException());
await Expect(Page.Locator(".modal-box.bg-error:has-text('Internal Server Error (500)')")).ToBeVisibleAsync();
}
}

0 comments on commit 2f9d07e

Please sign in to comment.