From b57c0523bb96ce840b5253147b3245ff48f07ab6 Mon Sep 17 00:00:00 2001 From: Edward Johnson Date: Sat, 11 Jun 2022 15:35:10 +0900 Subject: [PATCH 1/3] https://github.com/minkphp/MinkSelenium2Driver/pull/302 --- src/Selenium2Driver.php | 49 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index ee7f2e75..8a23e0e1 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -682,14 +682,15 @@ public function setValue($xpath, $value) } } - $value = strval($value); + # $value = strval($value); - if (in_array($elementName, array('input', 'textarea'))) { - $existingValueLength = strlen($element->attribute('value')); - $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $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))); + # $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 @@ -714,6 +715,25 @@ public function setValue($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, $elementName, $element); + } + /** * {@inheritdoc} */ @@ -1231,4 +1251,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))); + } } From f2a3755c3bba41b9df32df668744fd44e06cd753 Mon Sep 17 00:00:00 2001 From: Edward Johnson Date: Sat, 11 Jun 2022 19:32:35 +0900 Subject: [PATCH 2/3] do not overwrite existing values --- src/Selenium2Driver.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index 8a23e0e1..78b9c34d 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -1261,10 +1261,11 @@ 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; - } + // Edo: We do not want to overwrite the existing values. + // 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))); } From c99bf8e59ffbcd63f5114464e955e4311e92973c Mon Sep 17 00:00:00 2001 From: Edward Johnson Date: Sat, 11 Jun 2022 21:04:17 +0900 Subject: [PATCH 3/3] refactor so that sendKeys does not inappropriately replace the value --- src/Selenium2Driver.php | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index 78b9c34d..c0115786 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -682,15 +682,12 @@ public function setValue($xpath, $value) } } - # $value = strval($value); - - # if (in_array($elementName, array('input', 'textarea'))) { - # $existingValueLength = strlen($element->attribute('value')); - # $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value; - # } + if (in_array($elementName, array('input', 'textarea'))) { + $existingValueLength = strlen($element->attribute('value')); + $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . strval($value); + } - # $element->postValue(array('value' => array($value))); - $this->postElementValue($value, $elementName, $element); + $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 @@ -731,7 +728,7 @@ public function sendKeys($xpath, $value) } } - $this->postElementValue($value, $elementName, $element); + $this->postElementValue($value, $element); } /** @@ -1253,20 +1250,11 @@ private function uploadFile($path) } /** - * @param $value - * @param $elementName - * @param $element + * @param string $value the value of the element + * @param Element $element the webdriver element */ - private function postElementValue($value, $elementName, $element) + private function postElementValue($value, $element) { - $value = strval($value); - - // Edo: We do not want to overwrite the existing values. - // 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))); } }