-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
174 lines (139 loc) · 4.76 KB
/
index.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const fs = require('fs');
const { promisify } = require('util');
const Heapsnapshot = require('heapsnapshot');
const index = require('chrome-debugging-client');
const protocol = require('chrome-debugging-client/dist/protocol/tot');
if (process.argv.length < 3) {
console.error(`${process.argv[1]} [url]`);
}
const additionalArguments = [
'--ignore-certificate-errors',
];
const readAsync = promisify(fs.readFile);
runQUnitTestsWithSnapshots(process.argv[2]).catch((err) => {
console.error(err);
});
function runQUnitTestsWithSnapshots(url) {
return index.createSession(async (session) => {
const codeToInject = await readAsync('./qunit-injection-code.js', 'utf8');
const browser = await session.spawnBrowser('canary', { additionalArguments });
const apiClient = session.createAPIClient('localhost', browser.remoteDebuggingPort);
const [ tab ] = await apiClient.listTabs();
const client = await session.openDebuggingProtocol(tab.webSocketDebuggerUrl);
const page = new protocol.Page(client);
const runtime = new protocol.Runtime(client);
const heapProfiler = new protocol.HeapProfiler(client);
const debug = new protocol.Debugger(client);
const cons = new protocol.Console(client);
cons.messageAdded = (evt) => {
console.log(evt.message.level, evt.message.text);
};
const scriptMap = new Map();
debug.scriptParsed = (evt) => {
scriptMap.set(evt.scriptId, evt);
};
debug.scriptFailedToParse = (evt) => {
scriptMap.set(evt.scriptId, evt);
}
await Promise.all([page.enable(), debug.enable(), runtime.enable(), heapProfiler.enable(), cons.enable()]);
const contextCreated = eventPromise(runtime, 'executionContextCreated').then((evt) => {
const contextId = evt.context.id;
return runtime.evaluate({
contextId,
expression: `window.QUnit = { config: { autostart: false } };`,
returnByValue: true
}).then(() => contextId);
});
const pageLoad = eventPromise(page, 'loadEventFired');
await page.navigate({ url });
const contextId = await contextCreated;
await pageLoad;
if (!await isQUnitAvailable(runtime, contextId)) {
throw new Error('QUnit is not available on this page!');
}
const scriptId = await compileScript(runtime, codeToInject, 'http://localhost:7357/__pageload.js', contextId);
await runtime.runScript({
scriptId: scriptId,
});
while (true) {
let result = await runtime.evaluate({
contextId,
expression: `__resumeTest()`,
awaitPromise: true
});
let isDone = toBoolean(result);
if (isDone) break;
const snapshot = await takeHeapSnapshot(heapProfiler);
for (const Container of findObjectsInSnapshot(snapshot, 'Container')) {
console.log('leaked Container via:');
try {
const path = Heapsnapshot.pathToRoot(Container);
path.forEach((node) => console.log(node.name, node.in));
} catch(err) {
if (/has no path to root/.test(err.message)) {
console.log("Couldn't find path to root!");
} else {
throw err;
}
}
}
}
});
}
async function isQUnitAvailable(runtime, contextId) {
const hasQUnit = await runtime.evaluate({
contextId,
expression: `!!window.QUnit.start`,
awaitPromise: true
});
return toBoolean(hasQUnit);
}
async function takeHeapSnapshot(heapProfiler) {
await heapProfiler.collectGarbage();
let buffer = '';
heapProfiler.addHeapSnapshotChunk = (params) => {
buffer += params.chunk;
};
heapProfiler.reportHeapSnapshotProgress = (params) => {
console.log(params.done / params.total + "");
};
await heapProfiler.takeHeapSnapshot({ reportProgress: true });
return new Heapsnapshot(JSON.parse(buffer));
}
function *findObjectsInSnapshot(snapshot, name) {
for (const node of snapshot) {
if (node.type === 'object' && node.name === name) {
yield node;
}
}
}
function toBoolean(evalReturn) {
if (evalReturn.exceptionDetails) {
throw new Error(JSON.stringify(evalReturn.exceptionDetails));
}
const result = evalReturn.result;
if (result.type !== 'boolean') {
throw new Error(`expected result to be boolean but was ${JSON.stringify(result)}`);
}
return result.value;
}
function eventPromise(domain, eventName) {
return new Promise(resolve => {
domain[eventName] = (evt) => {
domain[eventName] = null;
resolve(evt);
}
});
}
async function compileScript(runtime, expression, sourceURL, executionContextId) {
const result = await runtime.compileScript({
expression,
sourceURL,
persistScript: true,
executionContextId
});
if (result.exceptionDetails) {
throw new Error(JSON.stringify(result.exceptionDetails));
}
return result.scriptId;
}