-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathBrowserExpectationFailedException.php
More file actions
75 lines (60 loc) · 2.41 KB
/
BrowserExpectationFailedException.php
File metadata and controls
75 lines (60 loc) · 2.41 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
<?php
declare(strict_types=1);
namespace Pest\Browser\Exceptions;
use Pest\Browser\Playwright\Page;
use Pest\Browser\Playwright\Playwright;
use Pest\Browser\ServerManager;
use PHPUnit\Framework\ExpectationFailedException;
use Throwable;
/**
* @internal
*/
final class BrowserExpectationFailedException
{
/**
* Creates a new browser expectation failed exception instance.
*/
public static function from(Page $page, ExpectationFailedException $e): ExpectationFailedException
{
$message = $e->getMessage();
if (Playwright::shouldDebugAssertions() === false && str_contains($message, 'Screenshot does not match the last one.') === false) {
$filename = $page->screenshot();
if ($filename !== null) {
$message .= " A screenshot of the page has been saved to [Tests/Browser/Screenshots/$filename].";
}
}
$consoleLogs = $page->consoleLogs();
if (count($consoleLogs) > 0) {
$message .= "\n\nThe following console logs were found:\n".implode("\n", array_map(
fn (array $error): string => '- '.$error['message'],
$consoleLogs,
));
}
$consoleMessages = $page->consoleMessages();
if (count($consoleMessages) > 0) {
$message .= "\n\nThe following console messages were found:\n".implode("\n", array_map(
fn (array $error): string => $error['level'] . ': ' . $error['message'],
$consoleMessages,
));
}
$javaScriptErrors = $page->javaScriptErrors();
if (count($javaScriptErrors) > 0) {
$message .= "\n\nThe following JavaScript errors were found:\n".implode("\n", array_map(
fn (array $error): string => '- '.$error['message'],
$javaScriptErrors,
));
}
$httpThrowable = ServerManager::instance()->http()->lastThrowable();
if ($httpThrowable instanceof Throwable) {
$message .= "\n\nThe following exception was thrown by the HTTP server:\n"
.$httpThrowable::class.': '.$httpThrowable->getMessage()
."\n\nStack trace:\n"
.$httpThrowable->getTraceAsString();
}
return new ExpectationFailedException(
$message,
$e->getComparisonFailure(),
$e->getPrevious(), // @phpstan-ignore-line
);
}
}