-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor common Blazor acceptance test code (#2074)
# Pull Request ## π€¨ Rationale Part of #1976 ## π©βπ» Implementation I've created a new project in `blazor-workspace` to contain code shared between the Nimble and Spright acceptance test projects. I ran into issues with the page components not being found when running tests from the VS Test Explorer. Through trial and error, I discovered that the `Setup` instance needed to be created in the same assembly as the page components for them to be discovered. Normally this is governed by the `AppAssembly` property of the `Router` component (in `App.razor`), but that is not used by the test runner. ## π§ͺ Testing Tests pass. ## β Checklist - [x] I have updated the project documentation to reflect my changes or determined no changes are needed. --------- Co-authored-by: Milan Raj <rajsite@users.noreply.github.com>
- Loading branch information
Showing
46 changed files
with
1,364 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/blazor-workspace/Tests/BlazorWorkspace.Testing.Acceptance/AcceptanceTestsBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
ο»Ώusing Microsoft.Playwright; | ||
using Xunit; | ||
|
||
namespace BlazorWorkspace.Testing.Acceptance; | ||
|
||
[Collection(nameof(PlaywrightFixture))] | ||
public abstract class AcceptanceTestsBase | ||
{ | ||
private readonly PlaywrightFixture _playwrightFixture; | ||
|
||
protected abstract Uri ServerAddress { get; } | ||
protected abstract string ComponentLibraryInitializationTestJavaScript { get; } | ||
|
||
protected AcceptanceTestsBase(PlaywrightFixture playwrightFixture) | ||
{ | ||
_playwrightFixture = playwrightFixture; | ||
} | ||
|
||
protected async Task<AsyncDisposablePage> NewPageForRouteAsync(string route) | ||
{ | ||
var page = await _playwrightFixture.BrowserContext!.NewPageAsync(); | ||
await NavigateToPageAsync(page, route); | ||
await WaitForComponentsInitializationAsync(page); | ||
return new AsyncDisposablePage(page); | ||
} | ||
|
||
private async Task NavigateToPageAsync(IPage page, string route) | ||
{ | ||
var address = new Uri(ServerAddress!, route).AbsoluteUri; | ||
await page.GotoAsync(address); | ||
} | ||
|
||
private async Task WaitForComponentsInitializationAsync(IPage page) | ||
{ | ||
await page.WaitForFunctionAsync(ComponentLibraryInitializationTestJavaScript); | ||
} | ||
|
||
protected sealed class AsyncDisposablePage : IAsyncDisposable | ||
{ | ||
public IPage Page { get; private set; } | ||
|
||
public AsyncDisposablePage(IPage page) | ||
{ | ||
Page = page; | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
await Page.CloseAsync(); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...kspace/Tests/BlazorWorkspace.Testing.Acceptance/BlazorWorkspace.Testing.Acceptance.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile> | ||
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<NoWarn>CA1716</NoWarn> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<AdditionalFiles Include="..\..\CodeAnalysisDictionary.xml" Link="CodeAnalysisDictionary.xml" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.29" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="Microsoft.Playwright" Version="[1.42.0]" /> | ||
<PackageReference Include="NI.CSharp.Analyzers" Version="[2.0.21]" /> | ||
<PackageReference Include="System.ComponentModel" Version="4.3.0" /> | ||
<PackageReference Include="xunit" Version="2.8.0" /> | ||
<PackageReference Include="xunit.extensibility.execution" Version="2.8.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\NimbleBlazor\NimbleBlazor.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
3 changes: 2 additions & 1 deletion
3
....Tests.Acceptance/Shared/MainLayout.razor β ...space.Testing.Acceptance/MainLayout.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...zor.Tests.Acceptance/PlaywrightFixture.cs β ...e.Testing.Acceptance/PlaywrightFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../blazor-workspace/Tests/BlazorWorkspace.Testing.Acceptance/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"profiles": { | ||
"BlazorWorkspace.Testing.Acceptance": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "https://localhost:64440;http://localhost:64441" | ||
} | ||
} | ||
} |
9 changes: 1 addition & 8 deletions
9
.../NimbleBlazor.Tests.Acceptance/Startup.cs β ...orWorkspace.Testing.Acceptance/Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.