Skip to content

Commit 8a360e5

Browse files
author
whhone
committed
[executeAsyncScript]
Implement executeAsyncScript in RemoteWebDriver. Add interface JavaScriptExecutor. This is done by our bootcamper, glh@fb!
1 parent 588a5fa commit 8a360e5

File tree

6 files changed

+102
-15
lines changed

6 files changed

+102
-15
lines changed

lib/JavaScriptExecutor.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?hh
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+
* WebDriver interface implemented by drivers that support JavaScript.
18+
*/
19+
interface JavaScriptExecutor {
20+
21+
/**
22+
* Inject a snippet of JavaScript into the page for execution in the context
23+
* of the currently selected frame. The executed script is assumed to be
24+
* synchronous and the result of evaluating the script will be returned.
25+
*
26+
* @param string $script The script to inject.
27+
* @param array $arguments The arguments of the script.
28+
* @return mixed The return value of the script.
29+
*/
30+
public function executeScript($script, array $arguments = array());
31+
32+
/**
33+
* Inject a snippet of JavaScript into the page for asynchronous execution in
34+
* the context of the currently selected frame.
35+
*
36+
* The driver will pass a callback as the last argument to the snippet, and
37+
* block until the callback is invoked.
38+
*
39+
* @see WebDriverExecuteAsyncScriptTestCase
40+
*
41+
* @param string $script The script to inject.
42+
* @param array $arguments The arguments of the script.
43+
* @return mixed The value passed by the script to the callback.
44+
*/
45+
public function executeAsyncScript($script, array $arguments = array());
46+
47+
}

lib/WebDriver.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,6 @@ public function getWindowHandles();
9696
*/
9797
public function quit();
9898

99-
/**
100-
* Inject a snippet of JavaScript into the page for execution in the context
101-
* of the currently selected frame. The executed script is assumed to be
102-
* synchronous and the result of evaluating the script will be returned.
103-
*
104-
* @param string $script The script to inject.
105-
* @param array $arguments The arguments of the script.
106-
* @return mixed The return value of the script.
107-
*/
108-
public function executeScript($script, array $arguments = array());
109-
11099
/**
111100
* Take a screenshot of the current page.
112101
*

lib/__init__.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
// interface
1717
require_once('WebDriverSearchContext.php');
18+
require_once('JavaScriptExecutor.php');
1819
require_once('WebDriver.php');
1920
require_once('WebDriverElement.php');
2021
require_once('WebDriverCommandExecutor.php');

lib/remote/HttpCommandExecutor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class HttpCommandExecutor implements WebDriverCommandExecutor {
3535
'elementFindElement' => array('method' => 'POST', 'url' => '/session/:sessionId/element/:id/element'),
3636
'elementFindElements' => array('method' => 'POST', 'url' => '/session/:sessionId/element/:id/elements'),
3737
'executeScript' => array('method' => 'POST', 'url' => '/session/:sessionId/execute'),
38+
'executeAsyncScript' => array('method' => 'POST', 'url' => '/session/:sessionId/execute_async'),
3839
'findElement' => array('method' => 'POST', 'url' => '/session/:sessionId/element'),
3940
'findElements' => array('method' => 'POST', 'url' => '/session/:sessionId/elements'),
4041
'focusFrame' => array('method' => 'POST', 'url' => '/session/:sessionId/frame'),

lib/remote/RemoteWebDriver.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
class RemoteWebDriver implements WebDriver {
16+
class RemoteWebDriver implements WebDriver, JavaScriptExecutor {
1717

1818
protected $executor;
1919
protected $mouse;
@@ -246,8 +246,28 @@ public function executeScript($script, array $arguments = array()) {
246246
'script' => $script,
247247
'args' => $this->prepareScriptArguments($arguments),
248248
);
249-
$response = $this->executor->execute('executeScript', $params);
250-
return $response;
249+
return $this->executor->execute('executeScript', $params);
250+
}
251+
252+
/**
253+
* Inject a snippet of JavaScript into the page for asynchronous execution in
254+
* the context of the currently selected frame.
255+
*
256+
* The driver will pass a callback as the last argument to the snippet, and
257+
* block until the callback is invoked.
258+
*
259+
* @see WebDriverExecuteAsyncScriptTestCase
260+
*
261+
* @param string $script The script to inject.
262+
* @param array $arguments The arguments of the script.
263+
* @return mixed The value passed by the script to the callback.
264+
*/
265+
public function executeAsyncScript($script, array $arguments = array()) {
266+
$params = array(
267+
'script' => $script,
268+
'args' => $this->prepareScriptArguments($arguments),
269+
);
270+
return $this->executor->execute('executeAsyncScript', $params);
251271
}
252272

253273
/**

lib/support/events/EventFiringWebDriver.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
class EventFiringWebDriver implements WebDriver {
16+
class EventFiringWebDriver implements WebDriver, JavaScriptExecutor {
1717

1818
/**
1919
* @var WebDriver
@@ -134,6 +134,12 @@ public function findElement(WebDriverBy $by) {
134134
* @throws WebDriverException
135135
*/
136136
public function executeScript($script, array $arguments = array()) {
137+
if (!$this->driver instanceof JavaScriptExecutor) {
138+
throw new UnsupportedOperationException(
139+
'driver does not implement JavaScriptExecutor',
140+
);
141+
}
142+
137143
$this->dispatch('beforeScript', $script, $this);
138144
try {
139145
$result = $this->driver->executeScript($script, $arguments);
@@ -144,6 +150,29 @@ public function executeScript($script, array $arguments = array()) {
144150
return $result;
145151
}
146152

153+
/**
154+
* @param $script
155+
* @param array $arguments
156+
* @return mixed
157+
* @throws WebDriverException
158+
*/
159+
public function executeAsyncScript($script, array $arguments = array()) {
160+
if (!$this->driver instanceof JavaScriptExecutor) {
161+
throw new UnsupportedOperationException(
162+
'driver does not implement JavaScriptExecutor',
163+
);
164+
}
165+
166+
$this->dispatch('beforeScript', $script, $this);
167+
try {
168+
$result = $this->driver->executeAsyncScript($script, $arguments);
169+
} catch (WebDriverException $exception) {
170+
$this->dispatchOnException($exception);
171+
}
172+
$this->dispatch('afterScript', $script, $this);
173+
return $result;
174+
}
175+
147176
/**
148177
* @return $this
149178
* @throws WebDriverException

0 commit comments

Comments
 (0)