-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathAwaitableWebpage.php
More file actions
85 lines (74 loc) · 2.2 KB
/
AwaitableWebpage.php
File metadata and controls
85 lines (74 loc) · 2.2 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
<?php
declare(strict_types=1);
namespace Pest\Browser\Api;
use Pest\Browser\Exceptions\BrowserExpectationFailedException;
use Pest\Browser\Execution;
use Pest\Browser\Playwright\Page;
use Pest\Browser\Playwright\Playwright;
use Pest\Browser\ServerManager;
use PHPUnit\Framework\ExpectationFailedException;
use Throwable;
/**
* @mixin Webpage
*/
final readonly class AwaitableWebpage
{
/**
* Creates a new awaitable webpage instance.
*
* @param array<int, string> $nonAwaitableMethods
*/
public function __construct(
private Page $page,
private string $initialUrl,
private array $nonAwaitableMethods = [
'assertScreenshotMatches',
'assertNoAccessibilityIssues',
'saveStorageState',
],
) {
//
}
/**
* Awaits for the given method to assert true or fail.
*
* @param array<int, mixed> $arguments
*/
public function __call(string $name, array $arguments): mixed
{
$webpage = new Webpage($this->page, $this->initialUrl);
try {
if (
in_array($name, $this->nonAwaitableMethods, true)
|| Playwright::timeout() <= 1000
) {
// @phpstan-ignore-next-line
$result = $webpage->{$name}(...$arguments);
} else {
$result = Execution::instance()->waitForExpectation(
// @phpstan-ignore-next-line
fn () => $webpage->{$name}(...$arguments),
);
}
} catch (ExpectationFailedException $e) {
ServerManager::instance()->http()->throwLastThrowableIfNeeded();
try {
$browserException = BrowserExpectationFailedException::from($this->page, $e);
} catch (Throwable) { // @phpstan-ignore-line
throw $e;
}
throw $browserException;
}
ServerManager::instance()->http()->throwLastThrowableIfNeeded();
return $result === $webpage
? $this
: $result;
}
/**
* Return the page instance.
*/
public function page(): Page
{
return $this->page;
}
}