Class ElementCollectionQuery<TResult>
- Namespace
- CSF.Screenplay.Selenium.Questions
- Assembly
- CSF.Screenplay.Selenium.dll
A question which reads or observes the state from a collection of HTML elements and returns the results.
public class ElementCollectionQuery<TResult> : IElementCollectionPerformableWithResult<TResult>, ICanReportForElements
Type Parameters
TResultThe type of the result returned by the query.
- Inheritance
-
ElementCollectionQuery<TResult>
- Implements
- Inherited Members
Examples
This example gets the text of every list item which has the class todo.
using CSF.Screenplay.Selenium.Elements;
using static CSF.Screenplay.Selenium.PerformableBuilder;
readonly ITarget todoItems = new CssSelector("li.todo", "the todo items");
// Within the logic of a custom task, deriving from IPerformableWithResult<IReadOnlyList<string>>
public async ValueTask<IReadOnlyList<string>> PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default)
{
// ... other performance logic
var texts = await actor.PerformAsync(ReadFromTheCollectionOfElements(todoItems).Text(), cancellationToken);
// ... other performance logic
return texts;
}
Remarks
Use this question via the builder method ReadFromTheCollectionOfElements(ITarget). The builder will then guide you through inspecting the state of the targets. The corresponding state information from each of the elements which are found by the target will be returned as the result of this question. Crucially, to decide which piece of information to retrieve from the target elements, you will build and use a Query.
This class is not a complete performable, as it relies upon shared logic to retrieve the SeleniumElementCollection which it queries. It has this in common with a number of Screenplay questions in the Selenium Plugin which observe collections of elements, those which derive from IElementCollectionPerformableWithResult<TResult>. In order for this class to be used as a full-fledged performable, an instance of this type must be wrapped within an instance of ElementCollectionPerformableWithResultAdapter<TResult>. The adapter class provides the shared boilerplate logic which provides access to the Selenium Element Collection. Note that the builder method(s) which create instances of this type include the 'wrap within an adapter' logic. Normal usage of this performable, when creating it from a builder, does not need to be concerned with this factor.
Constructors
ElementCollectionQuery(IQuery<TResult>)
Initializes a new instance of the ElementCollectionQuery<TResult> class with the specified query.
public ElementCollectionQuery(IQuery<TResult> query)
Parameters
queryIQuery<TResult>The query to be used for retrieving the value from each Selenium element.
Methods
GetReportFragment(Actor, Lazy<SeleniumElementCollection>, IFormatsReportFragment)
Counterpart to GetReportFragment(Actor, IFormatsReportFragment) except that this method also offers a Selenium element collection.
public ReportFragment GetReportFragment(Actor actor, Lazy<SeleniumElementCollection> elements, IFormatsReportFragment formatter)
Parameters
actorActorAn actor for whom to write the report fragment
elementsLazy<SeleniumElementCollection>The Selenium elements for which the report is being written
formatterIFormatsReportFragmentA report-formatting service
Returns
- ReportFragment
A human-readable report fragment.
Remarks
Please see the documentation for GetReportFragment(Actor, IFormatsReportFragment) for more information.
PerformAsAsync(ICanPerform, IWebDriver, Lazy<SeleniumElementCollection>, CancellationToken)
Counterpart to PerformAsAsync(ICanPerform, CancellationToken) except that this method also offers a Selenium WebDriver and element.
public ValueTask<IReadOnlyList<TResult>> PerformAsAsync(ICanPerform actor, IWebDriver webDriver, Lazy<SeleniumElementCollection> elements, CancellationToken cancellationToken = default)
Parameters
actorICanPerformThe actor that is performing.
webDriverIWebDriverThe Selenium WebDriver provided from the actor's abilities.
elementsLazy<SeleniumElementCollection>The single Selenium Element upon which this method should operate.
cancellationTokenCancellationTokenAn optional cancellation token by which to abort the performable.
Returns
- ValueTask<IReadOnlyList<TResult>>
A task which exposes a strongly-typed 'result' value when the performable represented by the current instance is complete.