123456789101112131415161718192021222324252627282930313233 |
- #!/usr/bin/env node
- const type = (arg =>
- arg == 'firefox' ? 'firefox' : arg == 'webkit' ? 'webkit' : 'chromium'
- )(process.argv[1]);
- const wsUrl = require('../config.json').playwright_ws_spec_endpoint ||
- require('../config.example.json').playwright_ws_spec_endpoint;
- const axios = require('axios').default;
- const playwright = require('playwright');
- axios.get('http://localhost:9222').then(res => {
- if (res.status !== 426) {
- console.error(Error('port 9222 is in use but not a websocket'));
- return setTimeout(() => process.exit(1), 30000);
- }
- return axios.get(wsUrl).then(jsonres => {
- const endpoint = jsonres.data[type];
- if (!endpoint) {
- console.error(Error('url does not return the required browser type'));
- return setTimeout(() => process.exit(1), 30000);
- }
- if (typeof endpoint !== 'string') throw Error('returned endpoint url is malformed');
- return playwright[type].connect(endpoint).catch(() => { throw Error('connection error'); });
- });
- }).catch(err => {
- if (!axios.isAxiosError(err)) console.log(err);
- playwright[type].launchServer({args: process.argv.slice(2), port: 9222}).then(svr =>
- require('http').createServer((req, res) => {
- const json = req.url === '/playwright-ws.json' &&
- JSON.stringify(Object.fromEntries([[type, svr.wsEndpoint()]]));
- if (json) { res.write(json); console.log(json); res.end(); }
- else res.destroy();
- }).listen(8080)
- );
- });
|