View source
<?php
namespace Zumba\Mink\Driver;
use Behat\Mink\Exception\DriverException;
trait KeyboardTrait {
protected function normalizeCharForKeyEvent($char) {
if (!is_int($char) && !is_string($char)) {
throw new DriverException("Unsupported key type, can only be integer or string");
}
if (is_string($char) && strlen($char) !== 1) {
throw new DriverException("Key can only have ONE character");
}
$key = $char;
if (is_int($char)) {
$key = chr($char);
}
return $key;
}
protected function keyEventModifierControl($modifier) {
if ($modifier === null) {
$modifier = "none";
}
if (!in_array($modifier, array(
"none",
"alt",
"ctrl",
"shift",
"meta",
))) {
throw new DriverException("Unsupported key modifier {$modifier}");
}
return $modifier;
}
public function keyDown($xpath, $char, $modifier = null) {
$element = $this
->findElement($xpath, 1);
$key = $this
->normalizeCharForKeyEvent($char);
$modifier = $this
->keyEventModifierControl($modifier);
return $this->browser
->keyEvent($element["page_id"], $element["ids"][0], "keydown", $key, $modifier);
}
public function keyPress($xpath, $char, $modifier = null) {
$element = $this
->findElement($xpath, 1);
$key = $this
->normalizeCharForKeyEvent($char);
$modifier = $this
->keyEventModifierControl($modifier);
return $this->browser
->keyEvent($element["page_id"], $element["ids"][0], "keypress", $key, $modifier);
}
public function keyUp($xpath, $char, $modifier = null) {
$this
->findElement($xpath, 1);
$element = $this
->findElement($xpath, 1);
$key = $this
->normalizeCharForKeyEvent($char);
$modifier = $this
->keyEventModifierControl($modifier);
return $this->browser
->keyEvent($element["page_id"], $element["ids"][0], "keyup", $key, $modifier);
}
}