-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathPlaywright.php
More file actions
357 lines (308 loc) · 9.06 KB
/
Playwright.php
File metadata and controls
357 lines (308 loc) · 9.06 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
declare(strict_types=1);
namespace Pest\Browser\Playwright;
use Pest\Browser\Enums\BrowserType;
use Pest\Browser\Enums\ColorScheme;
/**
* @internal
*/
final class Playwright
{
/**
* Browser types
*
* @var array<string, BrowserFactory>
*/
private static array $browserTypes = [];
/**
* Whether to run browsers in headless mode.
*/
private static bool $headless = true;
/**
* Whether to debug assertions.
*/
private static bool $shouldDebugAssertions = false;
/**
* Whether to show the diff on screenshot assertions.
*/
private static bool $shouldDiffOnScreenshotAssertions = false;
/**
* The default browser type.
*/
private static BrowserType $defaultBrowserType = BrowserType::CHROME;
/**
* The default color scheme.
*/
private static ColorScheme $defaultColorScheme = ColorScheme::LIGHT;
/**
* The timeout in milliseconds.
*/
private static int $timeout = 5_000;
/**
* The default userAgent.
*/
private static ?string $userAgent = null;
/**
* The default host.
*/
private static ?string $host = null;
/**
* The default screenshot threshold
*/
private static float $screenshotThreshold = 0.3;
/**
* Default maximum number of total pixels that can be different
* ENHANCE: establish a good actual baseline for this, leaving at the current Pest default of 300 which is very generous
* ENHANCE: establish a way to allow this to be undefined in JS to let the pixel ratio take precedence
*/
private static ?int $screenshotMaxDiffPixels = 300;
/**
* The maximum ratio of pixels that can differ (superseded by $screenshotMaxDiffPixels)
*/
private static float $screenshotMaxDiffPixelRatio = 0.01;
/**
* Get a browser factory for the given browser type.
*/
public static function browser(BrowserType $browserType): BrowserFactory
{
$name = $browserType->toPlaywrightName();
return self::$browserTypes[$name] ?? self::initialize($name);
}
/**
* Close all browser pages
*/
public static function close(): void
{
foreach (self::$browserTypes as $browserType) {
$browserType->close();
}
self::$browserTypes = [];
}
/**
* Set playwright in non-headless mode.
*/
public static function headed(): void
{
self::$headless = false;
}
/**
* Checks if Playwright is running in headless mode.
*/
public static function isHeadless(): bool
{
return self::$headless;
}
/**
* Set whether to show the diff on screenshot assertions.
*/
public static function setShouldDiffOnScreenshotAssertions(): void
{
self::$shouldDiffOnScreenshotAssertions = true;
}
/**
* Set the default color scheme.
*/
public static function setColorScheme(ColorScheme $colorScheme): void
{
self::$defaultColorScheme = $colorScheme;
}
/**
* Set the timeout for assertions.
*/
public static function setTimeout(int $timeout): void
{
self::$timeout = $timeout;
Client::instance()->setTimeout($timeout);
}
/**
* Get the timeout for assertions.
*/
public static function timeout(): int
{
return self::$timeout;
}
/**
* Set the default userAgent.
*/
public static function setUserAgent(string $userAgent): void
{
self::$userAgent = $userAgent;
}
/**
* Set the default host.
*/
public static function setHost(?string $host): void
{
self::$host = $host;
}
/**
* Get the default host.
*/
public static function host(): ?string
{
return self::$host;
}
/**
* Get the default color scheme.
*/
public static function defaultColorScheme(): ColorScheme
{
return self::$defaultColorScheme;
}
/**
* Whether to show the diff on screenshot assertions.
*/
public static function shouldShowDiffOnScreenshotAssertions(): bool
{
return self::$shouldDiffOnScreenshotAssertions;
}
/**
* Set whether to debug assertions.
*/
public static function setShouldDebugAssertions(): void
{
self::$shouldDebugAssertions = true;
}
/**
* Whether to debug assertions.
*/
public static function shouldDebugAssertions(): bool
{
return self::$shouldDebugAssertions;
}
/**
* Reset playwright state, reset browser types, without closing them.
*/
public static function reset(): void
{
foreach (self::$browserTypes as $browserType) {
$browserType->reset();
}
}
/**
* Sets the default browser type.
*/
public static function setDefaultBrowserType(BrowserType $browserType): void
{
self::$defaultBrowserType = $browserType;
}
/**
* Get the default browser type.
*/
public static function defaultBrowserType(): BrowserType
{
return self::$defaultBrowserType;
}
/**
* Set the screenshot threshold used for screenshot comparison
*
* @see https://playwright.dev/docs/api/class-locatorassertions#locator-assertions-to-have-screenshot-2-option-threshold
*/
public static function setScreenshotThreshold(float $threshold): void
{
self::$screenshotThreshold = max(0, min(1, $threshold));
}
/**
* get the browser screenshot threshold
*/
public static function screenshotThreshold(): float
{
return self::$screenshotThreshold;
}
/**
* An acceptable amount of pixels that could be different.
*
* @see https://playwright.dev/docs/api/class-pageassertions#page-assertions-to-have-screenshot-1-option-max-diff-pixels
*/
public static function setScreenshotMaxDiffPixels(?int $maxDiffPixels): void
{
self::$screenshotMaxDiffPixels = max(0, $maxDiffPixels);
}
/**
* An acceptable ratio of pixels that are different to the total amount of pixels,
*
* get the browser screenshot maxDiffPixels
*/
public static function screenshotMaxDiffPixels(): ?int
{
return self::$screenshotMaxDiffPixels;
}
/**
* An acceptable amount of pixels that could be different.
*
* @see https://playwright.dev/docs/api/class-pageassertions#page-assertions-to-have-screenshot-1-option-max-diff-pixel-ratio
*/
public static function setScreenshotMaxDiffPixelRatio(float $maxDiffPixelRatio): void
{
self::$screenshotMaxDiffPixelRatio = max(0, $maxDiffPixelRatio);
}
/**
* An acceptable ratio of pixels that are different to the total amount of pixels,
*
* get the browser screenshot maxDiffPixels
*/
public static function screenshotMaxDiffPixelRatio(): float
{
return self::$screenshotMaxDiffPixelRatio;
}
/**
* Executes a callback with a temporary timeout.
*
* @template TReturnType
*
* @param callable(): TReturnType $callback
* @return TReturnType
*/
public static function usingTimeout(int $timeout, callable $callback): mixed
{
$previousTimeout = Client::instance()->timeout();
Client::instance()->setTimeout($timeout);
try {
return $callback();
} finally {
Client::instance()->setTimeout($previousTimeout);
}
}
/**
* Initialize Playwright
*/
private static function initialize(string $browser): BrowserFactory
{
$response = Client::instance()->execute(
'',
'initialize',
[
'sdkLanguage' => 'javascript',
]
);
/** @var array{method: string|null, params: array{type: string|null, guid: string, initializer: array{name: string|null, preLaunchedBrowser?: array{guid: string}}}} $message */
foreach ($response as $message) {
if (
isset($message['method'])
&& $message['method'] === '__create__'
&& isset($message['params']['type'])
&& $message['params']['type'] === 'Playwright'
&& isset($message['params']['initializer']['preLaunchedBrowser'])
) {
self::$browserTypes[$browser]->prelaunch(
$message['params']['initializer']['preLaunchedBrowser']['guid'],
);
}
if (
isset($message['method'])
&& $message['method'] === '__create__'
&& isset($message['params']['type'])
&& $message['params']['type'] === 'BrowserType'
) {
$name = $message['params']['initializer']['name'] ?? '';
self::$browserTypes[$name] = new BrowserFactory(
$message['params']['guid'],
$name,
self::$headless,
self::$userAgent
);
}
}
return self::$browserTypes[$browser];
}
}