-
Notifications
You must be signed in to change notification settings - Fork 1
/
1054.js
66 lines (53 loc) · 1.37 KB
/
1054.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
const Nightmare = require('nightmare')
const express = require('express')
const http = require('http')
main().catch(console.error)
function html(body, head) {
return `<!doctype><html><head><meta charset="UTF-8">${head ||
''}</head><body>${body}</body></html>`
}
async function main() {
const app = express()
const server = await Server(app)
app.get('/', (req, res) => {
res.send(
html(
`<h2>hello world</h2>`,
`<meta http-equiv="refresh" content="1;url=${server.url}/">`
)
)
})
const nightmare = Nightmare({
loadTimeout: 45 * 1000,
waitTimeout: 10 * 1000,
show: true
})
await nightmare.goto(server.url, '/')
console.log(
await nightmare.evaluate(() => document.documentElement.outerHTML)
)
await nightmare.wait(5000)
console.log(
await nightmare.evaluate(() => document.documentElement.outerHTML)
)
await nightmare.end()
await server.close()
}
async function Server(handler) {
const server = await new Promise((res, _rej) => {
var s = http.createServer(handler)
s.listen(0, 'localhost', function() {
res(s)
})
})
const address = server.address()
return {
url: `http://${address.address}:${address.port}`,
close: function() {
return new Promise((res, _rej) => server.close(res))
}
}
}
function sleep(ms) {
return new Promise(res => setTimeout(res, ms))
}