Skip to content

Commit

Permalink
Merge pull request #2 from DFE-Digital/CL/IntegrationTestProject
Browse files Browse the repository at this point in the history
Cl/integration test project
  • Loading branch information
spanersoraferty authored Jun 25, 2024
2 parents 756ff1b + 1a1c35a commit 2fd4b40
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 13 deletions.
36 changes: 36 additions & 0 deletions DfE.Data.SearchPrototype.Test/DfE.Data.SearchPrototype.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.1.2" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.6" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="WireMock.Net" Version="1.5.58" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\dfe.data.SearchPrototype.web\DfE.Data.SearchPrototype.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions DfE.Data.SearchPrototype.Test/HomePageTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using DfE.Data.SearchPrototype.Test.Shared;
using Microsoft.AspNetCore.Mvc.Testing;

namespace DfE.Data.SearchPrototype.Test;

public class HomePageTests : PageTestHelper
{
public HomePageTests(WebApplicationFactory<Program> factory) : base(factory)
{
}

[Fact]
public async Task HomePage_ContainsExpectedTitle()
{
// act
var response = await NavigateToPage("");

// assert
var headings = response.GetElementsByTagName("h1");
Assert.Equal("Welcome", headings.First().InnerHtml);
}

[Fact]
public async Task HomePage_ContainsPrivacyLink()
{
// act
IDocument response = await NavigateToPage("");

// Assert
IHtmlAnchorElement privacyLink = response.GetHeaderLink("Privacy");
Assert.Equal("/Home/Privacy", privacyLink.PathName);
}
}
14 changes: 14 additions & 0 deletions DfE.Data.SearchPrototype.Test/Shared/DomBrowserHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AngleSharp.Dom;
using AngleSharp.Html.Dom;

namespace DfE.Data.SearchPrototype.Test.Shared;

public static class DomBrowserHelpers
{
public static IHtmlAnchorElement GetHeaderLink(this IDocument response, string linkName)
{
var header = response.GetElementsByTagName("header").Single();
var anchorTags = header.GetElementsByTagName("a");
return (IHtmlAnchorElement)anchorTags.Single(anchorTags => anchorTags.TextContent.Contains(linkName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Mvc.Testing;

namespace DfE.Data.SearchPrototype.Test.Shared;

public class IntegrationTestingWebApplicationFactory : IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _fixture;
protected HttpClient _httpClient;

public IntegrationTestingWebApplicationFactory(WebApplicationFactory<Program> fixture)
{
_fixture = fixture;
_httpClient = _fixture.CreateClient();
}
}
27 changes: 27 additions & 0 deletions DfE.Data.SearchPrototype.Test/Shared/PageTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using AngleSharp;
using AngleSharp.Dom;
using Microsoft.AspNetCore.Mvc.Testing;

namespace DfE.Data.SearchPrototype.Test.Shared;

public class PageTestHelper : IntegrationTestingWebApplicationFactory
{
private IBrowsingContext _context;

public PageTestHelper(WebApplicationFactory<Program> factory) : base(factory)
{
// anglesharp
AngleSharp.IConfiguration angleSharpConfig = Configuration.Default;
_context = BrowsingContext.New(angleSharpConfig);
//--------------------------------------------------------------------
}

protected async Task<IDocument> NavigateToPage(string webPage)
{
HttpResponseMessage response = await _httpClient.GetAsync(webPage);

var DOM = await response.Content.ReadAsStringAsync();

return await _context.OpenAsync(req => req.Content(DOM));
}
}
4 changes: 4 additions & 0 deletions DfE.Data.SearchPrototype.Test/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Project description

These integration tests run the web app and use Anglesharp to query the DOM of the returned page for expected elements.

3 changes: 3 additions & 0 deletions DfE.Data.SearchPrototype.Test/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"shadowCopy": false
}
6 changes: 6 additions & 0 deletions dfe.data.SearchPrototype.web/DfE.Data.SearchPrototype.web.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.10.35004.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DfE.Data.SearchPrototype.Web", "DfE.Data.SearchPrototype.Web.csproj", "{7B0E447F-DC82-41DD-B5B4-B23549EF1DE8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DfE.Data.SearchPrototype.Test", "..\DfE.Data.SearchPrototype.Test\DfE.Data.SearchPrototype.Test.csproj", "{AC9A6EF4-0C0B-4EE3-A728-9D77C87B27C4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{7B0E447F-DC82-41DD-B5B4-B23549EF1DE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B0E447F-DC82-41DD-B5B4-B23549EF1DE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B0E447F-DC82-41DD-B5B4-B23549EF1DE8}.Release|Any CPU.Build.0 = Release|Any CPU
{AC9A6EF4-0C0B-4EE3-A728-9D77C87B27C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC9A6EF4-0C0B-4EE3-A728-9D77C87B27C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC9A6EF4-0C0B-4EE3-A728-9D77C87B27C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC9A6EF4-0C0B-4EE3-A728-9D77C87B27C4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 3 additions & 1 deletion dfe.data.SearchPrototype.web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
app.Run();

public partial class Program { }
16 changes: 4 additions & 12 deletions dfe.data.SearchPrototype.web/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,15 @@
</a>
</div>
<div class="govuk-header__content">
<a class="govuk-header__link govuk-header__service-name" asp-controller="Home" asp-action="Index">
Search prototype
</a>
<a class="govuk-header__link govuk-header__service-name" asp-controller="Home" asp-action="Index">Search prototype</a>
<nav aria-label="Menu" class="govuk-header__navigation">
<button type="button" class="govuk-header__menu-button govuk-js-header-toggle" aria-controls="navigation" hidden>
Menu
</button>
<button type="button" class="govuk-header__menu-button govuk-js-header-toggle" aria-controls="navigation" hidden>Menu</button>
<ul id="navigation" class="govuk-header__navigation-list">
<li class="govuk-header__navigation-item govuk-header__navigation-item--active">
<a class="govuk-header__link" asp-controller="Home" asp-action="Index">
Home
</a>
<a class="govuk-header__link" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="govuk-header__navigation-item">
<a class="govuk-header__link" asp-controller="Home" asp-action="Privacy">
Privacy
</a>
<a class="govuk-header__link" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</nav>
Expand Down

0 comments on commit 2fd4b40

Please sign in to comment.