-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathwpt.ts
142 lines (121 loc) · 4.86 KB
/
wpt.ts
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
// Run WPT manually before calling this script
import { JSDOM, VirtualConsole } from 'jsdom';
import { TextEncoder, TextDecoder } from 'util';
import puppeteer, { Browser } from 'puppeteer';
import ndcPolyfill from '../../src/polyfill/index';
export interface TestResult {
name: string;
message: string;
status: number // 0: PASS, 1: FAIL, 2: TIMEOUT, 3: PRECONDITION_FAILED
}
export interface WptTestResult {
test: string;
result: TestResult[];
}
export async function runWptTests(wptTestList: string[], _forChrome = false, _wptServerUrl = 'http://web-platform.test:8000'): Promise<WptTestResult[]> {
let browser: Browser = null;
const results: WptTestResult[] = [];
if (_forChrome)
browser = await puppeteer.launch({
headless: true,
devtools: true,
});
// call runTest for each test path
for (let i = 0; i < wptTestList.length; i++) {
console.log(`Running test: ${wptTestList[i]} `);
const path = `${_wptServerUrl}${wptTestList[i]}`;
const result: TestResult[] = _forChrome ? await runTestForChrome(browser, path) : await runTestForLibrary(path);
results.push({ test: wptTestList[i], result });
// sleep for 1 second
// await new Promise((resolve) => setTimeout(resolve, 1000));
}
// close the client
if (_forChrome) await browser.close();
return results;
}
function runTestForLibrary(filePath: string): Promise<TestResult[]> {
// return new promise
return new Promise((resolve) => {
const virtualConsole = new VirtualConsole();
virtualConsole.sendTo(console);
JSDOM.fromURL(filePath, {
runScripts: 'dangerously',
resources: 'usable',
pretendToBeVisual: true,
virtualConsole,
beforeParse(window: any) {
// Assign the polyfill to the window object
Object.assign(window, ndcPolyfill);
// Overwrite the DOMException object
window.DOMException = DOMException;
window.TypeError = TypeError;
window.TextEncoder = TextEncoder;
window.TextDecoder = TextDecoder;
window.Uint8Array = Uint8Array;
window.ArrayBuffer = ArrayBuffer;
},
}).then((dom: any) => {
// Get the window object from the DOM
const { window } = dom;
window.addEventListener('load', () => {
window.add_completion_callback((results) => {
window.close();
// Meaning of status
// 0: PASS (test passed)
// 1: FAIL (test failed)
// 2: TIMEOUT (test timed out)
// 3: PRECONDITION_FAILED (test skipped)
const returnObject = [];
for (let i = 0; i < results.length; i++) {
returnObject.push({
name: results[i].name,
message: results[i].message,
status: results[i].status,
});
}
return resolve(returnObject);
});
});
});
});
}
async function runTestForChrome(browser: Browser, filePath: string): Promise<TestResult[]> {
const page = await browser.newPage();
// Evaluate the script in the page context
await page.evaluateOnNewDocument(() => {
function createDeferredPromise(): Promise<any> {
let resolve: any, reject: any;
const promise = new Promise(function (_resolve, _reject) {
resolve = _resolve;
reject = _reject;
});
(promise as any).resolve = resolve;
(promise as any).reject = reject;
return promise;
}
window.addEventListener('load', () => {
(window as any).resultPromise = createDeferredPromise();
(window as any).add_completion_callback((results) => {
// window.returnTestResults.push({ name: test.name, message: test.message, status: test.status });
const returnTestResults = [];
for (let i = 0; i < results.length; i++) {
returnTestResults.push({
name: results[i].name,
message: results[i].message,
status: results[i].status,
});
}
(window as any).resultPromise.resolve(returnTestResults);
});
});
});
// Navigate to the specified URL
await page.goto(filePath, { waitUntil: 'load' });
// get the results
const results = await page.evaluate(() => {
return (window as any).resultPromise;
});
// close the page
await page.close();
return results;
}