Table of Contents

Class SetTheElementValue

Namespace
CSF.Screenplay.Selenium.Tasks
Assembly
CSF.Screenplay.Selenium.dll

A Screenplay task which uses JavaScript to directly set the value of an HTML element.

public class SetTheElementValue : ISingleElementPerformable, ICanReportForElement
Inheritance
SetTheElementValue
Implements
Inherited Members

Examples

This example sets the value of an element that has id impossible_input to "I worked around it!" and triggers events which simulate a user changing the value interactively.

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

readonly ITarget impossibleInput = new ElementId("impossible_input", "the input field which a WebDriver cannot reach");

// Within the logic of a custom task, deriving from IPerformable
public async ValueTask PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default)
{
    // ... other performance logic
    await actor.PerformAsync(SetTheValueOf(impossibleInput).To("I worked around it!").AsIfSetInteractively(), cancellationToken);
    // ... other performance logic
}

Remarks

Use this task via the builder method SetTheValueOf(ITarget), optionally also using the builder method AsIfSetInteractively(). When AsIfSetInteractively is used then additional JavaScript will be executed in an attempt to simulate a human user interactively setting the value.

The rationale for this task is that sometimes, due to limitations of the WebDriver implementation, it is not possible to interact with an HTML element in the same way that a human user would. An example of this is web browsers which are affected by CannotSetInputTypeDateWithSendKeys. When these limitations are encountered, the only recourse is to work around them with JavaScript.

If this task is not instructed to simulate setting the value interactively then this task does no more than use JavaScript to set the value of the element. However, if it is simulating setting the value interactively then a number of HTML/JavaScript events are manually invoked, to give UI behaviour an opportunity to respond. These events (and the order in which they are triggered) are: focus, input, change and then blur. The element value is actually updated between the focus and input events.

Use this task judiciously and sparingly. It is best to interact with the web browser/WebDriver in the same manner in which a human user would, particularly when using Screenplay/Selenium for testing. This task is provided just to work around difficulties/limitations; it is not intended to be the standard way to update elements on a web page.

This class is not a complete performable, as it relies upon shared logic to retrieve the SeleniumElement upon which it shall act. It has this in common with many Screenplay actions & tasks in the Selenium extension which operate upon a single element. These performables derive from ISingleElementPerformable. 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 SingleElementPerformableAdapter. 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

SetTheElementValue(object, bool)

Initializes a new instance of the SetTheElementValue class.

public SetTheElementValue(object value, bool simulateInteractiveSet)

Parameters

value object

The value to set on the element.

simulateInteractiveSet bool

If true then the JavaScript will fire UI events to simulate having set the element interactively

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

A task which completes when the performable represented by the current instance is complete.

See Also