Skip to content

Commit bdf7f4d

Browse files
committed
chore: use playwright
1 parent e83dc7d commit bdf7f4d

File tree

6 files changed

+52
-181
lines changed

6 files changed

+52
-181
lines changed

examples/libp2p-in-the-browser/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"babel-plugin-syntax-async-functions": "^6.13.0",
3030
"babel-plugin-transform-regenerator": "^6.26.0",
3131
"babel-polyfill": "^6.26.0",
32-
"p-retry": "^4.2.0",
3332
"parcel-bundler": "^1.12.4"
3433
}
3534
}
+45-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
11
'use strict'
22

3-
const pkg = require('./package.json')
3+
const execa = require('execa')
4+
const { chromium } = require('playwright');
45

5-
module.exports = {
6-
[pkg.name]: function (browser) {
7-
browser
8-
.url(process.env.LIBP2P_EXAMPLE_TEST_URL)
9-
.waitForElementVisible('#status')
10-
.waitForElementVisible('#output')
11-
.pause(5000)
6+
async function run() {
7+
let url = ''
8+
const proc = execa('parcel', ['./index.html'], {
9+
preferLocal: true,
10+
localDir: __dirname,
11+
cwd: __dirname,
12+
all: true
13+
})
1214

13-
browser.expect.element('#status').text.to.contain('libp2p started!')
14-
browser.expect.element('#output').text.to.contain('libp2p id is')
15+
proc.all.on('data', async (chunk) => {
16+
/**@type {string} */
17+
const out = chunk.toString()
1518

16-
browser.expect.element('#output').text.to.contain('Found peer')
17-
browser.expect.element('#output').text.to.contain('Connected to')
19+
if (out.includes('Server running at')) {
20+
url = out.replace('Server running at ', '')
21+
}
22+
23+
if (out.includes('✨ Built in ')) {
24+
try {
25+
const browser = await chromium.launch();
26+
const page = await browser.newPage();
27+
await page.goto(url);
28+
await page.waitForFunction(selector => document.querySelector(selector).innerText === 'libp2p started!', '#status')
29+
await page.waitForFunction(
30+
selector => {
31+
const text = document.querySelector(selector).innerText
32+
return text.includes('libp2p id is') &&
33+
text.includes('Found peer') &&
34+
text.includes('Connected to')
35+
},
36+
'#output',
37+
{ timeout: 5000 }
38+
)
39+
await browser.close();
40+
41+
} catch (err) {
42+
console.error(err)
43+
process.exit(1)
44+
} finally {
45+
proc.cancel()
46+
}
47+
}
48+
})
1849

19-
browser.end()
20-
}
2150
}
51+
52+
module.exports = run

examples/nightwatch.conf.js

-35
This file was deleted.

examples/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
},
99
"license": "MIT",
1010
"dependencies": {
11-
"chromedriver": "^87.0.0",
1211
"execa": "^2.1.0",
1312
"fs-extra": "^8.1.0",
1413
"p-defer": "^3.0.0",
15-
"http-server": "~0.11.1",
16-
"nightwatch": "^1.2.4",
1714
"which": "^2.0.1"
15+
},
16+
"devDependencies": {
17+
"playwright": "^1.7.1"
1818
}
1919
}

examples/test.js

+4-36
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const fs = require('fs-extra')
77
const path = require('path')
88
const execa = require('execa')
99
const dir = path.join(__dirname, process.argv[2])
10-
const { startServer } = require('./utils')
1110

