Table of Contents

Class FindElements

Namespace
CSF.Screenplay.Selenium.Questions
Assembly
CSF.Screenplay.Selenium.dll

A question which searches for HTML elements that matche some criteria, optionally within a specified target, returning the results as a SeleniumElementCollection.

public class FindElements : ISingleElementPerformableWithResult<SeleniumElementCollection>, ICanReportForElement
Inheritance
FindElements
Implements
Inherited Members

Examples

This example gets a SeleniumElementCollection which contains every element in the list which has the ID todo, which also the class low_priority.

using CSF.Screenplay.Selenium.Elements;
using static CSF.Screenplay.Selenium.PerformableBuilder;

readonly ITarget todoList = new CssSelector("ul#todo", "the to-do list");
readonly Locator lowPriority = new ClassName("low_priority", "the low priority items");

// Within the logic of a custom task, deriving from IPerformableWithResult<SeleniumElementCollection>
public async ValueTask<SeleniumElementCollection> PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default)
{
    // ... other performance logic
    var elements = await actor.PerformAsync(FindElementsWithin(todoList).WhichMatch(lowPriority), cancellationToken);
    // ... other performance logic
    return elements;
}

Remarks

Use this question via either of the builder methods FindElementsWithin(ITarget) or FindElementsOnThePage(). The first searches within a specified target, the second searches within the whole page <body>. This question returns a collection of elements but that collection could be empty if the search does not find any matching elements. If you are looking for a single element and a 'nothing found' result should raise an exception then consider using the FindElement question instead.

The criteria by which an element is searched by this question is a class that derives from Locator. Particularly useful are the CssSelector, ClassName and XPath locators. ElementId is less likely to be useful, as it should only ever match a single element per web page.

This class is not a complete performable, as it relies upon shared logic to retrieve the SeleniumElement which it queries. It has this in common with a number of Screenplay questions in the Selenium Plugin which observe a single element, those which derive from ISingleElementPerformableWithResult<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 SingleElementPerformableWithResultAdapter<TResult>. The adapter class provides the shared boilerplate logic which provides access to the Selenium Element. 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

FindElements(string, Locator)

Initializes a new instance of the FindElements class.

public FindElements(string elementsName = null, Locator locatorBasedMatcher = null)

Parameters

elementsName string

An optional short, descriptive, human-readable name to give to the collection of elements which are found.

locatorBasedMatcher Locator

An optional Locator which should be used to filter the elements which are returned.

Methods

GetReportFragment(Actor, Lazy<SeleniumElement>, IFormatsReportFragment)

Counterpart to GetReportFragment(Actor, IFormatsReportFragment) except that this method also offers a Selenium element.

public ReportFragment GetReportFragment(Actor actor, Lazy<SeleniumElement> element, IFormatsReportFragment formatter)

Parameters

actor Actor

An actor for whom to write the report fragment

element Lazy<SeleniumElement>

The Selenium element for which the report is being written

formatter IFormatsReportFragment

A 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<SeleniumElement>, CancellationToken)

Counterpart to PerformAsAsync(ICanPerform, CancellationToken) except that this method also offers a Selenium WebDriver and element.

public ValueTask<SeleniumElementCollection> PerformAsAsync(ICanPerform actor, IWebDriver webDriver, Lazy<SeleniumElement> element, CancellationToken cancellationToken = default)

Parameters

actor ICanPerform

The actor that is performing.

webDriver IWebDriver

The Selenium WebDriver provided from the actor's abilities.

element Lazy<SeleniumElement>

The single Selenium Element upon which this method should operate.

cancellationToken CancellationToken

An optional cancellation token by which to abort the performable.

Returns

ValueTask<SeleniumElementCollection>

A task which exposes a strongly-typed 'result' value when the performable represented by the current instance is complete.

See Also