diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index 05d6e321..c15fec24 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -668,14 +668,12 @@ public function setValue(string $xpath, $value) throw new DriverException(sprintf('Only string values can be used for a %s element.', $elementName)); } - $value = strval($value); - if (in_array($elementName, array('input', 'textarea'))) { $existingValueLength = strlen($element->attribute('value')); - $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value; + $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . strval($value); } - $element->postValue(array('value' => array($value))); + $this->postElementValue($value, $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 @@ -700,6 +698,28 @@ public function setValue(string $xpath, $value) } } + /** + * {@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, $element); + } + + /** + * {@inheritdoc} + */ public function check(string $xpath) { $element = $this->findElement($xpath); @@ -1154,4 +1174,13 @@ private function uploadFile(string $path): string return $remotePath; } + + /** + * @param string $value the value of the element + * @param Element $element the webdriver element + */ + private function postElementValue($value, $element) + { + $element->postValue(array('value' => array($value))); + } }