-
Notifications
You must be signed in to change notification settings - Fork 40
/
WebDriverListener.php
217 lines (186 loc) · 8.51 KB
/
WebDriverListener.php
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
<?php declare(strict_types=1);
namespace Lmc\Steward\Listener;
use Facebook\WebDriver\Exception\UnknownServerException;
use Facebook\WebDriver\Exception\WebDriverException;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\WebDriverBrowserType;
use Lmc\Steward\ConfigProvider;
use Lmc\Steward\Selenium\CapabilitiesResolver;
use Lmc\Steward\Test\AbstractTestCase;
use Lmc\Steward\WebDriver\NullWebDriver;
use Lmc\Steward\WebDriver\RemoteWebDriver;
use Nette\Reflection\AnnotationsParser;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\WarningTestCase;
use PHPUnit\Runner\BaseTestRunner;
/**
* Listener for initialization and destruction of WebDriver before and after each test.
*
* Note: This is done as a listener rather then in setUp() and tearDown(), as a workaround
* for the sequence in which PHPUnit executes tearDown() of tests and addFailure() on listeners.
* If taking screenshot using addFailure(), tearDown() would have already been called and the
* browser would be closed.
*/
class WebDriverListener implements TestListener
{
use TestListenerDefaultImplementation;
protected const NO_BROWSER_ANNOTATION = 'noBrowser';
/** @var ConfigProvider */
protected $config;
/** @var CapabilitiesResolver */
protected $capabilitiesResolver;
public function __construct()
{
$this->config = ConfigProvider::getInstance();
}
protected function getCapabilitiesResolver(): CapabilitiesResolver
{
if ($this->capabilitiesResolver === null) {
$this->capabilitiesResolver = new CapabilitiesResolver($this->config);
}
return $this->capabilitiesResolver;
}
public function startTest(Test $test): void
{
if ($test instanceof WarningTestCase) {
return;
}
if (!$test instanceof AbstractTestCase) {
throw new \InvalidArgumentException('Test case must be descendant of Lmc\Steward\Test\AbstractTestCase');
}
if ($test->getStatus() === BaseTestRunner::STATUS_SKIPPED) {
return;
}
// Initialize NullWebDriver if self::NO_BROWSER_ANNOTATION is used on testcase class or test method
$testCaseAnnotations = AnnotationsParser::getAll(new \ReflectionClass($test));
$testAnnotations = AnnotationsParser::getAll(new \ReflectionMethod($test, $test->getName(false)));
if (isset($testCaseAnnotations[self::NO_BROWSER_ANNOTATION])
|| isset($testAnnotations[self::NO_BROWSER_ANNOTATION])
) {
$test->wd = new NullWebDriver();
$test->log(
'Initializing Null WebDriver (@%s annotation used %s)',
self::NO_BROWSER_ANNOTATION,
isset($testCaseAnnotations[self::NO_BROWSER_ANNOTATION]) ? 'on class' : 'on method'
);
return;
}
// Initialize real WebDriver otherwise
$test->log('Initializing "%s" WebDriver', $this->config->browserName);
$desiredCapabilities = $this->getCapabilitiesResolver()->resolveDesiredCapabilities($test);
$requiredCapabilities = $this->getCapabilitiesResolver()->resolveRequiredCapabilities($test);
$this->createWebDriver(
$test,
$this->config->serverUrl,
$desiredCapabilities,
$requiredCapabilities,
$connectTimeoutMs = 2 * 60 * 1000,
// How long could request to Selenium take (eg. how long could we wait in hub's queue to available node)
$requestTimeoutMs = 60 * 60 * 1000 // 1 hour (same as timeout for the whole process)
);
}
public function endTest(Test $test, float $time): void
{
if ($test instanceof WarningTestCase) {
return;
}
if (!$test instanceof AbstractTestCase) {
throw new \InvalidArgumentException('Test case must be descendant of Lmc\Steward\Test\AbstractTestCase');
}
if ($test->wd instanceof RemoteWebDriver) {
// The endTest() method of WebDriverListener is called before endTest() of ResultPrinter, so any output
// from this listener would overtake output from the tests itself, what would be confusing. Instead of this,
// we append them to the output of the test itself, to print them in proper chronological order.
$test->appendTestLog(
'Destroying "%s" WebDriver for session "%s"',
ConfigProvider::getInstance()->browserName,
$test->wd->getSessionID()
);
ob_start(); // Capture any output from commands bellow to make them appended to output of the test.
try {
// Workaround for PhantomJS 1.x - see https://github.com/detro/ghostdriver/issues/343
// Should be removed with PhantomJS 2
if (ConfigProvider::getInstance()->browserName === WebDriverBrowserType::PHANTOMJS) {
$test->wd->execute('deleteAllCookies');
}
$test->wd->close();
$test->wd->quit();
} catch (WebDriverException $e) {
$test->warn('Error closing the session, browser may died.');
} finally {
$output = ob_get_clean();
$test->appendFormattedTestLog($output);
}
}
}
/**
* Subroutine to encapsulate creation of real WebDriver. Handles some exceptions that may occur etc.
* The WebDriver instance is stored to $test->wd when created.
*
* @throws UnknownServerException
*/
protected function createWebDriver(
AbstractTestCase $test,
string $remoteServerUrl,
DesiredCapabilities $desiredCapabilities,
DesiredCapabilities $requiredCapabilities,
int $connectTimeoutMs,
int $requestTimeoutMs
): void {
$browserName = ConfigProvider::getInstance()->browserName;
for ($startAttempts = 0; $startAttempts < 4; $startAttempts++) {
try {
$test->wd = RemoteWebDriver::create(
$remoteServerUrl,
$desiredCapabilities,
$connectTimeoutMs,
$requestTimeoutMs,
null,
null,
$requiredCapabilities
);
return;
} catch (UnknownServerException $e) {
if ($this->cannotBindToFirefoxLockingPort($browserName, $e->getMessage())) {
// As a consequence of Selenium issue #5172 (cannot change locking port), Firefox may on CI server
// collide with other FF instance. As a workaround, we try to start it again after a short delay.
$test->warn(
'Firefox locking port is occupied; beginning attempt #%d to start it ("%s")',
$startAttempts + 2,
$e->getMessage()
);
sleep(1);
continue;
}
if (mb_strpos($e->getMessage(), 'Error forwarding the new session') !== false) {
$test->warn('Cannot execute test on the node. Maybe you started just the hub and not the node?');
}
throw $e;
} catch (WebDriverException $e) {
if ($this->mayBeMissingFullServerUrl($remoteServerUrl, $e->getMessage())) {
$test->warn('Unable to initialize new WebDriver session.');
$test->warn(
'Server URL ("%s") appears to be missing "/wd/hub" part, so make sure you are ussing full URL'
. ' of the server (not just a hostname and port).',
$remoteServerUrl
);
}
throw $e;
}
}
$test->warn('All %d attempts to instantiate Firefox WebDriver failed', $startAttempts + 1);
throw $e;
}
private function cannotBindToFirefoxLockingPort(string $browserName, string $exceptionMessage): bool
{
return $browserName === 'firefox'
&& mb_strpos($exceptionMessage, 'Unable to bind to locking port') !== false;
}
private function mayBeMissingFullServerUrl(string $remoteServerUrl, string $exceptionMessage): bool
{
return mb_strpos($exceptionMessage, 'JSON decoding of remote response failed.') !== false
&& mb_strpos($remoteServerUrl, 'wd/hub') === false;
}
}