This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
test-utils.js
77 lines (64 loc) · 1.58 KB
/
test-utils.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
/*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
"use strict";
import puppeteer from "puppeteer";
import puppeteerApi from "puppeteer/lib/api.js";
puppeteerApi.ElementHandle.prototype.getHref = function()
{
return this.evaluate(e => e.href);
};
export async function launchBrowser()
{
return await puppeteer.launch({
args: [
"--load-extension=crx-unpacked/",
"--no-pings",
// Google won't mangle URLs in current Chrome versions, relying on pings instead
"--user-agent=Firefox/40.0"
],
ignoreDefaultArgs: [
"--disable-extensions"
],
// Extensions are unsupported in headless Chrome, see
// https://bugs.chromium.org/p/chromium/issues/detail?id=706008
headless: false
});
}
export async function logTopLevelRequests(page, handler)
{
let requests = [];
function logRequest(request)
{
if (request.resourceType() == "document")
requests.push(request.url());
request.continue();
}
await page.setRequestInterception(true);
page.on("request", logRequest);
try
{
await handler();
}
finally
{
page.off("request", logRequest);
await page.setRequestInterception(false);
}
return requests;
}
export function newTarget(browser, handler)
{
return new Promise((resolve, reject) =>
{
function onNewTarget(target)
{
browser.off("targetcreated", onNewTarget);
resolve(target);
}
browser.on("targetcreated", onNewTarget);
handler();
});
}