Skip to content

Commit 1d3df22

Browse files
committed
added page models and sub-components
1 parent effc362 commit 1d3df22

File tree

13 files changed

+173
-37
lines changed

13 files changed

+173
-37
lines changed

DfE.Data.SearchPrototype.Test/HomePageTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AngleSharp.Dom;
22
using AngleSharp.Html.Dom;
3+
using DfE.Data.SearchPrototype.Test.PageModels;
34
using DfE.Data.SearchPrototype.Test.Shared;
45
using Microsoft.AspNetCore.Mvc.Testing;
56

@@ -15,18 +16,17 @@ public HomePageTests(WebApplicationFactory<Program> factory) : base(factory)
1516
public async Task HomePage_ContainsExpectedTitle()
1617
{
1718
// act
18-
var response = await NavigateToPage("");
19+
var response = await NavigateToPageAsync("");
1920

2021
// assert
21-
var headings = response.Heading();
22-
Assert.Equal("Welcome", headings.InnerHtml);
22+
Assert.Equal("Welcome", response.Heading().InnerHtml);
2323
}
2424

2525
[Fact]
26-
public async Task HomePage_ContainsPrivacyLink()
26+
public async Task HomePage_HeaderContainsPrivacyLink()
2727
{
2828
// act
29-
IDocument response = await NavigateToPage("");
29+
IDocument response = await NavigateToPageAsync("");
3030

3131
// Assert
3232
IHtmlAnchorElement privacyLink =
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using DfE.Data.SearchPrototype.Test.PageModels;
2+
using DfE.Data.SearchPrototype.Test.Shared;
3+
using Microsoft.AspNetCore.Mvc.Testing;
4+
5+
namespace DfE.Data.SearchPrototype.Test;
6+
7+
/// <summary>
8+
/// test class using the new page model way for traversing the DOM
9+
/// </summary>
10+
public class HomePageTestsUsingPageModel : PageTestHelper
11+
{
12+
public HomePageTestsUsingPageModel(WebApplicationFactory<Program> factory) : base(factory)
13+
{
14+
}
15+
16+
[Fact]
17+
public async Task HomePage_ContainsExpectedTitle()
18+
{
19+
// act
20+
var homePage = new HomePage(await NavigateToPageAsync(HomePage.Url));
21+
22+
// assert
23+
Assert.Equal("Welcome", homePage.Title);
24+
}
25+
26+
[Fact]
27+
public async Task HomePage_ContainsLinkToPrivacyPage()
28+
{
29+
// act
30+
var homePage = new HomePage(await NavigateToPageAsync(HomePage.Url));
31+
32+
var privacyPageLink = homePage.Header.PrivacyLink.Href;
33+
34+
// assert
35+
Assert.Equal(PrivacyPage.Url, homePage.Header.PrivacyLink.Href);
36+
}
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using AngleSharp.Dom;
2+
3+
namespace DfE.Data.SearchPrototype.Test.PageModels;
4+
5+
public class HomePage : Page
6+
{
7+
public static string Url = "/";
8+
9+
public HomePage(IDocument pageResponse) : base(pageResponse)
10+
{
11+
}
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using AngleSharp.Dom;
2+
3+
namespace DfE.Data.SearchPrototype.Test.PageModels;
4+
5+
public class HtmlElement
6+
{
7+
protected IElement _element;
8+
9+
public HtmlElement(IElement element)
10+
{
11+
_element = element;
12+
}
13+
14+
public string? Id { get => _element.Id; }
15+
public string? Class { get => _element.ClassName; }
16+
public string InnerHtml { get => _element.InnerHtml; }
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using AngleSharp.Dom;
2+
3+
namespace DfE.Data.SearchPrototype.Test.PageModels;
4+
5+
public class Link : HtmlElement
6+
{
7+
public Link(IElement element) : base(element)
8+
{
9+
}
10+
11+
public string? Href { get => _element.GetAttribute("href"); }
12+
public string? TextContent { get => _element.TextContent; }
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using AngleSharp.Dom;
2+
3+
namespace DfE.Data.SearchPrototype.Test.PageModels;
4+
5+
public abstract class Page
6+
{
7+
protected IDocument _DOM;
8+
9+
protected Page(IDocument pageResponse)
10+
{
11+
_DOM = pageResponse;
12+
}
13+
14+
public string Title {
15+
get => _DOM.GetElementsByTagName("h1").Single().InnerHtml;
16+
}
17+
18+
public PageHeader Header {
19+
get => new PageHeader(_DOM.GetElementsByTagName("header").Single());
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using AngleSharp.Dom;
2+
3+
namespace DfE.Data.SearchPrototype.Test.PageModels;
4+
5+
public class PageHeader : HtmlElement
6+
{
7+
public PageHeader(IElement element) : base(element)
8+
{
9+
}
10+
11+
public IEnumerable<Link> Links { get => _element.GetElementsByTagName("a").Select(x => new Link(x)); }
12+
13+
public Link PrivacyLink => Links.Single(link => link.TextContent == "Privacy");
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using AngleSharp.Dom;
2+
3+
namespace DfE.Data.SearchPrototype.Test.PageModels;
4+
5+
public class PrivacyPage : Page
6+
{
7+
public static string Url = "/Home/Privacy";
8+
9+
public PrivacyPage(IDocument pageResponse) : base(pageResponse)
10+
{
11+
}
12+
}

DfE.Data.SearchPrototype.Test/Shared/PageTestHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public PageTestHelper(WebApplicationFactory<Program> factory) : base(factory)
1616
//--------------------------------------------------------------------
1717
}
1818

19-
protected async Task<IDocument> NavigateToPage(string webPage)
19+
protected async Task<IDocument> NavigateToPageAsync(string webPage)
2020
{
2121
HttpResponseMessage response = await _httpClient.GetAsync(webPage);
2222

SpecFlow.Specs/Features/HomePageTests.feature

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ Just some basic checks that the expected things appear on the page
44

55
@tag1
66
Scenario: Home page renders as expected
7-
When I navigate to "/"
7+
When I navigate to the home page
88
Then The page heading is "Welcome"
9+
10+
#Scenario: Home page has link to Privacy page
11+
# Given I am on the home page
12+
# When I click on the Privacy link in the header
13+
# Then I am taken to the privacy page

SpecFlow.Specs/Features/HomePageTests.feature.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SpecFlow.Specs/StepDefinitions/HomePageStepDefinitions.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using AngleSharp.Dom;
2+
using DfE.Data.SearchPrototype.Test.Shared;
3+
using Microsoft.AspNetCore.Mvc.Testing;
4+
5+
namespace SpecFlow.Specs.StepDefinitions;
6+
7+
[Binding]
8+
public class PageStepDefinitions : PageTestHelper
9+
{
10+
private IDocument _response { get; set; } = null!;
11+
12+
private Dictionary<string, string> _pageNameToUrlConverter = new Dictionary<string, string>()
13+
{
14+
{ "home", "/" },
15+
{ "privacy", "/Home/Privacy" }
16+
};
17+
18+
public PageStepDefinitions(WebApplicationFactory<Program> factory) : base(factory)
19+
{
20+
}
21+
22+
[StepDefinition(@"I (navigate to| am on) the (home| privacy) page")]
23+
public async Task NavigateToThePage(string action, string pageName)
24+
{
25+
_response = await NavigateToPageAsync(_pageNameToUrlConverter[pageName]);
26+
}
27+
28+
[Then(@"The page heading is ""(.*)""")]
29+
public void PageHeadingIsExpected(string headingText)
30+
{
31+
// assert
32+
Assert.Equal(headingText, _response.Heading().InnerHtml);
33+
}
34+
}

0 commit comments

Comments
 (0)