-
Notifications
You must be signed in to change notification settings - Fork 844
Expand file tree
/
Copy pathRemoteWebDriverTest.php
More file actions
328 lines (263 loc) · 10.1 KB
/
Copy pathRemoteWebDriverTest.php
File metadata and controls
328 lines (263 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php declare(strict_types=1);
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\HttpCommandExecutor;
use Facebook\WebDriver\Remote\RemoteWebDriver;
/**
* @coversDefaultClass \Facebook\WebDriver\Remote\RemoteWebDriver
*/
class RemoteWebDriverTest extends WebDriverTestCase
{
/**
* @covers ::getTitle
*/
public function testShouldGetPageTitle(): void
{
$this->driver->get($this->getTestPageUrl(TestPage::INDEX));
$this->assertEquals(
'php-webdriver test page',
$this->driver->getTitle()
);
}
/**
* @covers ::get
* @covers ::getCurrentURL
*/
public function testShouldGetCurrentUrl(): void
{
$this->driver->get($this->getTestPageUrl(TestPage::INDEX));
$this->assertStringEndsWith('/index.html', $this->driver->getCurrentURL());
}
/**
* @covers ::getPageSource
*/
public function testShouldGetPageSource(): void
{
$this->driver->get($this->getTestPageUrl(TestPage::INDEX));
$source = $this->driver->getPageSource();
$this->assertStringContainsString('<h1 id="welcome">', $source);
$this->assertStringContainsString('Welcome to the php-webdriver testing page.', $source);
}
/**
* @covers ::getSessionID
* @covers ::isW3cCompliant
*/
public function testShouldGetSessionId(): void
{
// This tests is intentionally included in another test, to not slow down build.
// @TODO Remove following in 2.0
if (self::isW3cProtocolBuild()) {
$this->assertTrue($this->driver->isW3cCompliant());
} else {
$this->assertFalse($this->driver->isW3cCompliant());
}
$sessionId = $this->driver->getSessionID();
$this->assertIsString($sessionId);
$this->assertNotEmpty($sessionId);
}
/**
* @group exclude-saucelabs
* @covers ::getAllSessions
*/
public function testShouldGetAllSessions(): void
{
self::skipForW3cProtocol();
$sessions = RemoteWebDriver::getAllSessions($this->serverUrl, 30000);
$this->assertIsArray($sessions);
$this->assertCount(1, $sessions);
$this->assertArrayHasKey('capabilities', $sessions[0]);
$this->assertArrayHasKey('id', $sessions[0]);
}
/**
* @group exclude-saucelabs
* @covers ::getAllSessions
* @covers ::getCommandExecutor
* @covers ::quit
*/
public function testShouldQuitAndUnsetExecutor(): void
{
self::skipForW3cProtocol();
$this->assertCount(
1,
RemoteWebDriver::getAllSessions($this->serverUrl)
);
$this->assertInstanceOf(HttpCommandExecutor::class, $this->driver->getCommandExecutor());
$this->driver->quit();
// Wait a while until chromedriver finishes deleting the session.
// https://bugs.chromium.org/p/chromedriver/issues/detail?id=3736
usleep(250000); // 250 ms
$this->assertCount(
0,
RemoteWebDriver::getAllSessions($this->serverUrl)
);
$this->assertNull($this->driver->getCommandExecutor());
}
/**
* @covers ::getWindowHandle
* @covers ::getWindowHandles
*/
public function testShouldGetWindowHandles(): void
{
$this->driver->get($this->getTestPageUrl(TestPage::OPEN_NEW_WINDOW));
$windowHandle = $this->driver->getWindowHandle();
$windowHandles = $this->driver->getWindowHandles();
$this->assertIsString($windowHandle);
$this->assertNotEmpty($windowHandle);
$this->assertSame([$windowHandle], $windowHandles);
// Open second window
$this->driver->findElement(WebDriverBy::cssSelector('a'))->click();
$this->driver->wait()->until(
WebDriverExpectedCondition::numberOfWindowsToBe(2)
);
$this->assertCount(2, $this->driver->getWindowHandles());
}
/**
* @covers ::close
*/
public function testShouldCloseWindow(): void
{
$this->driver->get($this->getTestPageUrl(TestPage::OPEN_NEW_WINDOW));
$this->driver->findElement(WebDriverBy::cssSelector('a'))->click();
$this->driver->wait()->until(WebDriverExpectedCondition::numberOfWindowsToBe(2));
$this->assertCount(2, $this->driver->getWindowHandles());
$this->driver->close();
$this->assertCount(1, $this->driver->getWindowHandles());
}
/**
* @covers ::executeScript
* @group exclude-saucelabs
*/
public function testShouldExecuteScriptAndDoNotBlockExecution(): void
{
$this->driver->get($this->getTestPageUrl(TestPage::INDEX));
$element = $this->driver->findElement(WebDriverBy::id('id_test'));
$this->assertSame('Test by ID', $element->getText());
$start = microtime(true);
$scriptResult = $this->driver->executeScript('
setTimeout(
function(){document.getElementById("id_test").innerHTML = "Text changed by script";},
250
);
return "returned value";
');
$end = microtime(true);
$this->assertSame('returned value', $scriptResult);
$this->assertLessThan(250, $end - $start, 'executeScript() should not block execution');
// If we wait, the script should be executed and its value changed
usleep(300000); // wait 300 ms
$this->assertSame('Text changed by script', $element->getText());
}
/**
* @covers ::executeAsyncScript
* @covers \Facebook\WebDriver\WebDriverTimeouts::setScriptTimeout
*/
public function testShouldExecuteAsyncScriptAndWaitUntilItIsFinished(): void
{
$this->driver->manage()->timeouts()->setScriptTimeout(1);
$this->driver->get($this->getTestPageUrl(TestPage::INDEX));
$element = $this->driver->findElement(WebDriverBy::id('id_test'));
$this->assertSame('Test by ID', $element->getText());
$start = microtime(true);
$scriptResult = $this->driver->executeAsyncScript(
'var callback = arguments[arguments.length - 1];
setTimeout(
function(){
document.getElementById("id_test").innerHTML = "Text changed by script";
callback("returned value");
},
250
);'
);
$end = microtime(true);
$this->assertSame('returned value', $scriptResult);
$this->assertGreaterThan(
0.250,
$end - $start,
'executeAsyncScript() should block execution until callback() is called'
);
// The result must be immediately available, as the executeAsyncScript should block the execution until the
// callback is called.
$this->assertSame('Text changed by script', $element->getText());
}
/**
* @covers ::executeScript
* @covers ::prepareScriptArguments
* @group exclude-saucelabs
*/
public function testShouldExecuteScriptWithParamsAndReturnValue(): void
{
$this->driver->manage()->timeouts()->setScriptTimeout(1);
$this->driver->get($this->getTestPageUrl(TestPage::INDEX));
$element1 = $this->driver->findElement(WebDriverBy::id('id_test'));
$element2 = $this->driver->findElement(WebDriverBy::className('test_class'));
$scriptResult = $this->driver->executeScript(
'var element1 = arguments[0];
var element2 = arguments[1];
return "1: " + element1.innerText + ", 2: " + element2.innerText;
',
[$element1, $element2]
);
$this->assertSame('1: Test by ID, 2: Test by Class', $scriptResult);
}
/**
* @covers ::takeScreenshot
* @covers \Facebook\WebDriver\Support\ScreenshotHelper
*/
public function testShouldTakeScreenshot(): void
{
if (!extension_loaded('gd')) {
$this->markTestSkipped('GD extension must be enabled');
}
$this->driver->get($this->getTestPageUrl(TestPage::INDEX));
$outputPng = $this->driver->takeScreenshot();
$image = imagecreatefromstring($outputPng);
$this->assertNotFalse($image);
$this->assertGreaterThan(0, imagesx($image));
$this->assertGreaterThan(0, imagesy($image));
}
/**
* @covers ::takeScreenshot
* @covers \Facebook\WebDriver\Support\ScreenshotHelper
* @group exclude-safari
* Safari is returning different color profile and it does not have way to configure "force-color-profile"
*/
public function testShouldSaveScreenshotToFile(): void
{
if (!extension_loaded('gd')) {
$this->markTestSkipped('GD extension must be enabled');
}
$screenshotPath = sys_get_temp_dir() . '/' . uniqid('php-webdriver-') . '/selenium-screenshot.png';
$this->driver->get($this->getTestPageUrl(TestPage::INDEX));
$this->driver->takeScreenshot($screenshotPath);
$image = imagecreatefrompng($screenshotPath);
$this->assertNotFalse($image);
$this->assertGreaterThan(0, imagesx($image));
$this->assertGreaterThan(0, imagesy($image));
// Validate expected red box is present on the screenshot
$this->assertSame(
['red' => 255, 'green' => 0, 'blue' => 0, 'alpha' => 0],
imagecolorsforindex($image, imagecolorat($image, 5, 5))
);
// And whitespace has expected background color
$this->assertSame(
['red' => 250, 'green' => 250, 'blue' => 255, 'alpha' => 0],
imagecolorsforindex($image, imagecolorat($image, 15, 5))
);
unlink($screenshotPath);
rmdir(dirname($screenshotPath));
}
/**
* @covers ::getStatus
* @covers \Facebook\WebDriver\Remote\RemoteStatus
* @group exclude-saucelabs
* Status endpoint is not supported on Sauce Labs
*/
public function testShouldGetRemoteEndStatus(): void
{
$status = $this->driver->getStatus();
$this->assertIsBool($status->isReady());
$this->assertIsArray($status->getMeta());
if (getenv('BROWSER_NAME') !== 'safari') {
$this->assertNotEmpty($status->getMessage());
}
}
}