Skip to content

I.selected()

Asserts that a dropdown option, checkbox, or radio button is selected.

If the matching element is not selected, the command fails the test.

Usage

javascript
I.selected(option: string)
I.selected(list: string, option: string)

Parameters

ParameterTypeDescription
liststring(optional) The group containing the option — by visible text, name attribute, CSS or XPath selector
optionstringThe option element to assert — by visible text, value, CSS or XPath selector

Examples

Simple checkbox

Here's a simple checkbox for illustration:


Demo: Simple Checkbox
UI
HTML
<!--[--><label>
  <input id="agree-checkbox" type="checkbox" name="terms" value="agree" checked=""> I agree to the terms and conditions.
</label>
<!--]-->

To assert that the checkbox is checked, use I.selected(option):

js
// using text
I.selected("I agree")

// using option value
I.selected("agree")

// using css
I.selected("#agree-checkbox")

// using xpath
I.selected("//input[@name='terms']")

Radio button group

Here's a group of radio buttons for illustration:

Door color

Frame color
Demo: Radio Button Group
UI
Door color

Frame color
HTML
<!--[--><div>
  <b>Door color</b><br>
  <label><input type="radio" name="door-color" value="r" checked="">Red</label>
  <label><input type="radio" name="door-color" value="g">Green</label>
  <label><input type="radio" name="door-color" value="b">Blue</label>
</div>
<br>
<div>
  <b>Frame color</b><br>
  <label><input type="radio" name="frame-color" value="r" checked="">Red</label>
  <label><input type="radio" name="frame-color" value="g">Green</label>
  <label><input type="radio" name="frame-color" value="b">Blue</label>
</div>
<!--]-->

To assert that the option in a specific group is selected, use I.selected(list, option):

js
// using group text
I.selected("Door color", "Red")

// using name attribute
I.selected("door-color", "Red")

Select dropdown

Here's a <select> dropdown for illustration:


Demo: Select Dropdown
UI

HTML
<!--[--><label for="country-field"><b>Country</b></label><br>
<select id="country-field" name="country">
  <option value="">Select Country</option>
  <option value="SG" selected="">Singapore</option>
  <option value="US">United States</option>
</select>
<!--]-->

To assert that an option in a <select> group is selected, use I.selected(list, option):

js
// using labels
I.selected("Country", "Singapore")

// target option - by its `value` attribute
I.selected("Country", "SG")

// target dropdown - by `name` attribute
I.selected("country", "Singapore")

// target dropdown - by CSS or XPath
I.selected("#country-field", "Singapore")
I.selected("//select[@name='country']", "Singapore")