CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 853
Select, checkboxes, radio buttons
Php-webdriver provides helper classes to simplify interaction with <select>
, <input type="checkbox">
and <input type="radio">
elements.
If you'd like to learn about basic form interactions first, see Example command reference.
Consider following <select>
tag, with "English" option preselected.
<select name="language">
<option value="cs">Czech</option>
<option value="de">German</option>
<option value="en_GB" selected>English (UK)</option>
<option value="fr">French</option>
</select>
Use WebDriverSelect
class to interact with the <select>
element:
// First find the <select> element
$selectElement = $driver->findElement(WebDriverBy::name('language'));
// Now pass it to WebDriverSelect constructor
$select = new WebDriverSelect($selectElement);
// Get value of first selected option:
echo $select->getFirstSelectedOption()->getAttribute('value'); // will return "en_GB"
echo $select->getFirstSelectedOption()->getText(); // will return "English"
// Get all available <options>
$options = $select->getOptions(); // Will return array of all available <option> elements
// Get all selected options - useful when dealing with multi-select
$selectedOptions = $select->getAllSelectedOptions(); // will contain array of all selected <option> elements
// Select option
$select->selectByValue('fr'); // Will select "French"
$select->selectByIndex(1); // Will select "German"
$select->selectByVisibleText('Czech'); // Will select "Czech"
$select->selectByVisiblePartialText('UK'); // Will select "English (UK)"
// When dealing with multi-select, following methods might be useful:
$select->deselectAll();
$select->deselectByValue('...');
$select->deselectByIndex(0);
$select->deselectByVisibleText('...');
$select->deselectByVisiblePartialText('...');
Consider following list of <input type="checkbox">
tag with "Third" option preselected.
<fieldset>
<legend>Checkboxes</legend>
<input type="checkbox" name="checkbox" value="first">First<br>
<input type="checkbox" name="checkbox" value="second">Second<br>
<input type="checkbox" name="checkbox" value="third" checked>Third<br>
<input type="checkbox" name="checkbox" value="fourth">Fourth<br>
<input type="checkbox" name="checkbox" value="partial">Fifth partial<br>
</fieldset>
Use WebDriverCheckboxes
class to interact with the <input type="checkbox">
element:
// First find the <input type="checkbox"> element
$checkboxesElement = $driver->findElement(WebDriverBy::xpath('//input[@type="checkbox"]'));
// Now pass it to WebDriverCheckboxes constructor
$checkboxes = new WebDriverCheckboxes($checkboxesElement);
// Get value of first selected option:
echo $checkboxes->getFirstSelectedOption()->getAttribute('value'); // will return "third"
echo $checkboxes->getFirstSelectedOption()->getText(); // will return "Third"
// Get all available checkboxes
$options = $checkboxes->getOptions(); // Will return array of all available checkbox elements
// Get all selected options - useful when dealing with multi-select
$selectedOptions = $checkboxes->getAllSelectedOptions(); // will contain array of all selected checkboxes
// Select option
$checkboxes->selectByValue('fourth'); // Will select "Fourth"
$checkboxes->selectByIndex(0); // Will select "First"
$checkboxes->selectByVisibleText('Fourth'); // Will select "Fourth"
$checkboxes->selectByVisiblePartialText('partial'); // Will select "Fifth partial"
// When dealing with multi-select, following methods might be useful:
$checkboxes->deselectAll();
$checkboxes->deselectByValue('...');
$checkboxes->deselectByIndex(0);
$checkboxes->deselectByVisibleText('...');
$checkboxes->deselectByVisiblePartialText('...');
Consider following list of <input type="radio">
tag with "Second" option preselected.
<fieldset>
<legend>Radio buttons</legend>
<input type="radio" name="radio" value="first">First<br>
<input type="radio" name="radio" value="second" checked>Second (preselected)<br>
<input type="radio" name="radio" value="third">Third<br>
<input type="radio" name="radio" value="fourth">Fourth<br>
</fieldset>
Use WebDriverRadios
class to interact with the <input type="radio">
element:
// First find the <input type="radio"> element
$radiosElement = $driver->findElement(WebDriverBy::xpath('//input[@type="radio"]'));
// Now pass it to WebDriverRadios constructor
$radios = new WebDriverRadios($radiosElement);
// Get value of first selected option:
echo $radios->getFirstSelectedOption()->getAttribute('value'); // will return "second"
echo $radios->getFirstSelectedOption()->getText(); // will return "Second (preselected)"
// Get all available radios
$options = $radios->getOptions(); // Will return array of all available radios elements
// You can also get an array of all radios selected - one as it is radio button
$selectedOptions = $radios->getAllSelectedOptions(); // will contain array of the selected radio
// Select option
$radios->selectByValue('fourth'); // Will select "Fourth"
$radios->selectByIndex(0); // Will select "First"
$radios->selectByVisibleText('Fourth'); // Will select "Fourth"
$radios->selectByVisiblePartialText('preselected'); // Will select "Second (preselected)"
Using the following functions will lead to an UnsupportedOperationException
:
$radios->deselectAll();
$radios->deselectByValue('...');
$radios->deselectByIndex(0);
$radios->deselectByVisibleText('...');
$radios->deselectByVisiblePartialText('...');