Currently, the Selenium Driver setValue method includes the following at the bottom:
$value = strval($value);
if (in_array($elementName, array('input', 'textarea'))) {
$existingValueLength = strlen($element->attribute('value'));
// Add the TAB key to ensure we unfocus the field as browsers are triggering the change event only
// after leaving the field.
$value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value . Key::TAB;
}
$element->postValue(array('value' => array($value)));
There is no option given to exclude the tab key from the posted value, as it is presumed to be necessary to trigger the change event. In my work with Drupal 7's autocomplete field, however, the field reacts to the onChange event, and the tab will submit the value. In fields where the user must choose one of the predefined values, this causes an exception.
I'd like to be able to optionally not add the tab key to the input. Something akin to:
**
* {@inheritdoc}
*/
public function setValue($xpath, $value, $options=array()) {
$options = $options + array(
'addTab'=>TRUE
);
$element = $this->findElement($xpath);
$elementName = strtolower($element->name());
if ('select' === $elementName) {
if (is_array($value)) {
$this->deselectAllOptions($element);
foreach ($value as $option) {
$this->selectOptionOnElement($element, $option, TRUE);
}
return;
}
$this->selectOptionOnElement($element, $value);
return;
}
if ('input' === $elementName) {
$elementType = strtolower($element->attribute('type'));
if (in_array($elementType, array('submit', 'image', 'button', 'reset'))) {
throw new DriverException(sprintf('Impossible to set value an element with XPath "%s" as it is not a select, textarea or textbox', $xpath));
}
if ('checkbox' === $elementType) {
if ($element->selected() xor (bool) $value) {
$this->clickOnElement($element);
}
return;
}
if ('radio' === $elementType) {
$this->selectRadioValue($element, $value);
return;
}
if ('file' === $elementType) {
$element->postValue(array('value' => array(strval($value))));
return;
}
}
$value = strval($value);
if (in_array($elementName, array('input', 'textarea'))) {
$existingValueLength = strlen($element->attribute('value'));
// Add the TAB key to ensure we unfocus the field as browsers are triggering the change event only
// after leaving the field.
$value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value . (($options['addTab']) ? Key::TAB : '');
}
$element->postValue(array('value' => array($value)));
}
A diff of the file from the original with these changes would be:
290d289
<
636d634
< public function setValue($xpath, $value) {
637a636,639
> public function setValue($xpath, $value, $options=array()) {
> $options = $options + array(
> 'addTab'=>TRUE
> );
691c693
< $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value . Key::TAB;
---
> $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value . (($options['addTab']) ? Key::TAB : '');