-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathPendingAwaitablePage.php
More file actions
250 lines (213 loc) · 6.43 KB
/
PendingAwaitablePage.php
File metadata and controls
250 lines (213 loc) · 6.43 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
<?php
declare(strict_types=1);
namespace Pest\Browser\Api;
use Pest\Browser\Enums\BrowserType;
use Pest\Browser\Enums\ColorScheme;
use Pest\Browser\Enums\Device;
use Pest\Browser\Playwright\InitScript;
use Pest\Browser\Playwright\Playwright;
use Pest\Browser\Support\ComputeUrl;
/**
* @mixin Webpage|AwaitableWebpage
*/
final class PendingAwaitablePage
{
/**
* The webpage instance that will be returned when the page is visited.
*/
private ?AwaitableWebpage $waitablePage = null;
/**
* Creates a new pending awaitable page instance.
*
* @param array<string, mixed> $options
*/
public function __construct(
private readonly BrowserType $browserType,
private readonly Device $device,
private readonly string $url,
private readonly array $options,
) {
//
}
/**
* Creates the actual visit page instance, and calls the given method on it.
*
* @param array<int, mixed> $arguments
*/
public function __call(string $name, array $arguments): mixed
{
$this->waitablePage ??= $this->createAwaitablePage();
// @phpstan-ignore-next-line
return $this->waitablePage->{$name}(...$arguments);
}
/**
* Sets the color scheme to dark mode.
*/
public function inDarkMode(): self
{
return new self($this->browserType, $this->device, $this->url, [
'colorScheme' => ColorScheme::DARK->value,
...$this->options,
]);
}
/**
* Sets the color scheme to light mode.
*/
public function inLightMode(): self
{
return new self($this->browserType, $this->device, $this->url, [
'colorScheme' => ColorScheme::LIGHT->value,
...$this->options,
]);
}
/**
* Allows you to set a different locale, timezone, and location for the page.
*/
public function from(): From
{
return new From(
$this->browserType,
$this->device,
$this->url,
$this->options,
);
}
/**
* Allows you to set a different device for the page.
*/
public function on(): On
{
return new On(
$this->browserType,
$this->device,
$this->url,
$this->options,
);
}
/**
* Sets the locale for the page.
*/
public function withLocale(string $locale): self
{
return new self($this->browserType, $this->device, $this->url, [
'locale' => $locale,
...$this->options,
]);
}
/**
* Sets the userAgent for the page.
*/
public function withUserAgent(string $userAgent): self
{
return new self($this->browserType, $this->device, $this->url, [
'userAgent' => $userAgent,
...$this->options,
]);
}
/**
* Sets the host for the server.
*/
public function withHost(string $host): self
{
return new self($this->browserType, $this->device, $this->url, [
'host' => $host,
...$this->options,
]);
}
/**
* Sets the timezone for the page.
*/
public function withTimezone(string $timezone): self
{
return new self($this->browserType, $this->device, $this->url, [
'timezoneId' => $timezone,
...$this->options,
]);
}
/**
* Sets the geolocation for the page.
*/
public function geolocation(float $latitude, float $longitude): self
{
$geolocation = ['latitude' => $latitude, 'longitude' => $longitude];
return new self($this->browserType, $this->device, $this->url, [
'geolocation' => $geolocation,
'permissions' => ['geolocation'],
...$this->options,
]);
}
/**
* Creates the webpage instance.
*/
private function createAwaitablePage(): AwaitableWebpage
{
$options = $this->options;
$host = $this->extractHost($options);
return $this->withTemporaryHost($host, fn (): AwaitableWebpage => $this->buildAwaitablePage($options));
}
/**
* @param array<string, mixed> $options
*/
private function buildAwaitablePage(array $options): AwaitableWebpage
{
if (Playwright::shouldRecordVideoOnFailure()) {
// @phpstan-ignore-next-line
$testName = str_replace('__pest_evaluable_', '', test()->name());
// @phpstan-ignore-next-line
$sanitized = (string) preg_replace('/[^a-zA-Z0-9_-]/', '_', $testName);
}
$contextOptions = [
'locale' => 'en-US',
'timezoneId' => 'UTC',
'colorScheme' => Playwright::defaultColorScheme()->value,
...$this->device->context(),
...$options,
];
if (Playwright::shouldRecordVideoOnFailure()) {
// Empty array — Playwright uses its own temp storage; we retrieve via saveAs
$contextOptions['recordVideo'] = [];
}
$browser = Playwright::browser($this->browserType)->launch();
$context = $browser->newContext($contextOptions);
$context->addInitScript(InitScript::get());
$url = ComputeUrl::from($this->url);
// Strip context-only options before passing to goto
$gotoOptions = array_diff_key($options, array_flip(['host']));
$page = $context->newPage()->goto($url, $gotoOptions);
if (Playwright::shouldRecordVideoOnFailure()) {
Playwright::registerVideoRecording($page, $sanitized); // @phpstan-ignore-line
}
return new AwaitableWebpage(
$page,
$url,
);
}
/**
* @param array<string, mixed> &$options
*/
private function extractHost(array &$options): ?string
{
if (! array_key_exists('host', $options)) {
return null;
}
$host = $options['host'];
unset($options['host']);
return is_string($host) ? $host : null;
}
/**
* @param callable(): AwaitableWebpage $callback
*/
private function withTemporaryHost(?string $host, callable $callback): AwaitableWebpage
{
if ($host === null) {
return $callback();
}
$previousHost = Playwright::host();
Playwright::setHost($host);
try {
return $callback();
} finally {
Playwright::setHost($previousHost);
}
}
}