-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathInitScript.php
More file actions
62 lines (54 loc) · 1.82 KB
/
InitScript.php
File metadata and controls
62 lines (54 loc) · 1.82 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
<?php
declare(strict_types=1);
namespace Pest\Browser\Playwright;
/**
* @internal
*/
final class InitScript
{
/**
* Get the JavaScript code for the initialization script.
*/
public static function get(): string
{
$axe = (string) file_get_contents(
dirname(__DIR__, 2).'/resources/js/axe.min.js'
);
return <<<JS
$axe
window.__pestBrowser = {
jsErrors: [],
consoleLogs: [],
consoleMessages: []
};
const originalConsoleLog = console.log;
console.log = function(...args) {
window.__pestBrowser.consoleLogs.push({
timestamp: new Date().getTime(),
message: args.map(arg => String(arg)).join(' ')
});
originalConsoleLog.apply(console, args);
};
['debug', 'error', 'info', 'warning'].forEach(function (level) {
const methodName = level === 'warning' ? 'warn' : level;
const originalConsoleMethod = console[methodName];
console[methodName] = function(...args) {
window.__pestBrowser.consoleMessages.push({
timestamp: new Date().getTime(),
level: level,
message: args.map(arg => String(arg)).join(' ')
});
originalConsoleMethod.apply(console, args);
};
});
window.addEventListener('error', (e) => {
window.__pestBrowser.jsErrors.push({
message: e.message,
filename: e.filename,
lineno: e.lineno,
colno: e.colno
});
});
JS;
}
}