-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathMakesConsoleAssertions.php
More file actions
152 lines (122 loc) · 4.54 KB
/
MakesConsoleAssertions.php
File metadata and controls
152 lines (122 loc) · 4.54 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
<?php
declare(strict_types=1);
namespace Pest\Browser\Api\Concerns;
use InvalidArgumentException;
use Pest\Browser\Api\Webpage;
use Pest\Browser\Enums\AccessibilityIssueLevel;
use Pest\Browser\Playwright\Playwright;
use Pest\Browser\Support\AccessibilityFormatter;
/**
* @mixin Webpage
*
* @phpstan-import-type Violations from AccessibilityFormatter
*/
trait MakesConsoleAssertions
{
/**
* Asserts there are no console logs or JavaScript errors on the page.
*/
public function assertNoSmoke(): Webpage
{
$this->assertNoConsoleLogs();
$this->assertNoJavaScriptErrors();
return $this;
}
/**
* Asserts there are no broken images on the page.
*/
public function assertNoBrokenImages(): Webpage
{
$this->page->waitForLoadState('load');
$brokenImages = $this->page->brokenImages();
expect($brokenImages)->toBeEmpty(sprintf(
'Expected no broken images on the page initially with the url [%s], but found %s: %s',
$this->initialUrl,
count($brokenImages),
implode(', ', $brokenImages),
));
return $this;
}
/**
* Asserts there are no missing images on the page.
*/
public function assertNoMissingImages(): Webpage
{
return $this->assertNoBrokenImages();
}
/**
* Asserts there are no console logs on the page.
*/
public function assertNoConsoleLogs(): Webpage
{
$consoleLogs = $this->page->consoleLogs();
expect($consoleLogs)->toBeEmpty(sprintf(
'Expected no console logs on the page initially with the url [%s], but found %s: %s',
$this->initialUrl,
count($consoleLogs),
implode(', ', array_map(fn (array $log) => $log['message'], $consoleLogs)),
));
return $this;
}
/**
* Asserts there are no console messages at given levels on the page.
*
* @param array<int, string>|string $levels Console message level(s) to check for (debug, info, error, warning).
*/
public function assertNoConsoleMessages(array|string $levels = ['info', 'error', 'warning']): Webpage
{
if (is_string($levels)) {
$levels = [$levels];
}
$messages = $this->page->consoleMessages($levels);
expect($messages)->toBeEmpty(sprintf(
'Expected no console messages at levels [%s] on the page initially with the url [%s], but found %d: %s',
implode(', ', $levels),
$this->initialUrl,
count($messages),
implode(', ', array_map(fn (array $log): string => "{$log['level']}: {$log['message']}", $messages)),
));
return $this;
}
/**
* Asserts there are no JavaScript errors on the page.
*/
public function assertNoJavaScriptErrors(): Webpage
{
$javaScriptErrors = $this->page->javaScriptErrors();
expect($javaScriptErrors)->toBeEmpty(sprintf(
'Expected no JavaScript errors on the page initially with the url [%s], but found %s: %s',
$this->initialUrl,
count($javaScriptErrors),
implode(', ', array_map(fn (array $log) => $log['message'], $javaScriptErrors)),
));
return $this;
}
/**
* Asserts the accessibility of the page.
*/
public function assertNoAccessibilityIssues(int $level = 1): Webpage
{
$this->page->waitForLoadState('networkidle');
$this->page->waitForFunction('document.readyState === "complete"');
$level = AccessibilityIssueLevel::tryFromLevel($level);
if (! $level instanceof AccessibilityIssueLevel) {
throw new InvalidArgumentException(
'The accessibility issue level must be between [0] (critical) and [3] (minor).',
);
}
/** @var Violations|null $violations */
$violations = Playwright::usingTimeout(5_000, fn () => $this->page->evaluate('async () => ((await window.axe.run()).violations)'));
if (! is_array($violations)) {
$violations = [];
}
$violations = array_filter($violations, function (array $violation) use ($level): bool {
$violationImpact = $violation['impact'] ?? null;
$violationRank = is_string($violationImpact) ? AccessibilityIssueLevel::from($violationImpact)->level() : -1;
return $violationRank <= $level->level();
});
$report = AccessibilityFormatter::format($violations);
expect($violations)->toBeEmpty($report);
return $this;
}
}