-
Notifications
You must be signed in to change notification settings - Fork 844
Expand file tree
/
Copy pathWebDriverTestCase.php
More file actions
248 lines (213 loc) · 7.89 KB
/
Copy pathWebDriverTestCase.php
File metadata and controls
248 lines (213 loc) · 7.89 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
<?php declare(strict_types=1);
namespace Facebook\WebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Exception\NoSuchWindowException;
use Facebook\WebDriver\Exception\WebDriverException;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverBrowserType;
use OndraM\CiDetector\CiDetector;
use PHPUnit\Framework\TestCase;
/**
* The base class for test cases.
*/
class WebDriverTestCase extends TestCase
{
/** @var RemoteWebDriver $driver */
public $driver;
/** @var bool Indicate whether WebDriver should be created on setUp */
protected $createWebDriver = true;
/** @var string */
protected $serverUrl = 'http://localhost:4444';
/** @var DesiredCapabilities */
protected $desiredCapabilities;
/** @var int */
protected $connectionTimeout = 60000;
/** @var int */
protected $requestTimeout = 60000;
protected function setUp(): void
{
$this->desiredCapabilities = new DesiredCapabilities();
if (static::isSauceLabsBuild()) {
$this->setUpSauceLabs();
} else {
$browserName = getenv('BROWSER_NAME');
$disableHeadless = filter_var(getenv('DISABLE_HEADLESS') ?: '', FILTER_VALIDATE_BOOLEAN);
if ($browserName === '' || $browserName === false) {
$this->markTestSkipped(
'To execute functional tests browser name must be provided in BROWSER_NAME environment variable'
);
}
if ($browserName === WebDriverBrowserType::CHROME) {
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments([
'--screen-info={1280x720}',
'--no-sandbox', // workaround for https://github.com/SeleniumHQ/selenium/issues/4961
'--force-color-profile=srgb',
'--disable-search-engine-choice-screen',
]);
if (!$disableHeadless) {
$chromeOptions->addArguments(['--headless']);
}
if (!static::isW3cProtocolBuild()) {
$chromeOptions->setExperimentalOption('w3c', false);
}
$this->desiredCapabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
} elseif ($browserName === WebDriverBrowserType::FIREFOX) {
$firefoxOptions = new FirefoxOptions();
if (!$disableHeadless) {
$firefoxOptions->addArguments(['-headless']);
}
$this->desiredCapabilities->setCapability(FirefoxOptions::CAPABILITY, $firefoxOptions);
}
$this->desiredCapabilities->setBrowserName($browserName);
}
$this->createWebDriver();
}
protected function tearDown(): void
{
if ($this->driver !== null) {
try {
$this->driver->quit();
} catch (NoSuchWindowException $e) {
// browser may have died or is already closed
}
$this->driver = null;
if (getenv('BROWSER_NAME') === 'safari') {
// The Safari instance is already paired with another WebDriver session
usleep(200000); // 200ms
}
}
}
public static function isSauceLabsBuild(): bool
{
return getenv('SAUCELABS') ? true : false;
}
public static function isW3cProtocolBuild(): bool
{
return getenv('DISABLE_W3C_PROTOCOL') !== '1';
}
public static function isSeleniumServerUsed(): bool
{
return getenv('SELENIUM_SERVER') === '1';
}
public static function skipForW3cProtocol($message = 'Not supported by W3C specification'): void
{
if (static::isW3cProtocolBuild()) {
static::markTestSkipped($message);
}
}
public static function skipForJsonWireProtocol($message = 'Not supported by JsonWire protocol'): void
{
if (!static::isW3cProtocolBuild()) {
static::markTestSkipped($message);
}
}
/**
* Mark a test as skipped if the current browser is not in the list of browsers.
*
* @param string[] $browsers List of browsers for this test
*/
public static function skipForUnmatchedBrowsers(array $browsers = [], ?string $message = null): void
{
$browserName = (string) getenv('BROWSER_NAME');
if (!in_array($browserName, $browsers, true)) {
if (!$message) {
$browserList = implode(', ', $browsers);
$message = 'Browser ' . $browserName . ' not supported for this test (' . $browserList . ')';
}
static::markTestSkipped($message);
}
}
/**
* Rerun failed tests.
* @TODO Replace with PHPUnit 7.3+ builtin functionality once upgraded to PHP 7.1+
*/
public function runBare(): void
{
$e = null;
$numberOfRetires = 3;
for ($i = 0; $i < $numberOfRetires; ++$i) {
try {
parent::runBare();
return;
} catch (WebDriverException $e) {
// repeat
}
}
if ($e !== null) {
throw $e;
}
}
/**
* Get the URL of given test HTML on running webserver.
*/
protected function getTestPageUrl(string $path): string
{
$host = 'http://localhost:8000';
if ($alternateHost = getenv('FIXTURES_HOST')) {
$host = $alternateHost;
}
return $host . '/' . $path;
}
protected function setUpSauceLabs(): void
{
$this->serverUrl = sprintf(
'http://%s:%s@ondemand.saucelabs.com/wd/hub',
getenv('SAUCE_USERNAME'),
getenv('SAUCE_ACCESS_KEY')
);
$this->desiredCapabilities->setBrowserName(getenv('BROWSER_NAME'));
$this->desiredCapabilities->setVersion(getenv('VERSION'));
$this->desiredCapabilities->setPlatform(getenv('PLATFORM'));
$name = get_class($this) . '::' . $this->getName();
$tags = [get_class($this)];
$ciDetector = new CiDetector();
if ($ciDetector->isCiDetected()) {
$ci = $ciDetector->detect();
if (!empty($ci->getBuildNumber())) {
// SAUCE_TUNNEL_NAME appended as a workaround for GH actions not having environment value
// to distinguish runs of the matrix
$build = $ci->getBuildNumber() . '.' . getenv('SAUCE_TUNNEL_NAME');
}
}
if (getenv('SAUCE_TUNNEL_NAME')) {
$tunnelName = getenv('SAUCE_TUNNEL_NAME');
}
if (static::isW3cProtocolBuild()) {
$sauceOptions = [
'name' => $name,
'tags' => $tags,
];
if (isset($build)) {
$sauceOptions['build'] = $build;
}
if (isset($tunnelName)) {
$sauceOptions['tunnelName'] = $tunnelName;
}
$this->desiredCapabilities->setCapability('sauce:options', (object) $sauceOptions);
} else {
$this->desiredCapabilities->setCapability('name', $name);
$this->desiredCapabilities->setCapability('tags', $tags);
if (isset($tunnelName)) {
$this->desiredCapabilities->setCapability('tunnel-identifier', $tunnelName);
}
if (isset($build)) {
$this->desiredCapabilities->setCapability('build', $build);
}
}
}
protected function createWebDriver(): void
{
$this->driver = RemoteWebDriver::create(
$this->serverUrl,
$this->desiredCapabilities,
$this->connectionTimeout,
$this->requestTimeout,
null,
null,
null
);
}
}