-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.js
139 lines (112 loc) · 4.1 KB
/
test.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
const fs = require('fs');
const path = require('path');
const EC = require('eight-colors');
const rimraf = require('rimraf');
const assert = require('assert');
const PCR = require(path.resolve(__dirname, '../lib/index.js'));
// utils
const cleanSnapshotsDir = (options) => {
const snapshotsDir = options.snapshotsDir;
if (fs.existsSync(snapshotsDir)) {
console.log(`${EC.cyan('[clean]')} remove chromium snapshots dir: ${EC.magenta(snapshotsDir)} ...`);
rimraf.nativeSync(snapshotsDir);
}
};
const cleanStatsFile = (options) => {
const statsPath = path.resolve(options.cacheDir, options.statsName);
if (fs.existsSync(statsPath)) {
console.log(`${EC.cyan('[clean]')} remove stats cache: ${EC.magenta(statsPath)} ...`);
rimraf.nativeSync(statsPath);
}
};
const cleanAll = (options) => {
cleanSnapshotsDir(options);
cleanStatsFile(options);
};
describe('puppeteer-chromium-resolver', function() {
this.timeout(5 * 60 * 1000);
it('reinstall with default options', async () => {
const mergedOptions = PCR.getOptions();
cleanAll(mergedOptions);
const options = {};
const stats = await PCR(options);
assert(fs.existsSync(stats.executablePath));
});
it('async PCR with default options', async () => {
const options = {};
const stats = await PCR(options);
assert(fs.existsSync(stats.executablePath));
});
it('sync getStats', () => {
const stats = PCR.getStats();
assert(fs.existsSync(stats.executablePath));
});
it('async PCR without stats cache', async () => {
const mergedOptions = PCR.getOptions();
cleanStatsFile(mergedOptions);
const options = {};
const stats = await PCR(options);
assert(fs.existsSync(stats.executablePath));
});
it('async PCR with revision: 1337728', async () => {
const options = {
revision: '1337728'
};
const stats = await PCR(options);
assert(fs.existsSync(stats.executablePath));
});
it('sync getStats with revision: 1337728', () => {
const options = {
revision: '1337728'
};
const stats = PCR.getStats(options);
assert(fs.existsSync(stats.executablePath));
});
it('async PCR with downloadPath: .temp', async () => {
const options = {
downloadPath: '.temp'
};
const mergedOptions = PCR.getOptions(options);
cleanAll(mergedOptions);
const stats = await PCR(options);
assert(fs.existsSync(stats.executablePath));
});
it('sync getStats with downloadPath: .temp', () => {
const options = {
downloadPath: '.temp'
};
const stats = PCR.getStats(options);
assert(fs.existsSync(stats.executablePath));
});
it('async PCR with detectionPath: .temp', async () => {
const options = {
detectionPath: '.temp'
};
const stats = await PCR(options);
assert(fs.existsSync(stats.executablePath));
});
it('launch browser and open page', async () => {
const stats = await PCR();
console.log('puppeteerVersion', stats.puppeteerVersion);
console.log('executablePath', stats.executablePath);
const browser = await stats.puppeteer.launch({
// headless: 'new',
// headless: false,
// args: ['--no-sandbox'],
executablePath: stats.executablePath
}).catch(function(err) {
console.error(err);
});
console.log('browser.newPage ...');
const page = await browser.newPage();
console.log('page.setContent ...');
await page.setContent('<html><head><title>puppeteer-chromium-resolver</title></head><body></body></html>');
console.log('check head title ...');
const title = await page.$eval('head title', (el) => el.innerText);
assert.equal(title, 'puppeteer-chromium-resolver');
console.log('browser.close ...');
await browser.close().catch(function(err) {
console.error(err);
});
});
});