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

Deprecate keyPress, keyDown, keyUp in favor of pressKey. #831

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions src/Driver/CoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,20 @@ public function blur($xpath)
throw new UnsupportedDriverActionException('Mouse manipulations are not supported by %s', $this);
}

/**
* {@inheritdoc}
*/
public function pressKey($xpath, $keys)
{
throw new UnsupportedDriverActionException('Keyboard manipulations are not supported by %s', $this);
}

/**
* {@inheritdoc}
*/
public function keyPress($xpath, $char, $modifier = null)
{
@trigger_error(sprintf('The method %s is deprecated as of 1.11 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

Is triggering a deprecation notice from a method, that throws an exception a good idea?

Maybe we should trigger a deprecation notice in driver sub-classes, where this method is completely replaced?


The same goes for other similar places.

throw new UnsupportedDriverActionException('Keyboard manipulations are not supported by %s', $this);
}

Expand All @@ -406,6 +415,7 @@ public function keyPress($xpath, $char, $modifier = null)
*/
public function keyDown($xpath, $char, $modifier = null)
{
@trigger_error(sprintf('The method %s is deprecated as of 1.11 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
throw new UnsupportedDriverActionException('Keyboard manipulations are not supported by %s', $this);
}

Expand All @@ -414,6 +424,7 @@ public function keyDown($xpath, $char, $modifier = null)
*/
public function keyUp($xpath, $char, $modifier = null)
{
@trigger_error(sprintf('The method %s is deprecated as of 1.11 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
throw new UnsupportedDriverActionException('Keyboard manipulations are not supported by %s', $this);
}

Expand Down
15 changes: 15 additions & 0 deletions src/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,15 @@ public function focus($xpath);
*/
public function blur($xpath);

/**
* @param string $xpath
* @param string|array|int $keys a string to type ('PHP Hypertext Preprocessor'), a integer key code (88), or an array of keys to press in sequence (e.g.: ["\u{E008}", 'H', "\u{E001}", 'ello']). See https://w3c.github.io/webdriver/#keyboard-actions
*
* @throws UnsupportedDriverActionException When operation not supported by the driver
* @throws DriverException When the operation cannot be done
*/
public function pressKey($xpath, $keys);

/**
* Presses specific keyboard key.
*
Expand All @@ -527,6 +536,8 @@ public function blur($xpath);
*
* @throws UnsupportedDriverActionException When operation not supported by the driver
* @throws DriverException When the operation cannot be done
*
* @deprecated use pressKey($xpath, $keys) instead.
*/
public function keyPress($xpath, $char, $modifier = null);

Expand All @@ -539,6 +550,8 @@ public function keyPress($xpath, $char, $modifier = null);
*
* @throws UnsupportedDriverActionException When operation not supported by the driver
* @throws DriverException When the operation cannot be done
*
* @deprecated use pressKey($xpath, $keys) instead.
*/
public function keyDown($xpath, $char, $modifier = null);

Expand All @@ -551,6 +564,8 @@ public function keyDown($xpath, $char, $modifier = null);
*
* @throws UnsupportedDriverActionException When operation not supported by the driver
* @throws DriverException When the operation cannot be done
*
* @deprecated use pressKey($xpath, $keys) instead.
*/
public function keyUp($xpath, $char, $modifier = null);

Expand Down
46 changes: 42 additions & 4 deletions src/Element/NodeElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,37 +305,52 @@ public function blur()
$this->getDriver()->blur($this->getXpath());
}

/**
* Press one or more keyboard keys on an element.
*
* @param string|int|array $keys a string to type ('PHP Hypertext Preprocessor'), a integer key code (88), or an array of keys to press in sequence (e.g.: ["\u{E008}", 'H', "\u{E001}", 'ello']). See https://w3c.github.io/webdriver/#keyboard-actions
Copy link
Member

Choose a reason for hiding this comment

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

our own API should not be based on the Webdriver representation of modifier keys, as that might not fit other drivers.

*/
public function pressKey($keys) {
Copy link
Member

Choose a reason for hiding this comment

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

Changing an interface is a BC break.

$this->getDriver()->pressKey($this->getXpath(), $keys);
}

/**
* Presses specific keyboard key.
*
* @param string|int $char could be either char ('b') or char-code (98)
* @param string|null $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
*
* @deprecated use pressKey($keys) instead.
*/
public function keyPress($char, $modifier = null)
{
$this->getDriver()->keyPress($this->getXpath(), $char, $modifier);
$this->pressKey($this->wrapCharWithModifier($char, $modifier));
Copy link
Member

Choose a reason for hiding this comment

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

Adding a @deprecated tag alone in the DocBlock won't help much, but having a matching deprecation notice triggering code will.


The same goes for other similar places in this class.

Copy link
Member

Choose a reason for hiding this comment

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

keyPress, keyDown and keyUp are not something that can be implemented on top of pressKey, which is the priammy reason to deprecate them.

}

/**
* Pressed down specific keyboard key.
*
* @param string|int $char could be either char ('b') or char-code (98)
* @param string|null $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
* @param string|null $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')'
*
* @deprecated use pressKey($keys) instead.
*/
public function keyDown($char, $modifier = null)
{
$this->getDriver()->keyDown($this->getXpath(), $char, $modifier);
$this->pressKey($this->wrapCharWithModifier($char, $modifier));
}

/**
* Pressed up specific keyboard key.
*
* @param string|int $char could be either char ('b') or char-code (98)
* @param string|null $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
*
* @deprecated use pressKey($keys) instead.
*/
public function keyUp($char, $modifier = null)
{
$this->getDriver()->keyUp($this->getXpath(), $char, $modifier);
$this->pressKey($this->wrapCharWithModifier($char, $modifier));
}

/**
Expand All @@ -347,4 +362,27 @@ public function submit()
{
$this->getDriver()->submitForm($this->getXpath());
}

/**
* Polyfill to convert from deprecated keyPress(), keyDown(), keyUp() parameters to pressKey() parameters.
*
* @param string|int $char could be either char ('b') or char-code (98)
* @param string|null $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
* @return string|int|array a $keys parameter that pressKey() can parse
*/
private function wrapCharWithModifier($char, $modifier = null)
{
switch ($modifier) {
case 'ctrl':
return ["\u{E009}", $char, "\u{E001}"];
case 'alt':
return ["\u{E00A}", $char, "\u{E001}"];
case 'shift':
return ["\u{E008}", $char, "\u{E001}"];
case 'meta':
return ["\u{E03D}", $char, "\u{E001}"];
default:
return $char;
}
}
}
4 changes: 4 additions & 0 deletions tests/Driver/CoreDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function testInterfaceMethods(\ReflectionMethod $method)
if ('setSession' === $method->getName()) {
return; // setSession is actually implemented, so we don't expect an exception here.
}
if (in_array($method->getName(), ['keyPress', 'keyDown', 'keyUp'])) {
return; // Skip deprecated methods in the driver
}


$driver = $this->getMockForAbstractClass('Behat\Mink\Driver\CoreDriver');

Expand Down
6 changes: 3 additions & 3 deletions tests/Element/NodeElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public function testKeyPress()

$this->driver
->expects($this->once())
->method('keyPress')
->method('pressKey')
->with('elem', 'key');

$node->keyPress('key');
Expand All @@ -517,7 +517,7 @@ public function testKeyDown()

$this->driver
->expects($this->once())
->method('keyDown')
->method('pressKey')
->with('elem', 'key');

$node->keyDown('key');
Expand All @@ -529,7 +529,7 @@ public function testKeyUp()

$this->driver
->expects($this->once())
->method('keyUp')
->method('pressKey')
->with('elem', 'key');

$node->keyUp('key');
Expand Down