This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
/
testPuppeteer.js
75 lines (70 loc) · 2.36 KB
/
testPuppeteer.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
// eslint-disable-next-line import/no-extraneous-dependencies
const puppeteer = require('puppeteer');
// eslint-disable-next-line import/no-extraneous-dependencies
const express = require('express');
const { EventEmitter } = require('events');
const path = require('path');
const app = express();
const events = new EventEmitter();
app.get('/connect', async (req, res) => {
events.emit('connect', req, res);
});
events.on('connect', async (req, res) => {
const { url, message, kick, waitAfterSendingMessage, leave } = req.query;
// TODO: Use getBrowser.js from https://github.com/BrycensRanch/Focus-SIS/blob/feat/classroom-integration/getBrowser.js
const browser = await puppeteer.launch({
args: [
`--use-fake-device-for-media-stream`,
`--use-fake-ui-for-media-stream`,
`--no-sandbox`,
`--use-file-for-fake-video-capture=${path.join(
__dirname,
'cypress/fixtures/akiyo_cif.y4m'
)}`,
],
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
// await page.setViewport({ width: 1920, height: 1080 });
const wait = (milliseconds) =>
// eslint-disable-next-line no-promise-executor-return
new Promise((r) => setTimeout(r, milliseconds));
await wait(2500);
console.log(`sending message: ${message}`);
if (Array.isArray(message) && message.length && message !== 'undefined') {
const promises = [];
// eslint-disable-next-line no-restricted-syntax
for (const msg of message) {
// eslint-disable-next-line no-await-in-loop, no-restricted-syntax
await page.type('#message', msg);
// eslint-disable-next-line no-await-in-loop, no-restricted-syntax
// eslint-disable-next-line no-await-in-loop
await page.click('#messageSend');
}
await Promise.all(promises);
} else if (message) {
await page.type('#message', message);
await page.click('#messageSend');
}
if (kick) {
await res.send('ok');
await wait(3000);
await page.click('#kickButton');
}
if (leave) {
await wait(1000);
await page.click('#leaveRoom');
}
if (waitAfterSendingMessage && !kick) {
// not a bug, it's a feature!
await res.send('ok');
await wait(2500);
await browser.close();
} else if (kick) {
await browser.close();
} else {
await res.send('ok');
await browser.close();
}
});
app.listen(8081, () => {});