Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Playwright/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function execute(string $guid, string $method, array $params = [], array
/** @var array{id: string|null, params: array{add: string|null}, error: array{error: array{message: string|null}}} $response */
$response = json_decode($responseJson, true);

// A response addressed to another request was stranded by a generator
// abandoned before its final frame was read — goto() breaks on its
// waitUntil event, leaving its response unconsumed. Processing it here
// would desynchronize the stream, and the TargetClosedError a context
// close sends to a still-settling goto would fail an unrelated command.
if (isset($response['id']) && $response['id'] !== $requestId) {
continue;
}

if (isset($response['error']['error']['message'])) {
$message = $response['error']['error']['message'];

Expand Down
115 changes: 115 additions & 0 deletions tests/Unit/Playwright/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

use Amp\Websocket\Client\WebsocketConnection;
use Amp\Websocket\WebsocketMessage;
use Pest\Browser\Playwright\Client;
use PHPUnit\Framework\ExpectationFailedException;

it('drops responses addressed to another request', function (): void {
$connection = $this->createStub(WebsocketConnection::class);

$sent = null;
$connection->method('sendText')->willReturnCallback(function (string $payload) use (&$sent): void {
$sent = json_decode($payload, true);
});

$frames = [
['id' => 'stranded-request', 'result' => ['value' => 'stale']],
];

$connection->method('receive')->willReturnCallback(function () use (&$frames, &$sent): WebsocketMessage {
$frame = array_shift($frames) ?? ['id' => $sent['id']];

return WebsocketMessage::fromText((string) json_encode($frame));
});

$client = new Client();

new ReflectionProperty(Client::class, 'websocketConnection')->setValue($client, $connection);

$messages = iterator_to_array($client->execute('page@1', 'evaluateExpression'));

expect($messages)->toHaveCount(1)
->and($messages[0]['id'])->not->toBe('stranded-request');
});

it('does not fail the current request on an error frame addressed to another one', function (): void {
$connection = $this->createStub(WebsocketConnection::class);

$sent = null;
$connection->method('sendText')->willReturnCallback(function (string $payload) use (&$sent): void {
$sent = json_decode($payload, true);
});

$frames = [
['id' => 'stranded-goto', 'error' => ['error' => ['message' => 'Target page, context or browser has been closed']]],
];

$connection->method('receive')->willReturnCallback(function () use (&$frames, &$sent): WebsocketMessage {
$frame = array_shift($frames) ?? ['id' => $sent['id']];

return WebsocketMessage::fromText((string) json_encode($frame));
});

$client = new Client();

new ReflectionProperty(Client::class, 'websocketConnection')->setValue($client, $connection);

$messages = iterator_to_array($client->execute('browser@1', 'newContext'));

expect($messages)->toHaveCount(1)
->and($messages[0]['id'])->not->toBe('stranded-goto');
});

it('still fails the current request on its own error frame', function (): void {
$connection = $this->createStub(WebsocketConnection::class);

$sent = null;
$connection->method('sendText')->willReturnCallback(function (string $payload) use (&$sent): void {
$sent = json_decode($payload, true);
});

$connection->method('receive')->willReturnCallback(function () use (&$sent): WebsocketMessage {
return WebsocketMessage::fromText((string) json_encode([
'id' => $sent['id'],
'error' => ['error' => ['message' => 'Target page, context or browser has been closed']],
]));
});

$client = new Client();

new ReflectionProperty(Client::class, 'websocketConnection')->setValue($client, $connection);

expect(fn (): array => iterator_to_array($client->execute('page@1', 'click')))
->toThrow(ExpectationFailedException::class, 'Target page, context or browser has been closed');
});

it('still yields event frames, which carry no id', function (): void {
$connection = $this->createStub(WebsocketConnection::class);

$sent = null;
$connection->method('sendText')->willReturnCallback(function (string $payload) use (&$sent): void {
$sent = json_decode($payload, true);
});

$frames = [
['guid' => 'frame@1', 'method' => 'navigated', 'params' => ['url' => 'http://127.0.0.1/']],
];

$connection->method('receive')->willReturnCallback(function () use (&$frames, &$sent): WebsocketMessage {
$frame = array_shift($frames) ?? ['id' => $sent['id']];

return WebsocketMessage::fromText((string) json_encode($frame));
});

$client = new Client();

new ReflectionProperty(Client::class, 'websocketConnection')->setValue($client, $connection);

$messages = iterator_to_array($client->execute('frame@1', 'goto', ['url' => 'http://127.0.0.1/']));

expect($messages)->toHaveCount(2)
->and($messages[0]['method'])->toBe('navigated');
});