-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathbrowser.ts
More file actions
86 lines (74 loc) · 2.38 KB
/
browser.ts
File metadata and controls
86 lines (74 loc) · 2.38 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
76
77
78
79
80
81
82
83
84
85
86
import { logger } from '@twilio/flex-dev-utils';
import Puppeteer, { Browser as PuppeteerBrowser, Page, ConsoleMessage, ConsoleMessageType } from 'puppeteer';
import assertion from './assertion';
import { App, BaseUrl } from './pages';
export class Browser {
static app: App;
private static _browser: PuppeteerBrowser;
private static _page: Page;
private static _domainsToInclude = ['twilio', 'localhost', 'unpkg'];
/**
* Initializes browser object
*/
static async create(baseUrls: BaseUrl): Promise<void> {
this._browser = await Puppeteer.launch({
headless: true,
protocolTimeout: 300000,
args: [
'--use-fake-ui-for-media-stream',
'--disable-features=site-per-process',
'--no-sandbox',
'--disable-setuid-sandbox',
],
});
this._page = await this._browser.newPage();
await this._page.setRequestInterception(true);
this._attachLogListener();
this._attachNetworkInterceptor();
this.app = new App(this._page, baseUrls);
assertion.app.init(this.app);
}
static async kill(): Promise<void> {
logger.info('Finally called');
try {
this._page.removeAllListeners();
await this._browser.close();
} catch (e) {
logger.error('Failed to quit browser');
}
}
/**
* Attach browser log listener
*/
private static _attachNetworkInterceptor(): void {
this._page.on('request', async (request): Promise<void> => {
// Ignoring any 3rd part requests to speed up Twilio console load time
const url = request.url();
const re = new RegExp(this._domainsToInclude.join('|'));
if (request.resourceType() === 'script' && !re.test(url)) {
return request.abort();
}
return request.continue();
});
}
/**
* Attach network interceptor
*/
private static _attachLogListener(): void {
this._page.on('console', (msg: ConsoleMessage) => {
const logTypes: ConsoleMessageType[] = ['error'];
const url = msg.location().url || '';
const re = new RegExp(this._domainsToInclude.join('|'));
if (logTypes.includes(msg.type()) && re.test(url)) {
logger.error({
msg: msg.text(),
url,
args: msg.args() || '',
lineNumber: msg.location().lineNumber,
colNumber: msg.location().columnNumber,
stackTrace: msg.stackTrace() || '',
});
}
});
}
}