summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-11-11 01:11:01 +0300
committereug-vs <eug-vs@keemail.me>2020-11-11 01:11:01 +0300
commitc687f40c206e1d1f874e91dd396c8d68c64c83f2 (patch)
tree4776bd99d32b79591b21f19b637e4a3927d0d214
parenta52bf99bbf2d394685f92aa13b5bff438ff8ef0a (diff)
downloadbsu-fantom-c687f40c206e1d1f874e91dd396c8d68c64c83f2.tar.gz
refactor: move click handlers to utils
-rw-r--r--src/attendConference.js5
-rw-r--r--src/index.js27
-rw-r--r--src/utils.js13
3 files changed, 29 insertions, 16 deletions
diff --git a/src/attendConference.js b/src/attendConference.js
index b7f744f..80dc1c5 100644
--- a/src/attendConference.js
+++ b/src/attendConference.js
@@ -1,6 +1,5 @@
-const clickElementByXPath = (page, xPath) => page
- .waitForXPath(xPath)
- .then(item => item.click());
+const { clickElementByXPath } = require('./utils.js');
+
const attendConference = async page => {
// Join as "Listen only"
diff --git a/src/index.js b/src/index.js
index 1432923..5345587 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,12 +1,7 @@
require('dotenv').config();
const puppeteer = require('puppeteer');
const attendConference = require('./attendConference.js');
-
-
-const clickAndWait = (page, selector) => Promise.all([
- page.click(selector),
- page.waitForNavigation(),
-]);
+const { clickElementBySelector } = require('./utils.js');
const {
EDUFPMI_URL,
@@ -15,33 +10,39 @@ const {
COURSE_URL
} = process.env;
-puppeteer.launch({ headless: false })
+puppeteer.launch({ headless: true })
.then(async browser => {
const page = await browser.newPage();
// Login
- await page.goto(EDUFPMI_URL, { waitUntil: 'networkidle0' });
+ await page.goto(EDUFPMI_URL, { waitUntil: 'domcontentloaded' });
await page.type('input#username', EDUFPMI_USERNAME);
await page.type('input#password', EDUFPMI_PASSWORD);
- await clickAndWait(page, 'button#loginbtn');
+ await Promise.all([
+ clickElementBySelector(page, 'button#loginbtn'),
+ page.waitForNavigation()
+ ]);
console.log(`Logged in as ${EDUFPMI_USERNAME}`);
// Find the course
- await page.goto(COURSE_URL, { waitUntil: 'networkidle0' });
- await clickAndWait(page, 'li.bigbluebuttonbn');
+ await page.goto(COURSE_URL);
+ await Promise.all([
+ clickElementBySelector(page, 'li.bigbluebuttonbn'),
+ page.waitForNavigation() // Wait until the next page loads
+ ]);
// Launch a meeting
const conferencePagePromise = new Promise(resolve => browser.on(
'targetcreated',
target => resolve(target.page())
));
- await page.click('input#join_button_input');
+ await clickElementBySelector(page, 'input#join_button_input');
const conferencePage = await conferencePagePromise;
await attendConference(conferencePage);
// Fill up the attendance
await page.goBack();
- await clickAndWait(page, 'li.attendance');
+ await clickElementBySelector(page, 'li.attendance');
})
.catch(e => console.log(e));
diff --git a/src/utils.js b/src/utils.js
new file mode 100644
index 0000000..cf9fe38
--- /dev/null
+++ b/src/utils.js
@@ -0,0 +1,13 @@
+const clickElementByXPath = (page, xPath) => page
+ .waitForXPath(xPath)
+ .then(item => item.click());
+
+const clickElementBySelector = (page, selector) => page
+ .waitForSelector(selector)
+ .then(item => item.click());
+
+
+module.exports = {
+ clickElementByXPath,
+ clickElementBySelector
+};