diff --git a/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByIndexTests.cs b/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByIndexTests.cs index 8f4e005f..4970c944 100644 --- a/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByIndexTests.cs +++ b/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByIndexTests.cs @@ -1,6 +1,6 @@ -using System; using CSF.Screenplay.Selenium.Elements; +using static CSF.Screenplay.Selenium.Tasks.ReadTheListItemsBuilder; using static CSF.Screenplay.PerformanceStarter; using static CSF.Screenplay.Selenium.PerformableBuilder; @@ -22,8 +22,8 @@ public async Task SelectTheOptionFromShouldAddOneSelectedItem(IStage stage) await Given(webster).WasAbleTo(OpenTheUrl(testPage)); await When(webster).AttemptsTo(SelectTheOption(0).From(selectElement)); - var contents = await Then(webster).Should(ReadFromTheElement(displayText).TheText()); + var contents = await Then(webster).Should(ReadTheListItemsIn(displayText)); - Assert.That(contents, Is.EqualTo($"First{Environment.NewLine}Third")); + Assert.That(contents, Is.EqualTo(new [] { "First", "Third" })); } } diff --git a/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByTextTests.cs b/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByTextTests.cs index 3ed84074..cbe6405e 100644 --- a/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByTextTests.cs +++ b/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByTextTests.cs @@ -1,8 +1,7 @@ - -using System; using CSF.Screenplay.Selenium.Elements; using static CSF.Screenplay.PerformanceStarter; using static CSF.Screenplay.Selenium.PerformableBuilder; +using static CSF.Screenplay.Selenium.Tasks.ReadTheListItemsBuilder; namespace CSF.Screenplay.Selenium.Actions; @@ -22,8 +21,8 @@ public async Task SelectTheOptionFromShouldAddOneSelectedItem(IStage stage) await Given(webster).WasAbleTo(OpenTheUrl(testPage)); await When(webster).AttemptsTo(SelectTheOption("Second").From(selectElement)); - var contents = await Then(webster).Should(ReadFromTheElement(displayText).TheText()); + var contents = await Then(webster).Should(ReadTheListItemsIn(displayText)); - Assert.That(contents, Is.EqualTo($"Second{Environment.NewLine}Third")); + Assert.That(contents, Is.EqualTo(new [] { "Second", "Third" })); } } diff --git a/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByValueTests.cs b/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByValueTests.cs index 098a262c..eb2dfd09 100644 --- a/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByValueTests.cs +++ b/Tests/CSF.Screenplay.Selenium.Tests/Actions/SelectByValueTests.cs @@ -1,8 +1,8 @@ -using System; using CSF.Screenplay.Selenium.Elements; using static CSF.Screenplay.PerformanceStarter; using static CSF.Screenplay.Selenium.PerformableBuilder; +using static CSF.Screenplay.Selenium.Tasks.ReadTheListItemsBuilder; namespace CSF.Screenplay.Selenium.Actions; @@ -22,8 +22,8 @@ public async Task SelectTheOptionFromShouldAddOneSelectedItem(IStage stage) await Given(webster).WasAbleTo(OpenTheUrl(testPage)); await When(webster).AttemptsTo(SelectTheOptionWithValue("Two").From(selectElement)); - var contents = await Then(webster).Should(ReadFromTheElement(displayText).TheText()); + var contents = await Then(webster).Should(ReadTheListItemsIn(displayText)); - Assert.That(contents, Is.EqualTo($"Second{Environment.NewLine}Third")); + Assert.That(contents, Is.EqualTo(new [] { "Second", "Third" })); } } diff --git a/Tests/CSF.Screenplay.Selenium.Tests/Tasks/ReadTheListItems.cs b/Tests/CSF.Screenplay.Selenium.Tests/Tasks/ReadTheListItems.cs new file mode 100644 index 00000000..1b757964 --- /dev/null +++ b/Tests/CSF.Screenplay.Selenium.Tests/Tasks/ReadTheListItems.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using CSF.Screenplay.Selenium.Elements; +using static CSF.Screenplay.Selenium.PerformableBuilder; + +namespace CSF.Screenplay.Selenium.Tasks; + +public class ReadTheListItems : IPerformableWithResult>, ICanReport +{ + static readonly Locator listItems = new CssSelector("li", "the list items"); + + readonly ITarget target; + + public ReportFragment GetReportFragment(Actor actor, IFormatsReportFragment formatter) + => formatter.Format("{Actor} reads the text from list items within {Target}", actor, target); + + public async ValueTask> PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default) + { + var items = await actor.PerformAsync(FindElementsWithin(target).WhichMatch(listItems), cancellationToken); + return await actor.PerformAsync(ReadFromTheCollectionOfElements(items).Text(), cancellationToken); + } + + public ReadTheListItems(ITarget target) + { + this.target = target ?? throw new System.ArgumentNullException(nameof(target)); + } +} + +public static class ReadTheListItemsBuilder +{ + public static IPerformableWithResult> ReadTheListItemsIn(ITarget target) + => new ReadTheListItems(target); +} \ No newline at end of file