Skip to content

Commit 1bb59dd

Browse files
committed
sendKeys, keyUp, keyDown
1 parent 67a51fd commit 1bb59dd

File tree

4 files changed

+93
-9
lines changed

4 files changed

+93
-9
lines changed

PHPWebDriver/WebDriverActionChains.php

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ function unwind_associated_array($arr) {
2828
}
2929
}
3030

31+
function unwind_array($arr) {
32+
if (count($arr) > 0) {
33+
$u = "array(";
34+
foreach ($arr as $a) {
35+
$u = $u . '"' . $a . '"' . ",";
36+
}
37+
$u = $u . ")";
38+
return $u;
39+
} else {
40+
return "array()";
41+
}
42+
}
43+
3144
class PHPWebDriver_WebDriverActionChains {
3245
protected $session;
3346
protected $actions = array();
@@ -39,7 +52,7 @@ public function __construct($session) {
3952

4053
public function perform() {
4154
foreach ($this->actions as $action) {
42-
// var_dump($action);
55+
var_dump($action);
4356
$before = error_get_last();
4457
$result = eval($action);
4558
$after = error_get_last();
@@ -95,9 +108,41 @@ public function dragAndDropByOffset($source, $target, $xOffset, $yOffset, $curl_
95108
return $this;
96109
}
97110

98-
public function keyDown($value, $onElement = null) {}
99-
100-
public function keyUp($value, $onElement = null) {}
111+
public function keyDown($value, $curl_opts = array()) {
112+
$typing = array();
113+
if (is_a($value, 'PHPWebDriver_WebDriverKeys')) {
114+
array_push($typing, $value->key);
115+
// }
116+
// if (is_int($value)) {
117+
// $val = strval($val);
118+
// foreach($val as $i) {
119+
// array_push($typing, $i);
120+
// }
121+
} else {
122+
array_push($typing, str_split($value));
123+
}
124+
125+
$this->actions[] = '$this->session->keys(array("value" => ' . unwind_array($typing) . '), ' . unwind_associated_array($curl_opts) . ');';
126+
return $this;
127+
}
128+
129+
public function keyUp($value, $curl_opts = array()) {
130+
$typing = array();
131+
if (is_a($value, 'PHPWebDriver_WebDriverKeys')) {
132+
array_push($typing, $value->key);
133+
// }
134+
// if (is_int($value)) {
135+
// $val = strval($val);
136+
// foreach($val as $i) {
137+
// array_push($typing, $i);
138+
// }
139+
} else {
140+
array_push($typing, str_split($value));
141+
}
142+
143+
$this->actions[] = '$this->session->keys(array("value" => ' . unwind_array($typing) . '), ' . unwind_associated_array($curl_opts) . ');';
144+
return $this;
145+
}
101146

102147
public function moveByOffset($xOffset, $yOffset, $curl_opts = array()) {
103148
$this->actions[] = '$this->session->moveto(array("xoffset" => ' . $xOffset . ', "yoffset" => ' . $yOffset . '));';
@@ -109,7 +154,7 @@ public function moveToElement($toElement, $curl_opts = array()) {
109154
return $this;
110155
}
111156

112-
public function moveToElementWithOffset($toElement, $xOffset, $yOffset) {
157+
public function moveToElementWithOffset($toElement, $xOffset, $yOffset, $curl_opts = array()) {
113158
$this->actions[] = '$this->session->moveto(array("element" => "' . $toElement->getID() . '", "xoffset" => '.$xOffset.', "yoffset" => '.$yOffset.'));';
114159
return $this;
115160
}
@@ -122,7 +167,10 @@ public function release($on_element = null, $curl_opts = array()) {
122167
return $this;
123168
}
124169

125-
public function sendKeys($keysToSend) {}
170+
public function sendKeys($keysToSend, $curl_opts = array()) {
171+
$this->actions[] = '$this->session->keys(array("value" => ' . unwind_array(str_split($keysToSend)) . '), ' . unwind_associated_array($curl_opts) . ');';
172+
return $this;
173+
}
126174

127175
public function sendKeysToElement($toElement, $keysToSend) {}
128176
}

PHPWebDriver/WebDriverBase.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ protected function curl(http://github.com/$http_method,%3C/div%3E%3C/code%3E%3C/div%3E%3C/td%3E%3C/tr%3E%3Ctr%20class=%22diff-line-row%22%3E%3Ctd%20data-grid-cell-id=%22diff-0fbc28535908588d0e48a4b17fa7080b021a0e5c8b29e5a7a14e3d93cd622f6d-147-147-0%22%20data-selected=%22false%22%20role=%22gridcell%22%20style=%22background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">147
147
if ($http_method === 'POST') {
148148
curl_setopt($curl, CURLOPT_POST, true);
149149
if ($params && is_array($params)) {
150-
// var_dump(json_encode($params));
151-
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
150+
curl_setopt($curl, CURLOPT_POSTFIELDS, str_replace('\\\\u', '\\u', json_encode($params)));
152151
} else {
153152
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-length: 0'));
154153
}

PHPWebDriver/WebDriverKeys.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,25 @@ class PHPWebDriver_WebDriverKeys {
6060
'MetaKey' => "\uE03D",
6161
);
6262

63+
function __construct($name = null) {
64+
if ($name) {
65+
$this->key = self::$keys[$name];
66+
}
67+
}
68+
69+
function __get($property) {
70+
switch($property) {
71+
case "key":
72+
return json_decode('"' . $this->key . '"');
73+
default:
74+
if (isset($property)) {
75+
return $this->$property;
76+
}
77+
}
78+
}
79+
6380
public static function __callStatic($name, $arguments) {
81+
trigger_error("Use PHPWebDriver_WebDriverKeys('KeyName')", E_USER_DEPRECATED);
6482
if (isset(self::$keys[$name])) {
6583
return json_decode('"' . self::$keys[$name] . '"');
6684
} else {

test/ActionChainsTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_once(dirname(__FILE__) . '/../PHPWebDriver/WebDriver.php');
33
require_once(dirname(__FILE__) . '/../PHPWebDriver/WebDriverActionChains.php');
44
require_once(dirname(__FILE__) . '/../PHPWebDriver/WebDriverBy.php');
5+
require_once(dirname(__FILE__) . '/../PHPWebDriver/WebDriverKeys.php');
56

67
class ProxyTest extends PHPUnit_Framework_TestCase {
78
protected static $session;
@@ -51,7 +52,6 @@ public function testRightClick() {
5152

5253
/**
5354
* @group chains
54-
* @group elephant
5555
*/
5656
public function testDragAndDropByOffset() {
5757
self::$session->open("http://jqueryui.com/droppable");
@@ -69,5 +69,24 @@ public function testDragAndDropByOffset() {
6969
$this->AssertEquals($dropped->text(), 'Dropped!');
7070
}
7171

72+
/**
73+
* @group chains
74+
*/
75+
public function testTypingWithModifiers() {
76+
self::$session->open("http://pastebin.com/");
77+
78+
$paste = self::$session->element(PHPWebDriver_WebDriverBy::ID, "paste_code");
79+
$paste->click();
80+
81+
$chain = new PHPWebDriver_WebDriverActionChains(self::$session);
82+
$chain->keyDown(new PHPWebDriver_WebDriverKeys('ShiftKey'), array(CURLOPT_VERBOSE => true));
83+
$chain->sendKeys('monkey');
84+
$chain->keyUp(new PHPWebDriver_WebDriverKeys('ShiftKey'));
85+
$chain->sendKeys(' butt!');
86+
$chain->perform();
87+
}
88+
89+
90+
7291
}
7392
?>

0 commit comments

Comments
 (0)