Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 9688c6a

Browse files
committed
Merge pull request #82 from DavertMik/master
Keyboard
2 parents 8ccef05 + d2ecdde commit 9688c6a

File tree

6 files changed

+139
-6
lines changed

6 files changed

+139
-6
lines changed

lib/WebDriverKeyboard.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
interface WebDriverKeyboard {
17+
18+
public function sendKeys($keys);
19+
20+
/**
21+
* Press a modifier key
22+
*
23+
* @see WebDriverKeys
24+
* @param $key
25+
* @return $this
26+
*/
27+
public function pressKey($key);
28+
29+
/**
30+
* Release a modifier key
31+
*
32+
* @see WebDriverKeys
33+
* @param $key
34+
* @return $this
35+
*/
36+
public function releaseKey($key);
37+
38+
}

lib/WebDriverKeys.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,16 @@ class WebDriverKeys {
8383
const META = "\xEE\x80\xBD";
8484
const COMMAND = "\xEE\x80\xBD"; // ALIAS
8585
const ZENKAKU_HANKAKU = "\xEE\x80\xC0";
86+
87+
static function encode(array $keys)
88+
{
89+
$encoded = array();
90+
foreach ($keys as $key)
91+
{
92+
if (is_array($key)) $key = implode('',$key).self::NULL;
93+
$encoded[] = (string)$key;
94+
}
95+
return $encoded;
96+
}
97+
8698
}

lib/__init__.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
require_once('WebDriverKeys.php');
3131
require_once('WebDriverNavigation.php');
3232
require_once('WebDriverMouse.php');
33+
require_once('WebDriverKeyboard.php');
3334
require_once('WebDriverOptions.php');
3435
require_once('WebDriverPoint.php');
3536
require_once('WebDriverSelect.php');
@@ -50,6 +51,7 @@
5051
require_once('interactions/internal/WebDriverMoveToOffsetAction.php');
5152
require_once('internal/WebDriverLocatable.php');
5253
require_once('remote/RemoteMouse.php');
54+
require_once('remote/RemoteKeyboard.php');
5355
require_once('remote/RemoteWebDriver.php');
5456
require_once('remote/RemoteWebElement.php');
5557
require_once('remote/WebDriverBrowserType.php');

lib/remote/RemoteKeyboard.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* Execute keyboard commands for RemoteWebDriver.
18+
*/
19+
class RemoteKeyboard implements WebDriverKeyboard {
20+
21+
private $executor;
22+
23+
24+
public function __construct($executor) {
25+
$this->executor = $executor;
26+
}
27+
28+
/**
29+
* Send keys to active element
30+
*
31+
* @param $keys
32+
* @return $this
33+
*/
34+
public function sendKeys($keys) {
35+
$this->sendKeysToActiveElement(WebDriverKeys::encode(func_get_args()));
36+
return $this;
37+
}
38+
39+
/**
40+
* Press a modifier key
41+
*
42+
* @see WebDriverKeys
43+
* @param $key
44+
* @return $this
45+
*/
46+
public function pressKey($key)
47+
{
48+
$this->sendKeysToActiveElement(array($key));
49+
return $this;
50+
}
51+
52+
/**
53+
* Release a modifier key
54+
*
55+
* @see WebDriverKeys
56+
* @param $key
57+
* @return $this
58+
*/
59+
public function releaseKey($key)
60+
{
61+
$this->sendKeysToActiveElement(array($key));
62+
return $this;
63+
}
64+
65+
private function sendKeysToActiveElement($value)
66+
{
67+
$params = array(
68+
'value' => $value
69+
);
70+
$this->executor->execute('sendKeys', $params);
71+
}
72+
73+
}

lib/remote/RemoteWebDriver.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,12 @@ public function getMouse() {
265265
return $this->mouse;
266266
}
267267

268-
/**
269-
* @return WebDriverKeyboard
270-
*/
271-
public function getKeyboard() {
272-
throw new UnsupportedOperationException('not yet implemented');
268+
public function getKeyboard()
269+
{
270+
if (!$this->keyboard) {
271+
$this->keyboard = new RemoteKeyboard($this->executor);
272+
}
273+
return $this->keyboard;
273274
}
274275

275276
/**

lib/remote/RemoteWebElement.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,19 @@ public function isSelected() {
247247
/**
248248
* Simulate typing into an element, which may set its value.
249249
*
250+
* ``` php
251+
* $element->sendKeys('foo'); // => 'foo'
252+
* $element->sendKeys('test', WebDriverKeys::ARROW_LEFT, 's' ); // => 'test'
253+
* $element->sendKeys(array(WebDriverKeys::CONTROL, 'a'), WebDriverKeys::SPACE); // => ' '
254+
* ```
255+
*
250256
* @param mixed $value The data to be typed.
251257
* @return WebDriverElement The current instance.
252258
*/
253259
public function sendKeys($value) {
260+
$value = WebDriverKeys::encode(func_get_args());
254261
$params = array(
255-
'value' => array((string)$value),
262+
'value' => $value,
256263
':id' => $this->id,
257264
);
258265
$this->executor->execute('sendKeysToElement', $params);

0 commit comments

Comments
 (0)