Skip to content

Commit

Permalink
Refactor common Blazor acceptance test code (#2074)
Browse files Browse the repository at this point in the history
# 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
m-akinc and rajsite authored May 8, 2024
1 parent 2cb7620 commit 0195da9
Show file tree
Hide file tree
Showing 46 changed files with 1,364 additions and 391 deletions.
7 changes: 7 additions & 0 deletions packages/blazor-workspace/BlazorWorkspace.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SprightBlazor.Tests", "Test
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SprightBlazor.Tests.Acceptance", "Tests\SprightBlazor.Tests.Acceptance\SprightBlazor.Tests.Acceptance.csproj", "{8E335572-CD8B-4879-8760-73416CF103B1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorWorkspace.Testing.Acceptance", "Tests\BlazorWorkspace.Testing.Acceptance\BlazorWorkspace.Testing.Acceptance.csproj", "{D94A4535-51B3-4E70-9582-3F3A44C7A798}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -73,6 +75,10 @@ Global
{8E335572-CD8B-4879-8760-73416CF103B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E335572-CD8B-4879-8760-73416CF103B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E335572-CD8B-4879-8760-73416CF103B1}.Release|Any CPU.Build.0 = Release|Any CPU
{D94A4535-51B3-4E70-9582-3F3A44C7A798}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D94A4535-51B3-4E70-9582-3F3A44C7A798}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D94A4535-51B3-4E70-9582-3F3A44C7A798}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D94A4535-51B3-4E70-9582-3F3A44C7A798}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -86,6 +92,7 @@ Global
{7C65AEA1-8CA2-48DC-81FE-CE39295BDD4B} = {E5C31FAF-7DEF-494F-A0D2-C9A4875F2132}
{CEA7A89F-CF8E-4128-927E-24CBBF2C8C63} = {E5C31FAF-7DEF-494F-A0D2-C9A4875F2132}
{8E335572-CD8B-4879-8760-73416CF103B1} = {E5C31FAF-7DEF-494F-A0D2-C9A4875F2132}
{D94A4535-51B3-4E70-9582-3F3A44C7A798} = {E5C31FAF-7DEF-494F-A0D2-C9A4875F2132}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {38E2A588-0714-41E7-9BA3-D89622560FF9}
Expand Down
9 changes: 9 additions & 0 deletions packages/blazor-workspace/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ Visual Studio Code commands are included to build and run the example projects.
- `blazor-wasm-example:build`: Build the `Demo.Client` project
- `blazor-wasm-example:watch`: Run the `Demo.Client` project in watch mode (to automatically pick up code changes)

## Creating a New Project

When creating a new project in the Blazor workspace, ensure it includes the following configuration:

- .NET version matches other projects in the workspace
- `<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>`
- `<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>`
- Package reference to `NI.CSharp.Analyzers` with same version spec as other projects

## Additional Tips

### Enabling IIS
Expand Down
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();
}
}
}
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>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Web
@using NimbleBlazor
@namespace NimbleBlazor.Tests.Acceptance.Shared
@namespace BlazorWorkspace.Testing.Acceptance
@inherits LayoutComponentBase

<PageTitle>Nimble Blazor tests</PageTitle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.JSInterop;
using NimbleBlazor;

namespace SprightBlazor.Tests.Acceptance.Shared;
namespace BlazorWorkspace.Testing.Acceptance;

/// <summary>
/// The MainLayout Component.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Playwright;
using Xunit;

namespace NimbleBlazor.Tests.Acceptance;
namespace BlazorWorkspace.Testing.Acceptance;

/// <summary>
/// Fixture to handle Playwright initialization for acceptance tests.
Expand Down
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"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
ο»Ώnamespace NimbleBlazor.Tests.Acceptance;
ο»Ώnamespace BlazorWorkspace.Testing.Acceptance;

/// <summary>
/// Web server initialization for Blazor Server
/// </summary>
public sealed class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Hosting.Server.Features;
using Xunit;

namespace SprightBlazor.Tests.Acceptance;
namespace BlazorWorkspace.Testing.Acceptance;

public abstract class WebHostServerFixture : IAsyncLifetime, IDisposable
{
Expand All @@ -20,6 +20,8 @@ public async Task InitializeAsync()
ServerAddress = new Uri(addressFeature!.Addresses.First());
}

protected abstract Startup StartupFactory(WebHostBuilderContext context);

public async Task DisposeAsync()
{
if (_host != null)
Expand All @@ -42,5 +44,14 @@ protected virtual void Dispose(bool disposing)
}
}

protected abstract IHost CreateWebHost();
private IHost CreateWebHost()
{
return new HostBuilder()
.ConfigureWebHost(webHostBuilder => webHostBuilder
.UseKestrel()
.UseStartup(StartupFactory)
.UseStaticWebAssets()
.UseUrls("http://127.0.0.1:0")) // Pick a port dynamically
.Build();
}
}
Loading

0 comments on commit 0195da9

Please sign in to comment.