-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbrowser.js
More file actions
48 lines (43 loc) · 1.18 KB
/
browser.js
File metadata and controls
48 lines (43 loc) · 1.18 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
const puppeteer = require('puppeteer');
/**
* This is a thin wrapper so that we use a singleton of
* the browser that puppeteer creates
*/
class Browser {
setUp(done) {
const puppeteerOpts = this.options && this.options.puppeteer ?
this.options.puppeteer :
{};
puppeteer.launch(puppeteerOpts).then(async b => {
this.setBrowser(b);
done();
});
}
setBrowser(b) {
this.browser = b;
const oldNewPage = this.browser.newPage.bind(this.browser);
this.browser.newPage = async function () {
const page = await oldNewPage();
this.lastPage = page;
return page;
};
}
setOptions(opts) {
this.options = opts;
}
test(promise) {
return (done) => {
promise(this.browser, this.options)
.then(() => done()).catch(done);
};
}
}
/*
* Create a new browser and use a proxy to pass
* any puppeteer calls to the inner browser
*/
module.exports = new Proxy(new Browser(), {
get: function (target, name) {
return name in target ? target[name].bind(target) : target.browser[name];
}
});