Table of Contents

Class SingleElementQuery<TResult>

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

A question which reads or observes the state of a single HTML element and returns the result.

public class SingleElementQuery<TResult> : ISingleElementPerformableWithResult<TResult>, ICanReportForElement

Type Parameters

TResult

The type of the result returned by the query.

Inheritance
SingleElementQuery<TResult>
Implements
Inherited Members

Examples

This example gets the background color of a list item which has the class warning.

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

readonly ITarget warningItem = new CssSelector("li.warning", "the warning item");

// Within the logic of a custom task, deriving from IPerformableWithResult<string>
public async ValueTask<string> PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default)
{
    // ... other performance logic
    var bgColor = await actor.PerformAsync(ReadFromTheElement(warningItem).TheCssProperty("background-color"), cancellationToken);
    // ... other performance logic
    return bgColor;
}

Remarks

Use this question via the builder method ReadFromTheElement(ITarget). The builder will then guide you through inspecting the state of the target. That state information will be returned as the result of this question. Crucially, to decide which piece of information to retrieve from the target, you will build and use a Query.

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

SingleElementQuery(IQuery<TResult>)

Initializes a new instance of the SingleElementQuery<TResult> class with the specified query.

public SingleElementQuery(IQuery<TResult> query)

Parameters

query IQuery<TResult>

The query to be used for retrieving the value from the Selenium element.

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<TResult> 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<TResult>

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

See Also