1211
testExample(dir)
1312
.then(() => {}, (err) => {
@@ -22,12 +21,7 @@ testExample(dir)
2221
async function testExample (dir) {
2322
await installDeps(dir)
2423
await build(dir)
25-
26-
if (dir.includes('browser')) {
27-
await runBrowserTest(dir)
28-
} else {
29-
await runNodeTest(dir)
30-
}
24+
await runTest(dir)
3125
}
3226

3327
async function installDeps (dir) {
@@ -85,7 +79,7 @@ async function build (dir) {
8579
await proc
8680
}
8781

88-
async function runNodeTest (dir) {
82+
async function runTest (dir) {
8983
console.info('Running node tests in', dir)
9084
const testFile = path.join(dir, 'test.js')
9185

@@ -94,33 +88,7 @@ async function runNodeTest (dir) {
9488
return
9589
}
9690

97-
const runTest = require(testFile)
98-
99-
await runTest()
100-
}
101-
102-
async function runBrowserTest (dir) {
103-
console.info('Running browser tests in', dir)
104-
105-
const server = await startServer(dir)
91+
const test = require(testFile)
10692

107-
console.info('Running tests at', server.url)
108-
109-
const proc = execa('nightwatch', [ path.join(dir, 'test.js') ], {
110-
cwd: __dirname,
111-
env: {
112-
...process.env,
113-
LIBP2P_EXAMPLE_TEST_URL: server.url
114-
}
115-
})
116-
117-
proc.all.on('data', (data) => {
118-
process.stdout.write(data)
119-
})
120-
121-
try {
122-
await proc
123-
} finally {
124-
server.stop()
125-
}
93+
await test()
12694
}

examples/utils.js

-92
Original file line numberDiff line numberDiff line change
@@ -3,96 +3,6 @@
33
const execa = require('execa')
44
const fs = require('fs-extra')
55
const which = require('which')
6-
const path = require('path')
7-
8-
async function startServer (dir) {
9-
async function serveFrom (path) {
10-
return new Promise((resolve, reject) => {
11-
let output = ''
12-
13-
const proc = execa.command(`http-server ${path} -a 127.0.0.1`, {
14-
cwd: __dirname
15-
})
16-
proc.all.on('data', (data) => {
17-
process.stdout.write(data)
18-
19-
const line = data.toString('utf8')
20-
output += line
21-
22-
if (output.includes('Hit CTRL-C to stop the server')) {
23-
// find the port
24-
const port = output.match(/http:\/\/127.0.0.1:(\d+)/)[1]
25-
26-
if (!port) {
27-
throw new Error(`Could not find port in ${output}`)
28-
}
29-
30-
resolve({
31-
stop: () => {
32-
console.info('Stopping server')
33-
proc.kill('SIGINT', {
34-
forceKillAfterTimeout: 2000
35-
})
36-
},
37-
url: `http://127.0.0.1:${port}`
38-
})
39-
}
40-
})
41-
42-
proc.then(() => {}, (err) => reject(err))
43-
})
44-
}
45-
46-
const serverPaths = [
47-
path.join(dir, 'build'),
48-
path.join(dir, 'dist'),
49-
path.join(dir, 'public')
50-
]
51-
52-
for (const p of serverPaths) {
53-
if (fs.existsSync(p)) {
54-
return serveFrom(p)
55-
}
56-
}
57-
58-
// running a bare index.html file
59-
const files = [
60-
path.join(dir, 'index.html')
61-
]
62-
63-
for (const f of files) {
64-
if (fs.existsSync(f)) {
65-
console.info('Found bare file', f)
66-
67-
if (!fs.existsSync(path.resolve(dir, '../../dist'))) {
68-
console.info('Building IPFS')
69-
const proc = execa.command('npm run build', {
70-
cwd: path.resolve(dir, '../../'),
71-
env: {
72-
...process.env,
73-
CI: true // needed for some "clever" build tools
74-
}
75-
})
76-
proc.all.on('data', (data) => {
77-
process.stdout.write(data)
78-
})
79-
80-
await proc
81-
}
82-
83-
return Promise.resolve({
84-
url: `file://${f}`,
85-
stop: () => { }
86-
})
87-
}
88-
}
89-
90-
throw new Error('Browser examples must contain a `public`, `dist` or `build` folder or an `index.html` file')
91-
}
92-
93-
function ephemeralPort (min = 49152, max = 65535) {
94-
return Math.floor(Math.random() * (max - min + 1) + min)
95-
}
966

977
async function isExecutable (command) {
988
try {
@@ -147,7 +57,5 @@ async function waitForOutput (expectedOutput, command, args = [], opts = {}) {
14757
}
14858

14959
module.exports = {
150-
startServer,
151-
ephemeralPort,
15260
waitForOutput
15361
}

0 commit comments

Comments
 (0)