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

Commit 9cb15db

Browse files
author
whhone
committed
[Composite actions for keyboard]
// 'hello' as username // '!@#$%ABCDE' as password $driver->action() ->sendKeys($username_element, 'name') ->sendKeys($password_element, array(WebDriverKeys::SHIFT, '12345abcde')) ->perform();
1 parent e2c54f8 commit 9cb15db

12 files changed

+245
-13
lines changed

lib/WebDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function executeScript($script, array $arguments = array());
110110
/**
111111
* Take a screenshot of the current page.
112112
*
113-
* @param string $save_as The path of the screenshot to be saved.
113+
* @param $save_as The path of the screenshot to be saved.
114114
* @return string The screenshot in PNG format.
115115
*/
116116
public function takeScreenshot($save_as = null);

lib/WebDriverKeys.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,25 @@ class WebDriverKeys {
8383
const META = "\xEE\x80\xBD";
8484
const COMMAND = "\xEE\x80\xBD"; // ALIAS
8585
const ZENKAKU_HANKAKU = "\xEE\x80\xC0";
86+
87+
/**
88+
* Encode input of `sendKeys()`.
89+
* @params string|array $keys
90+
*/
91+
public static function encode($keys) {
92+
if (is_string($keys)) {
93+
$keys = array($keys);
94+
}
95+
96+
$encoded = array();
97+
foreach ($keys as $key) {
98+
if (is_array($key)) {
99+
// handle modified keys
100+
$key = implode('', $key).self::NULL;
101+
}
102+
$encoded[] = (string)$key;
103+
}
104+
105+
return $encoded;
106+
}
86107
}

lib/__init__.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
require_once('WebDriver.php');
1919
require_once('WebDriverElement.php');
2020
require_once('WebDriverCommandExecutor.php');
21+
require_once('WebDriverAction.php');
22+
require_once('WebDriverEventListener.php');
23+
24+
// abstract class
25+
require_once('interactions/internal/WebDriverKeysRelatedAction.php');
26+
require_once('interactions/internal/WebDriverSingleKeyAction.php');
2127

2228
// class
23-
require_once('WebDriverAction.php');
2429
require_once('WebDriverAlert.php');
2530
require_once('WebDriverBy.php');
2631
require_once('WebDriverDimension.php');
@@ -57,9 +62,11 @@
5762
require_once('remote/WebDriverBrowserType.php');
5863
require_once('remote/WebDriverCapabilityType.php');
5964
require_once('remote/HttpCommandExecutor.php');
65+
require_once('interactions/internal/WebDriverSendKeysAction.php');
66+
require_once('interactions/internal/WebDriverKeyDownAction.php');
67+
require_once('interactions/internal/WebDriverKeyUpAction.php');
6068

61-
require_once('EventFiringWebDriver.php');
62-
require_once('EventFiringWebDriverNavigation.php');
69+
require_once('support/events/EventFiringWebDriver.php');
70+
require_once('support/events/EventFiringWebDriverNavigation.php');
6371
require_once('WebDriverDispatcher.php');
64-
require_once('WebDriverEventListener.php');
65-
require_once('EventFiringWebElement.php');
72+
require_once('support/events/EventFiringWebElement.php');

