diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index 87d25065..3b9a7a2c 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -666,16 +666,7 @@ public function setValue($xpath, $value) } } - $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; - } - - $element->postValue(array('value' => array($value))); + $this->postElementValue($value, $elementName, $element); // Remove the focus from the element if the field still has focus in // order to trigger the change event. By doing this instead of simply // triggering the change event for the given xpath we ensure that the @@ -692,6 +683,25 @@ public function setValue($xpath, $value) $this->executeJsOnElement($element, $script); } + /** + * {@inheritdoc} + */ + public function sendKeys($xpath, $value) + { + $element = $this->findElement($xpath); + $elementName = strtolower($element->name()); + + if ('input' === $elementName) { + $elementType = strtolower($element->attribute('type')); + + if (in_array($elementType, array('submit', 'image', 'button', 'reset', 'checkbox', 'radio', 'file'))) { + throw new DriverException(sprintf('Impossible to send keys on element with XPath "%s" as it is not a textbox', $xpath)); + } + } + + $this->postElementValue($value, $elementName, $element); + } + /** * {@inheritdoc} */ @@ -1211,4 +1221,21 @@ private function uploadFile($path) return $remotePath; } + /** + * @param $value + * @param $elementName + * @param $element + */ + private function postElementValue($value, $elementName, $element) + { + $value = strval($value); + + if (in_array($elementName, array('input', 'textarea'))) { + $existingValueLength = strlen($element->attribute('value')); + $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value; + } + + $element->postValue(array('value' => array($value))); + } + }