Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sendKeys method implementing Mink DriverInterface #302

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions src/Selenium2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -692,6 +683,25 @@ public function setValue($xpath, $value)
$this->executeJsOnElement($element, $script);
}

/**
* {@inheritdoc}
*/
public function sendKeys($xpath, $value)
aik099 marked this conversation as resolved.
Show resolved Hide resolved
{
$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}
*/
Expand Down Expand Up @@ -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;
}
Comment on lines +1233 to +1236
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood @stof comment (see minkphp/Mink#767 (comment)) correctly, then this should be moved back to setValue method to allow sendKeys to send keys (as the name suggests) instead of replacing current input value with provided one (as the setValue method does).


$element->postValue(array('value' => array($value)));
}

}