Class SelectByIndex
- Namespace
- CSF.Screenplay.Selenium.Actions
- Assembly
- CSF.Screenplay.Selenium.dll
A partial Screenplay Action which selects an option from an HTML <select> element based
upon that option's zero-based index (position).
public class SelectByIndex : ISingleElementPerformable, ICanReportForElement
- Inheritance
-
SelectByIndex
- Implements
- Inherited Members
Examples
This example selects the second option (index 1) from the element with class optional_extras,
which is a descendent of an element with the ID confirm_purchase.
using CSF.Screenplay.Selenium.Elements;
using static CSF.Screenplay.Selenium.PerformableBuilder;
readonly ITarget optionalExtras = new CssSelector("#confirm_purchase .optional_extras", "the list of optional extras");
// 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(SelectTheOption(1).From(optionalExtras), cancellationToken);
// ... other performance logic
}
Remarks
Use this action via the builder method SelectTheOption(int). Performing this action, as an actor which has the BrowseTheWeb ability, instructs the web browser to select an option from the HTML element indicated by the target.
This action has differing behaviour depending whether the <select> element has the
multiple attribute or not.
- For an element which permits multiple-selection, this action adds the chosen option to the option(s) which are already selected.
- For an element which does not permit multiple-selection, this action replaces the option which is currently selected with the chosen option.
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
SelectByIndex(int)
Initializes a new instance of the SelectByIndex class with the specified index.
public SelectByIndex(int index)
Parameters
indexintThe zero-based index of the option to select.
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
actorActorAn actor for whom to write the report fragment
elementLazy<SeleniumElement>The Selenium element 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<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
actorICanPerformThe actor that is performing.
webDriverIWebDriverThe Selenium WebDriver provided from the actor's abilities.
elementLazy<SeleniumElement>The single Selenium Element upon which this method should operate.
cancellationTokenCancellationTokenAn 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.