1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk/) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDev/ for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2014-2016 Webino, s. r. o. (http://webino.sk/) |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WebinoDev\Test\Selenium; |
11
|
|
|
|
12
|
|
|
use PHPWebDriver_WebDriver; |
13
|
|
|
use PHPWebDriver_WebDriverWait as Wait; |
14
|
|
|
use PHPWebDriver_WebDriverElement; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base test case for Selenium WebDriver tests |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
use ElementTrait; |
23
|
|
|
use ElementsTrait; |
24
|
|
|
use ScreenshotTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Selenium Web Driver host URI |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected static $webDriverHost = 'http://localhost:%s/wd/hub'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Selenium Web Driver port |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected static $webDriverPort = '4444'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected static $browser = 'htmlunit'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $browserBin; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $uri; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var PHPWebDriver_WebDriver |
57
|
|
|
*/ |
58
|
|
|
protected $webDriver; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var \PHPWebDriver_WebDriverSession |
62
|
|
|
*/ |
63
|
|
|
protected $session; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Resolves URI to open session |
67
|
|
|
*/ |
68
|
|
|
protected function setUp() |
69
|
|
|
{ |
70
|
|
|
$this->uri = $this->resolveUri(); |
71
|
|
|
$this->webDriver = new PHPWebDriver_WebDriver($this->resolveHost()); |
72
|
|
|
$this->session = $this->webDriver->session($this->resolveBrowser(), $this->resolveCapabilities()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tears down the fixture, for example, closes a network connection. |
77
|
|
|
* This method is called after a test is executed. |
78
|
|
|
*/ |
79
|
|
|
protected function tearDown() |
80
|
|
|
{ |
81
|
|
|
$this->session->close(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function getBrowser() |
88
|
|
|
{ |
89
|
|
|
return $this::$browser; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getBrowserBin() |
96
|
|
|
{ |
97
|
|
|
return $this->browserBin; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected function setBrowserBin($bin) |
104
|
|
|
{ |
105
|
|
|
$this->browserBin = (string) $bin; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return \PHPWebDriver_WebDriverSession |
111
|
|
|
*/ |
112
|
|
|
protected function getSession() |
113
|
|
|
{ |
114
|
|
|
return $this->session; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function getSessionId() |
121
|
|
|
{ |
122
|
|
|
return $this->session->getCookie('PHPSESSID')['value']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function getServerUrl() |
129
|
|
|
{ |
130
|
|
|
$parts = parse_url($this->session->url()); |
131
|
|
|
return $parts['scheme'] . '://' . $parts['host']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get raw source from URL |
136
|
|
|
* |
137
|
|
|
* @param string $url |
138
|
|
|
* @param null $sessId |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function source($url, $sessId = null) |
142
|
|
|
{ |
143
|
|
|
$sid = $sessId ? $sessId : $this->getSessionId(); |
144
|
|
|
$opts = ['http' => ['header'=> 'Cookie: PHPSESSID=' . $sid ."\r\n"]]; |
145
|
|
|
return file_get_contents($url, false, stream_context_create($opts)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Resolve Selenium WebDriver host |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function resolveHost() |
154
|
|
|
{ |
155
|
|
|
$host = getenv('HOST'); |
156
|
|
|
empty($host) or $this::$webDriverHost = $host; |
157
|
|
|
return sprintf($this::$webDriverHost, $this->resolvePort()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Resolve Selenium WebDriver port |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function resolvePort() |
166
|
|
|
{ |
167
|
|
|
$port = getenv('PORT'); |
168
|
|
|
empty($port) or $this::$webDriverPort = $port; |
169
|
|
|
return $this::$webDriverPort; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Resolve test session browser |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
protected function resolveBrowser() |
178
|
|
|
{ |
179
|
|
|
$browser = getenv('BROWSER'); |
180
|
|
|
empty($browser) or $this::$browser = $browser; |
181
|
|
|
|
182
|
|
|
switch ($this::$browser) { |
183
|
|
|
case 'chromium': |
184
|
|
|
$this::$browser = 'chrome'; |
185
|
|
|
$this->setBrowserBin('/usr/bin/chromium-browser'); |
186
|
|
|
break; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $this::$browser; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Resolve test session capabilities |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
|
protected function resolveCapabilities() |
198
|
|
|
{ |
199
|
|
|
switch ($this->getBrowser()) { |
200
|
|
|
case 'chrome': |
201
|
|
|
// Fixes OpenVZ |
202
|
|
|
$opts = ['args' => ['no-sandbox', 'start-maximized']]; |
203
|
|
|
$bin = $this->getBrowserBin(); |
204
|
|
|
$bin and $opts+= ['binary' => $bin]; |
205
|
|
|
return ['chromeOptions' => $opts]; |
206
|
|
|
} |
207
|
|
|
return []; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Resolve test target URI |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
protected function resolveUri() |
216
|
|
|
{ |
217
|
|
|
$uri = getenv('URI'); |
218
|
|
|
if (empty($uri)) { |
219
|
|
|
throw new RuntimeException('Expected URI env'); |
220
|
|
|
} |
221
|
|
|
return $uri; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Opens URI and asserts not error |
226
|
|
|
* |
227
|
|
|
* @param string $path |
228
|
|
|
* @param string $caption |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
|
|
protected function openOk($path = '', $caption = 'Home') |
232
|
|
|
{ |
233
|
|
|
$this->session->open($this->uri . $path); |
234
|
|
|
$this->attachScreenshot($caption); |
235
|
|
|
$this->assertNotError(); |
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Assert that page is without errors |
241
|
|
|
* |
242
|
|
|
* @return $this |
243
|
|
|
*/ |
244
|
|
|
protected function assertNotError() |
245
|
|
|
{ |
246
|
|
|
$this->assertNotContains('Error', $this->session->title()); |
247
|
|
|
|
248
|
|
|
// strip script contents & tags |
249
|
|
|
$text = $this->session->source(); |
250
|
|
|
$expr = '~<(script).*?>.*?</script>~si'; |
251
|
|
|
$src = strip_tags(preg_replace($expr, '', $text)); |
252
|
|
|
|
253
|
|
|
$this->assertNotContains('Error', $src); |
254
|
|
|
$this->assertNotContains('Exception', $src); |
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Clicks on a link |
260
|
|
|
* |
261
|
|
|
* @param string $linkText |
262
|
|
|
* @param callable $callback |
263
|
|
|
* @return $this |
264
|
|
|
*/ |
265
|
|
|
protected function clickLink($linkText, callable $callback = null) |
266
|
|
|
{ |
267
|
|
|
$elm = $this->elementByLinkText($linkText); |
268
|
|
|
$elm->click(); |
269
|
|
|
$this->attachScreenshot('Click ' . $linkText); |
270
|
|
|
$callback and call_user_func($callback, $elm); |
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Clicks on ajax-link |
276
|
|
|
* |
277
|
|
|
* @param string $linkText |
278
|
|
|
* @param callable $callback |
279
|
|
|
* @return $this |
280
|
|
|
*/ |
281
|
|
|
protected function clickAjaxLink($linkText, callable $callback = null) |
282
|
|
|
{ |
283
|
|
|
$this->clickLink($linkText, $callback); |
284
|
|
|
$this->attachScreenshot('Click ' . $linkText); |
285
|
|
|
$this->waitForAjax(); |
286
|
|
|
$this->attachScreenshot($linkText); |
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param string $name |
292
|
|
|
* @param string $value |
293
|
|
|
* @return $this |
294
|
|
|
*/ |
295
|
|
|
protected function clickSelect($name, $value) |
296
|
|
|
{ |
297
|
|
|
$selector = sprintf('select[name="%s"] option[value="%s"]', $name, $value); |
298
|
|
|
$this->elementByCssSelector($selector)->click(); |
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Enters the input value |
304
|
|
|
* |
305
|
|
|
* @param string|PHPWebDriver_WebDriverElement $name |
306
|
|
|
* @param string $value |
307
|
|
|
* @param callable|false|null $callback |
308
|
|
|
* @return $this |
309
|
|
|
*/ |
310
|
|
|
protected function enterInput($name, $value, $callback = null) |
311
|
|
|
{ |
312
|
|
|
$resolveElm = function () use ($name) { |
313
|
|
|
return ($name instanceof PHPWebDriver_WebDriverElement) ? $name : $this->elementByName($name); |
314
|
|
|
}; |
315
|
|
|
|
316
|
|
|
/** @var PHPWebDriver_WebDriverElement $elm */ |
317
|
|
|
$elm = $resolveElm(); |
318
|
|
|
if (null === $callback) { |
319
|
|
|
sleep(1); |
320
|
|
|
$elm->clear(); |
321
|
|
|
$elm->sendKeys(''); |
322
|
|
|
sleep(1); |
323
|
|
|
} |
324
|
|
|
$elm->clear(); |
325
|
|
|
$elm->sendKeys($value); |
326
|
|
|
$this->attachScreenshot('Input ' . $name); |
327
|
|
|
is_callable($callback) and call_user_func($callback, $elm); |
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Submit the input value |
333
|
|
|
* |
334
|
|
|
* @param string|PHPWebDriver_WebDriverElement $name |
335
|
|
|
* @param string $value |
336
|
|
|
* @return $this |
337
|
|
|
*/ |
338
|
|
|
protected function submitInput($name, $value) |
339
|
|
|
{ |
340
|
|
|
$this->enterInput($name, $value, function ($elm) { |
341
|
|
|
$elm->submit(); |
342
|
|
|
}); |
343
|
|
|
return $this; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Assert that input value is same than expected |
348
|
|
|
* |
349
|
|
|
* @param string|PHPWebDriver_WebDriverElement $name |
350
|
|
|
* @param string $expectedValue |
351
|
|
|
* @param callable $callback |
352
|
|
|
* @return $this |
353
|
|
|
*/ |
354
|
|
|
public function assertInput($name, $expectedValue, callable $callback = null) |
355
|
|
|
{ |
356
|
|
|
$elm = ($name instanceof PHPWebDriver_WebDriverElement) ? $name : $this->elementByName($name); |
357
|
|
|
$this->assertSame($expectedValue, $elm->attribute('value')); |
358
|
|
|
$callback and call_user_func($callback, $elm); |
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Focus a browser window |
364
|
|
|
* |
365
|
|
|
* @param int $index |
366
|
|
|
* @return $this |
367
|
|
|
*/ |
368
|
|
|
protected function focusWindow($index) |
369
|
|
|
{ |
370
|
|
|
$session = $this->getSession(); |
371
|
|
|
$windows = $session->window_handles(); |
372
|
|
|
$session->focusWindow($windows[$index]); |
373
|
|
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Close a browser window |
378
|
|
|
* |
379
|
|
|
* @return $this |
380
|
|
|
*/ |
381
|
|
|
protected function closeWindow() |
382
|
|
|
{ |
383
|
|
|
$this->getSession()->deleteWindow(); |
384
|
|
|
$this->focusWindow(0); |
385
|
|
|
return $this; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Wait for something, then do something else |
390
|
|
|
* |
391
|
|
|
* @param callable $action |
392
|
|
|
* @param callable $callback |
393
|
|
|
* @return $this |
394
|
|
|
*/ |
395
|
|
|
protected function waitFor(callable $action, callable $callback = null) |
396
|
|
|
{ |
397
|
|
|
$elm = (new Wait($this->session))->until($action); |
398
|
|
|
$callback and call_user_func($callback, $elm); |
399
|
|
|
return $this; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Ajax wait |
404
|
|
|
* |
405
|
|
|
* Depends on jQuery. |
406
|
|
|
* |
407
|
|
|
* @param float $delay Seconds |
408
|
|
|
* @return $this |
409
|
|
|
*/ |
410
|
|
|
protected function waitForAjax($delay = .1) |
411
|
|
|
{ |
412
|
|
|
// delay slightly more than required |
413
|
|
|
$delay and usleep($delay * 1100000); |
414
|
|
|
|
415
|
|
|
$this->waitFor(function () { |
416
|
|
|
return $this->session->execute(['script' => 'return !jQuery.active', 'args' => []]); |
417
|
|
|
}); |
418
|
|
|
|
419
|
|
|
return $this; |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|