-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathClient.php
More file actions
197 lines (163 loc) · 5.85 KB
/
Client.php
File metadata and controls
197 lines (163 loc) · 5.85 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
<?php
declare(strict_types=1);
namespace Pest\Browser\Playwright;
use Amp\Websocket\Client\WebsocketConnection;
use Generator;
use Pest\Browser\Exceptions\PlaywrightOutdatedException;
use PHPUnit\Framework\ExpectationFailedException;
use WeakReference;
use function Amp\Websocket\Client\connect;
/**
* @internal
*/
final class Client
{
/**
* Client instance.
*/
private static ?Client $instance = null;
/**
* WebSocket client instance.
*/
private ?WebsocketConnection $websocketConnection = null;
/**
* Registry of Page instances for handling events.
*
* @var array<string, WeakReference<Page>>
*/
private array $pages = [];
/**
* Default timeout for requests in milliseconds.
*/
private int $timeout = 5_000;
/**
* Returns the current client instance.
*/
public static function instance(): self
{
if (! self::$instance instanceof self) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Connects to the Playwright server.
*/
public function connectTo(string $url): void
{
if (! $this->websocketConnection instanceof WebsocketConnection) {
$browser = Playwright::defaultBrowserType()->toPlaywrightName();
$launchOptions = json_encode([
'headless' => Playwright::isHeadless(),
'ignoreHTTPSErrors' => true,
'bypassCSP' => true,
]);
$this->websocketConnection = connect(
"ws://$url?browser=$browser&launch-options=$launchOptions",
);
}
}
/**
* Executes a method on the Playwright instance.
*
* @param array<string, mixed> $params
* @param array<string, mixed> $meta
* @return Generator<array<string, mixed>>
*/
public function execute(string $guid, string $method, array $params = [], array $meta = []): Generator
{
assert($this->websocketConnection instanceof WebsocketConnection, 'WebSocket client is not connected.');
$requestId = uniqid();
$requestJson = (string) json_encode([
'id' => $requestId,
'guid' => $guid,
'method' => $method,
'params' => ['timeout' => $this->timeout, ...$params],
'metadata' => $meta,
]);
$this->websocketConnection->sendText($requestJson);
while (true) {
// @phpstan-ignore-next-line
$responseJson = $this->fetch($this->websocketConnection);
/** @var array{id: string|null, guid: string|null, method: string|null, params: array{add: string|null, type: string|null, guid: string|null, initializer: array{mainFrame: array{guid: string}, opener: array{guid: string}}|null }, error: array{error: array{message: string|null}}} $response */
$response = json_decode($responseJson, true);
if (isset($response['error']['error']['message'])) {
$message = $response['error']['error']['message'];
if (str_contains($message, 'Playwright was just installed or updated')) {
throw new PlaywrightOutdatedException();
}
throw new ExpectationFailedException($message);
}
if (isset($response['method']) && $response['method'] === '__create__'
&& isset($response['params']['type']) && $response['params']['type'] === 'Page'
&& isset($response['guid'], $response['params']['guid'], $response['params']['initializer']['opener']['guid'])) {
$this->handlePopupCreation($response['params']['initializer']['opener']['guid'], $response['params']['guid'], $response['params']['initializer']);
}
if (isset($response['method']) && $response['method'] === '__dispose__'
&& isset($response['guid']) && $this->getPage($response['guid']) instanceof Page) {
$this->unregisterPage($response['guid']);
}
yield $response;
if (
(isset($response['id']) && $response['id'] === $requestId)
|| (isset($params['waitUntil']) && isset($response['params']['add']) && $params['waitUntil'] === $response['params']['add'])
) {
break;
}
}
}
/**
* Sets the timeout in milliseconds for requests.
*/
public function setTimeout(int $timeout): void
{
$this->timeout = $timeout;
}
/**
* Returns the current timeout for requests.
*/
public function timeout(): int
{
return $this->timeout;
}
/**
* Registers the current page for event handling.
*/
public function registerPage(string $guid, Page $page): void
{
$this->pages[$guid] = WeakReference::create($page);
}
/**
* Removes page from event handling.
*/
public function unregisterPage(string $guid): void
{
unset($this->pages[$guid]);
}
private function getPage(string $guid): ?Page
{
if (! array_key_exists($guid, $this->pages)) {
return null;
}
return $this->pages[$guid]->get();
}
/**
* Handles popup creation events.
*
* @param array{mainFrame: array{guid: string}, opener: array{guid: string}} $initializer
*/
private function handlePopupCreation(string $openerGuid, string $popupGuid, array $initializer): void
{
$opener = $this->getPage($openerGuid);
if ($opener instanceof Page && $opener->hasPendingPopup()) {
$opener->handlePopupCreation($popupGuid, $initializer['mainFrame']['guid']);
}
}
/**
* Fetches the response from the Playwright server.
*/
private function fetch(WebsocketConnection $client): string
{
return (string) $client->receive()?->read();
}
}