Skip to content

Commit 0cbe02f

Browse files
committed
Add missing Page Options
1 parent b79c43f commit 0cbe02f

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the community-maintained Playwright PHP project.
7+
* It is not affiliated with or endorsed by Microsoft.
8+
*
9+
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Playwright\Page\Options;
16+
17+
final readonly class WaitForResponseOptions
18+
{
19+
public function __construct(
20+
public ?float $timeout = null,
21+
) {
22+
}
23+
24+
/**
25+
* @return array<string, mixed>
26+
*/
27+
public function toArray(): array
28+
{
29+
$options = [];
30+
if (null !== $this->timeout) {
31+
$options['timeout'] = $this->timeout;
32+
}
33+
34+
return $options;
35+
}
36+
37+
/**
38+
* @param array<string, mixed>|self $options
39+
*/
40+
public static function from(array|self $options = []): self
41+
{
42+
if ($options instanceof self) {
43+
return $options;
44+
}
45+
46+
/** @var float|null $timeout */
47+
$timeout = $options['timeout'] ?? null;
48+
49+
return new self($timeout);
50+
}
51+
}

src/Page/Page.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use Playwright\Page\Options\TypeOptions;
5757
use Playwright\Page\Options\WaitForLoadStateOptions;
5858
use Playwright\Page\Options\WaitForPopupOptions;
59+
use Playwright\Page\Options\WaitForResponseOptions;
5960
use Playwright\Page\Options\WaitForSelectorOptions;
6061
use Playwright\Page\Options\WaitForUrlOptions;
6162
use Playwright\Screenshot\ScreenshotHelper;
@@ -763,15 +764,19 @@ public function waitForURL($url, array|WaitForUrlOptions $options = []): self
763764
}
764765

765766
/**
766-
* @param string|callable $url
767-
* @param array<string, mixed> $options
767+
* @param string|callable $url
768+
* @param array<string, mixed>|WaitForResponseOptions $options
768769
*/
769-
public function waitForResponse($url, array $options = []): ResponseInterface
770+
public function waitForResponse($url, array|WaitForResponseOptions $options = []): ResponseInterface
770771
{
771-
$action = $options['action'] ?? null;
772-
unset($options['action']);
772+
$action = null;
773+
if (is_array($options)) {
774+
$action = $options['action'] ?? null;
775+
unset($options['action']);
776+
}
773777

774-
$response = $this->sendCommand('waitForResponse', ['url' => $url, 'options' => $options, 'jsAction' => $action]);
778+
$options = WaitForResponseOptions::from($options);
779+
$response = $this->sendCommand('waitForResponse', ['url' => $url, 'options' => $options->toArray(), 'jsAction' => $action]);
775780

776781
return $this->createResponse($this->pageId, $response['response']);
777782
}

src/Page/PageInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Playwright\Page\Options\TypeOptions;
3737
use Playwright\Page\Options\WaitForLoadStateOptions;
3838
use Playwright\Page\Options\WaitForPopupOptions;
39+
use Playwright\Page\Options\WaitForResponseOptions;
3940
use Playwright\Page\Options\WaitForSelectorOptions;
4041
use Playwright\Page\Options\WaitForUrlOptions;
4142

@@ -220,6 +221,12 @@ public function waitForEvents(): void;
220221
*/
221222
public function waitForPopup(callable $action, array|WaitForPopupOptions $options = []): self;
222223

224+
/**
225+
* @param string|callable $url
226+
* @param array<string, mixed>|WaitForResponseOptions $options
227+
*/
228+
public function waitForResponse($url, array|WaitForResponseOptions $options = []): ResponseInterface;
229+
223230
/**
224231
* Set files to an input element with type="file".
225232
*
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the community-maintained Playwright PHP project.
7+
* It is not affiliated with or endorsed by Microsoft.
8+
*
9+
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Playwright\Tests\Unit\Page\Options;
16+
17+
use PHPUnit\Framework\Attributes\CoversClass;
18+
use PHPUnit\Framework\TestCase;
19+
use Playwright\Page\Options\WaitForResponseOptions;
20+
21+
#[CoversClass(WaitForResponseOptions::class)]
22+
final class WaitForResponseOptionsTest extends TestCase
23+
{
24+
public function testCanBeCreatedFromConstructor(): void
25+
{
26+
$options = new WaitForResponseOptions(timeout: 1000.0);
27+
28+
$this->assertSame(1000.0, $options->timeout);
29+
$this->assertEquals(['timeout' => 1000.0], $options->toArray());
30+
}
31+
32+
public function testCanBeCreatedFromArray(): void
33+
{
34+
$options = WaitForResponseOptions::from(['timeout' => 1000.0]);
35+
36+
$this->assertSame(1000.0, $options->timeout);
37+
}
38+
39+
public function testCanBeCreatedFromSelf(): void
40+
{
41+
$original = new WaitForResponseOptions(timeout: 1000.0);
42+
$options = WaitForResponseOptions::from($original);
43+
44+
$this->assertSame($original, $options);
45+
}
46+
}

0 commit comments

Comments
 (0)