-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrunner.test.js
executable file
·146 lines (127 loc) · 4.27 KB
/
runner.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
140
141
142
143
144
145
146
'use strict';
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const { Runner, launch, launchWithoutNoise, launchWithHeadless } = require('../index');
process.on('unhandledRejection', console.trace);
async function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
describe('Runner', function () {
this.timeout(5000);
it('set and get port', async function () {
this.timeout(10000);
const runner = await launchWithHeadless({
port: 4577,
});
assert.notEqual(runner.chromeProcess, null);
assert.equal(runner.port, 4577);
return await runner.kill();
});
it('use Runner to launch() then kill() and ensure chromeDataDirPrepared event emit', async function () {
const runner = new Runner({
chromeFlags: ['--headless', '--disable-gpu']
});
runner.once('chromeDataDirPrepared', console.log);
await runner.launch();
return await runner.kill();
});
it.skip('launch() then kill()', async function () {
const runner = await launch();
assert.notEqual(runner.chromeProcess, null);
return await runner.kill();
});
it.skip('launchWithoutNoise() then kill()', async function () {
const runner = await launchWithoutNoise();
assert.notEqual(runner.chromeProcess, null);
assert.notEqual(runner.port, null);
return await runner.kill();
});
it('launchWithHeadless() then kill()', async function () {
const runner = await launchWithHeadless();
assert.notEqual(runner.chromeProcess, null);
assert.notEqual(runner.port, null);
return await runner.kill();
});
it('set chromeDataDir option', async function () {
const chromeDataDir = path.resolve(__dirname, '../chrome_runner_test');
const runner = await launchWithHeadless({
chromeDataDir,
});
assert.equal(runner.chromeDataDir, chromeDataDir);
return await runner.kill();
});
it('restart chrome when chrome exit unexpected', async function () {
this.timeout(8000);
const runner = await launchWithHeadless();
process.kill(runner.chromeProcess.pid);
await delay(2000);
process.kill(runner.chromeProcess.pid);
await delay(2000);
process.kill(runner.chromeProcess.pid);
await delay(2000);
await runner.kill();
});
it('after kill() all tmp file should be removed', async function () {
const runner = await launchWithHeadless();
const chromeDataDir = runner.chromeDataDir;
assert.notEqual(runner.chromeProcess, null);
await runner.kill();
// TODO windows removed tmp dir failed
if (process.platform !== 'win32') {
assert.equal(fs.existsSync(chromeDataDir), false, `tmp dir ${chromeDataDir} should be removed`);
}
});
it('emit chromeDataDirRemoved after kill', function (done) {
launchWithHeadless().then(async (runner) => {
// TODO windows removed tmp dir failed
if (process.platform !== 'win32') {
runner.once('chromeDataDirRemoved', (chromeDataDir) => {
console.log(chromeDataDir);
done();
});
} else {
done();
}
await runner.kill();
});
});
it('should emit chromeAlive event then chromeRestarted event when chrome exit unexpected', function (done) {
launchWithHeadless().then((runner) => {
let chromeDead;
runner.once('chromeDead', async () => {
chromeDead = true;
});
runner.once('chromeRestarted', async () => {
if (chromeDead) {
await runner.kill();
done();
}
});
process.kill(runner.chromeProcess.pid);
});
});
it('set monitorInterval should emit chromeAlive event interval', function (done) {
this.timeout(7000);
launchWithHeadless({
monitorInterval: 1000,
}).then((runner) => {
let times = 0;
runner.on('chromeAlive', async () => {
times++;
if (times >= 5) {
await runner.kill();
done();
}
});
});
});
it('set disableLogging option', async function () {
const runner = await launchWithHeadless({
disableLogging: true,
});
assert.equal(fs.existsSync(path.join(runner.chromeDataDir, 'chrome-err.log')), false);
assert.equal(fs.existsSync(path.join(runner.chromeDataDir, 'chrome-out.log')), false);
return await runner.kill();
});
});