Skip to content

Commit 1f786d2

Browse files
committed
WIP #276 - Add test coverage
1 parent ebe571c commit 1f786d2

4 files changed

Lines changed: 146 additions & 48 deletions

File tree

CSF.Screenplay.Selenium/Builders/TextMultiQueryBuilder.cs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -53,52 +53,4 @@ public TextMultiQueryBuilder(ITarget elements)
5353
this.elements = elements ?? throw new System.ArgumentNullException(nameof(elements));
5454
}
5555
}
56-
57-
/// <summary>
58-
/// Builder type for a performable that gets a single text value.
59-
/// </summary>
60-
/// <remarks>
61-
/// <para>
62-
/// The purpose of this builder is to enable or disable the trimming of whitespace characters at the beginning or end of the returned text.
63-
/// </para>
64-
/// </remarks>
65-
public class TextQueryBuilder : IGetsPerformableWithResult<string>
66-
{
67-
readonly ITarget element;
68-
bool trimWhitespace = true;
69-
70-
/// <summary>
71-
/// Configures the performable to disable the trimming of whitespace characters at the beginning or end of the returned text.
72-
/// </summary>
73-
/// <remarks>
74-
/// <para>
75-
/// Trimming of whitespace at the beginning/end of text is enabled by default. This is because some browsers (Safari)
76-
/// include whitespace at the beginning/end of text read from the browser, which isn't visible to the end user. This is typically the
77-
/// space which is inherent in the markup, but which browsers ignore when actually displaying content.
78-
/// </para>
79-
/// <para>
80-
/// Trimming it by default ensures that Screenplay reproduces functionality reliably cross-browser.
81-
/// If this causes an issue and you would like the leading/trailing whitespace included in the result then use this method.
82-
/// Note that you may see different results in browsers which include leading/trailing whitespace anyway.
83-
/// </para>
84-
/// </remarks>
85-
/// <returns>The current instance of the builder, so calls may be chained.</returns>
86-
public TextQueryBuilder WithoutTrimmingWhitespace()
87-
{
88-
trimWhitespace = false;
89-
return this;
90-
}
91-
92-
IPerformableWithResult<string> IGetsPerformableWithResult<string>.GetPerformable()
93-
=> SingleElementPerformableWithResultAdapter.From(SingleElementQuery.From(new TextQuery(trimWhitespace)), element);
94-
95-
/// <summary>
96-
/// Initializes a new instance of the <see cref="TextQueryBuilder"/> class.
97-
/// </summary>
98-
/// <param name="element">The target element to query.</param>
99-
public TextQueryBuilder(ITarget element)
100-
{
101-
this.element = element ?? throw new System.ArgumentNullException(nameof(element));
102-
}
103-
}
10456
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using CSF.Screenplay.Performables;
2+
using CSF.Screenplay.Selenium.Elements;
3+
using CSF.Screenplay.Selenium.Queries;
4+
using CSF.Screenplay.Selenium.Questions;
5+
6+
namespace CSF.Screenplay.Selenium.Builders
7+
{
8+
/// <summary>
9+
/// Builder type for a performable that gets a single text value.
10+
/// </summary>
11+
/// <remarks>
12+
/// <para>
13+
/// The purpose of this builder is to enable or disable the trimming of whitespace characters at the beginning or end of the returned text.
14+
/// </para>
15+
/// </remarks>
16+
public class TextQueryBuilder : IGetsPerformableWithResult<string>
17+
{
18+
readonly ITarget element;
19+
bool trimWhitespace = true;
20+
21+
/// <summary>
22+
/// Configures the performable to disable the trimming of whitespace characters at the beginning or end of the returned text.
23+
/// </summary>
24+
/// <remarks>
25+
/// <para>
26+
/// Trimming of whitespace at the beginning/end of text is enabled by default. This is because some browsers (Safari)
27+
/// include whitespace at the beginning/end of text read from the browser, which isn't visible to the end user. This is typically the
28+
/// space which is inherent in the markup, but which browsers ignore when actually displaying content.
29+
/// </para>
30+
/// <para>
31+
/// Trimming it by default ensures that Screenplay reproduces functionality reliably cross-browser.
32+
/// If this causes an issue and you would like the leading/trailing whitespace included in the result then use this method.
33+
/// Note that you may see different results in browsers which include leading/trailing whitespace anyway.
34+
/// </para>
35+
/// </remarks>
36+
/// <returns>The current instance of the builder, so calls may be chained.</returns>
37+
public TextQueryBuilder WithoutTrimmingWhitespace()
38+
{
39+
trimWhitespace = false;
40+
return this;
41+
}
42+
43+
IPerformableWithResult<string> IGetsPerformableWithResult<string>.GetPerformable()
44+
=> SingleElementPerformableWithResultAdapter.From(SingleElementQuery.From(new TextQuery(trimWhitespace)), element);
45+
46+
/// <summary>
47+
/// Initializes a new instance of the <see cref="TextQueryBuilder"/> class.
48+
/// </summary>
49+
/// <param name="element">The target element to query.</param>
50+
public TextQueryBuilder(ITarget element)
51+
{
52+
this.element = element ?? throw new System.ArgumentNullException(nameof(element));
53+
}
54+
}
55+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Generic;
2+
using CSF.Extensions.WebDriver;
3+
using CSF.Extensions.WebDriver.Factories;
4+
using CSF.Screenplay.Performables;
5+
using CSF.Screenplay.Selenium.Elements;
6+
using Moq;
7+
using OpenQA.Selenium;
8+
9+
namespace CSF.Screenplay.Selenium.Builders;
10+
11+
[TestFixture, Parallelizable]
12+
public class TextMultiQueryBuilderTests
13+
{
14+
[Test, AutoMoqData]
15+
public async Task WithoutTrimmingWhitespaceShouldBuildAQueryWhichDoesNotTrimWhitespace([Frozen] ITarget target,
16+
TextMultiQueryBuilder sut,
17+
Actor actor,
18+
SeleniumElement element1,
19+
SeleniumElement element2,
20+
IWebDriver driver)
21+
{
22+
actor.IsAbleTo(new BrowseTheWeb(Mock.Of<IGetsWebDriver>(f => f.GetDefaultWebDriver(null) == new WebDriverAndOptions(driver, Mock.Of<DriverOptions>()))));
23+
Mock.Get(target).Setup(x => x.GetElements(driver)).Returns(new SeleniumElementCollection([element1, element2], "the elements"));
24+
Mock.Get(element1.WebElement).SetupGet(x => x.Text).Returns(" foo bar ");
25+
Mock.Get(element2.WebElement).SetupGet(x => x.Text).Returns(" baz bob ");
26+
sut.WithoutTrimmingWhitespace();
27+
var performable = ((IGetsPerformableWithResult<IReadOnlyList<string>>) sut).GetPerformable();
28+
var result = await performable.PerformAsAsync(actor);
29+
Assert.That(result, Is.EqualTo(new [] {" foo bar ", " baz bob "}));
30+
}
31+
32+
[Test, AutoMoqData]
33+
public async Task ShouldBuildAQueryWhichTrimsWhitespaceByDefault([Frozen] ITarget target,
34+
TextMultiQueryBuilder sut,
35+
Actor actor,
36+
SeleniumElement element1,
37+
SeleniumElement element2,
38+
IWebDriver driver)
39+
{
40+
actor.IsAbleTo(new BrowseTheWeb(Mock.Of<IGetsWebDriver>(f => f.GetDefaultWebDriver(null) == new WebDriverAndOptions(driver, Mock.Of<DriverOptions>()))));
41+
Mock.Get(target).Setup(x => x.GetElements(driver)).Returns(new SeleniumElementCollection([element1, element2], "the elements"));
42+
Mock.Get(element1.WebElement).SetupGet(x => x.Text).Returns(" foo bar ");
43+
Mock.Get(element2.WebElement).SetupGet(x => x.Text).Returns(" baz bob ");
44+
var performable = ((IGetsPerformableWithResult<IReadOnlyList<string>>) sut).GetPerformable();
45+
var result = await performable.PerformAsAsync(actor);
46+
Assert.That(result, Is.EqualTo(new [] {"foo bar", "baz bob"}));
47+
}
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using CSF.Extensions.WebDriver;
2+
using CSF.Extensions.WebDriver.Factories;
3+
using CSF.Screenplay.Performables;
4+
using CSF.Screenplay.Selenium.Elements;
5+
using Moq;
6+
using OpenQA.Selenium;
7+
8+
namespace CSF.Screenplay.Selenium.Builders;
9+
10+
[TestFixture, Parallelizable]
11+
public class TextQueryBuilderTests
12+
{
13+
[Test, AutoMoqData]
14+
public async Task WithoutTrimmingWhitespaceShouldBuildAQueryWhichDoesNotTrimWhitespace([Frozen] ITarget target,
15+
TextQueryBuilder sut,
16+
Actor actor,
17+
SeleniumElement element,
18+
IWebDriver driver)
19+
{
20+
actor.IsAbleTo(new BrowseTheWeb(Mock.Of<IGetsWebDriver>(f => f.GetDefaultWebDriver(null) == new WebDriverAndOptions(driver, Mock.Of<DriverOptions>()))));
21+
Mock.Get(target).Setup(x => x.GetElement(driver)).Returns(element);
22+
Mock.Get(element.WebElement).SetupGet(x => x.Text).Returns(" foo bar ");
23+
sut.WithoutTrimmingWhitespace();
24+
var performable = ((IGetsPerformableWithResult<string>) sut).GetPerformable();
25+
var result = await performable.PerformAsAsync(actor);
26+
Assert.That(result, Is.EqualTo(" foo bar "));
27+
}
28+
29+
[Test, AutoMoqData]
30+
public async Task ShouldBuildAQueryWhichTrimsWhitespaceByDefault([Frozen] ITarget target,
31+
TextQueryBuilder sut,
32+
Actor actor,
33+
SeleniumElement element,
34+
IWebDriver driver)
35+
{
36+
actor.IsAbleTo(new BrowseTheWeb(Mock.Of<IGetsWebDriver>(f => f.GetDefaultWebDriver(null) == new WebDriverAndOptions(driver, Mock.Of<DriverOptions>()))));
37+
Mock.Get(target).Setup(x => x.GetElement(driver)).Returns(element);
38+
Mock.Get(element.WebElement).SetupGet(x => x.Text).Returns(" foo bar ");
39+
var performable = ((IGetsPerformableWithResult<string>) sut).GetPerformable();
40+
var result = await performable.PerformAsAsync(actor);
41+
Assert.That(result, Is.EqualTo("foo bar"));
42+
}
43+
}

0 commit comments

Comments
 (0)