lib/interactions/WebDriverActions.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
class WebDriverActions {
2020

2121
protected $driver;
22+
protected $keyboard;
2223
protected $mouse;
2324
protected $action;
2425

25-
// TODO: add keyboard actions
26-
// protected $keyboard;
27-
2826
public function __construct(WebDriver $driver) {
2927
$this->driver = $driver;
28+
$this->keyboard = $driver->getKeyboard();
3029
$this->mouse = $driver->getMouse();
3130
$this->action = new WebDriverCompositeAction();
3231
}
@@ -137,6 +136,7 @@ public function moveToElement(WebDriverElement $element,
137136
$this->action->addAction(new WebDriverMoveToOffsetAction(
138137
$this->mouse, $element, $x_offset, $y_offset
139138
));
139+
return $this;
140140
}
141141

142142
/**
@@ -149,4 +149,45 @@ public function release(WebDriverElement $element = null) {
149149
);
150150
return $this;
151151
}
152+
153+
/**
154+
* Press a key on keyboard.
155+
* If $element is provided, focus on that element first.
156+
*
157+
* @see WebDriverKeys for special keys like CONTROL, ALT, etc.
158+
*/
159+
public function keyDown(WebDriverElement $element = null, $key) {
160+
$this->action->addAction(
161+
new WebDriverKeyDownAction($this->keyboard, $this->mouse, $element, $key)
162+
);
163+
return $this;
164+
}
165+
166+
/**
167+
* Release a key on keyboard.
168+
* If $element is provided, focus on that element first.
169+
*
170+
* @see WebDriverKeys for special keys like CONTROL, ALT, etc.
171+
*/
172+
public function keyUp(WebDriverElement $element = null, $key) {
173+
$this->action->addAction(
174+
new WebDriverKeyUpAction($this->keyboard, $this->mouse, $element, $key)
175+
);
176+
return $this;
177+
}
178+
179+
/**
180+
* Send keys by keyboard.
181+
* If $element is provided, focus on that element first.
182+
*
183+
* @see WebDriverKeys for special keys like CONTROL, ALT, etc.
184+
*/
185+
public function sendKeys(WebDriverElement $element = null, $keys) {
186+
$this->action->addAction(
187+
new WebDriverSendKeysAction(
188+
$this->keyboard, $this->mouse, $element, $keys
189+
)
190+
);
191+
return $this;
192+
}
152193
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
class WebDriverKeyDownAction
17+
extends WebDriverSingleKeyAction
18+
implements WebDriverAction {
19+
20+
public function perform() {
21+
$this->focusOnElement();
22+
$this->keyboard->pressKey($this->key);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
class WebDriverKeyUpAction
17+
extends WebDriverSingleKeyAction
18+
implements WebDriverAction {
19+
20+
public function perform() {
21+
$this->focusOnElement();
22+
$this->keyboard->releaseKey($this->key);
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
* Base class for all keyboard-related actions.
18+
*/
19+
abstract class WebDriverKeysRelatedAction {
20+
21+
protected $keyboard;
22+
protected $mouse;
23+
protected $locationProvider;
24+
25+
public function __construct(
26+
WebDriverKeyboard $keyboard,
27+
WebDriverMouse $mouse,
28+
WebDriverLocatable $location_provider = null) {
29+
$this->keyboard = $keyboard;
30+
$this->mouse = $mouse;
31+
$this->locationProvider = $location_provider;
32+
}
33+
34+
protected function focusOnElement() {
35+
if ($this->locationProvider) {
36+
$this->mouse->click($this->locationProvider->getCoordinates());
37+
}
38+
}
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
class WebDriverSendKeysAction
17+
extends WebDriverKeysRelatedAction
18+
implements WebDriverAction {
19+
20+
private $keys;
21+
22+
public function __construct(
23+
WebDriverKeyboard $keyboard,
24+
WebDriverMouse $mouse,
25+
WebDriverLocatable $location_provider = null,
26+
$keys) {
27+
parent::__construct($keyboard, $mouse, $location_provider);
28+
$this->keys = $keys;
29+
}
30+
31+
public function perform() {
32+
$this->focusOnElement();
33+
$this->keyboard->sendKeys($this->keys);
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
abstract class WebDriverSingleKeyAction
17+
extends WebDriverKeysRelatedAction
18+
implements WebDriverAction {
19+
20+
private $keys;
21+
22+
public function __construct(
23+
WebDriverKeyboard $keyboard,
24+
WebDriverMouse $mouse,
25+
WebDriverLocatable $location_provider = null,
26+
$key) {
27+
parent::__construct($keyboard, $mouse, $location_provider);
28+
$this->key = $key;
29+
}
30+
31+
abstract public function perform();
32+
}

lib/remote/RemoteKeyboard.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct($executor) {
3232
*/
3333
public function sendKeys($keys) {
3434
$this->executor->execute('sendKeys', array(
35-
'value' => array((string)$keys),
35+
'value' => WebDriverKeys::encode($keys),
3636
));
3737
return $this;
3838
}
@@ -41,7 +41,7 @@ public function sendKeys($keys) {
4141
* Press a modifier key
4242
*
4343
* @see WebDriverKeys
44-
* @param $key
44+
* @param string $key
4545
* @return $this
4646
*/
4747
public function pressKey($key) {
@@ -55,7 +55,7 @@ public function pressKey($key) {
5555
* Release a modifier key
5656
*
5757
* @see WebDriverKeys
58-
* @param $key
58+
* @param string $key
5959
* @return $this
6060
*/
6161
public function releaseKey($key) {

0 commit comments

Comments
 (0)