From 4f0561acef93970f69b0ddda1cea132532355e7d Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 14 Nov 2020 16:35:14 +0300 Subject: refactor: create handlers folder --- src/attendConference.js | 23 ----------------- src/handlers/attendConference.js | 23 +++++++++++++++++ src/handlers/index.js | 53 +++++++++++++++++++++++++++++++++++++++ src/handlers/utils.js | 13 ++++++++++ src/index.js | 54 ---------------------------------------- src/utils.js | 13 ---------- 6 files changed, 89 insertions(+), 90 deletions(-) delete mode 100644 src/attendConference.js create mode 100644 src/handlers/attendConference.js create mode 100644 src/handlers/index.js create mode 100644 src/handlers/utils.js delete mode 100644 src/index.js delete mode 100644 src/utils.js diff --git a/src/attendConference.js b/src/attendConference.js deleted file mode 100644 index 54d6479..0000000 --- a/src/attendConference.js +++ /dev/null @@ -1,23 +0,0 @@ -const { clickElementByXPath } = require('./utils.js'); - -const CONFERENCE_DURATION = 1000 * 60 * 80; // 80 minutes - - -const attendConference = async (page, onJoin) => { - // Join as "Listen only" - await clickElementByXPath(page, '//span[contains(text(),"Listen only")]'); - if (typeof(onJoin) === 'function') onJoin(); - console.log('Joined the conference'); - - // Wait 5 seconds - await page.waitForTimeout(CONFERENCE_DURATION); - console.log('Time to leave!') - - // Leave audio and close the tab - await clickElementByXPath(page, '//button[contains(@aria-label,"Leave audio")]'); - await page.waitForTimeout(1500); - await page.close(); - console.log('Left the conference'); -}; - -module.exports = attendConference; diff --git a/src/handlers/attendConference.js b/src/handlers/attendConference.js new file mode 100644 index 0000000..54d6479 --- /dev/null +++ b/src/handlers/attendConference.js @@ -0,0 +1,23 @@ +const { clickElementByXPath } = require('./utils.js'); + +const CONFERENCE_DURATION = 1000 * 60 * 80; // 80 minutes + + +const attendConference = async (page, onJoin) => { + // Join as "Listen only" + await clickElementByXPath(page, '//span[contains(text(),"Listen only")]'); + if (typeof(onJoin) === 'function') onJoin(); + console.log('Joined the conference'); + + // Wait 5 seconds + await page.waitForTimeout(CONFERENCE_DURATION); + console.log('Time to leave!') + + // Leave audio and close the tab + await clickElementByXPath(page, '//button[contains(@aria-label,"Leave audio")]'); + await page.waitForTimeout(1500); + await page.close(); + console.log('Left the conference'); +}; + +module.exports = attendConference; diff --git a/src/handlers/index.js b/src/handlers/index.js new file mode 100644 index 0000000..c50ef23 --- /dev/null +++ b/src/handlers/index.js @@ -0,0 +1,53 @@ +const puppeteer = require('puppeteer'); +const attendConference = require('./attendConference.js'); +const { clickElementBySelector } = require('./utils.js'); + +const { + EDUFPMI_URL, + EDUFPMI_USERNAME, + EDUFPMI_PASSWORD, + COURSE_URL +} = process.env; + +puppeteer.launch({ headless: true }) + .then(async browser => { + const page = await browser.newPage(); + + // Login + await page.goto(EDUFPMI_URL, { waitUntil: 'domcontentloaded' }); + await page.type('input#username', EDUFPMI_USERNAME); + await page.type('input#password', EDUFPMI_PASSWORD); + await Promise.all([ + clickElementBySelector(page, 'button#loginbtn'), + page.waitForNavigation() + ]); + console.log(`Logged in as ${EDUFPMI_USERNAME}`); + + // Find the course + await page.goto(COURSE_URL); + await Promise.all([ + clickElementBySelector(page, 'li.bigbluebuttonbn a'), + page.waitForNavigation() // Wait until the next page loads + ]); + + // Launch the conference in a new tab + const conferencePagePromise = new Promise(resolve => browser.on( + 'targetcreated', + target => resolve(target.page()) + )); + await clickElementBySelector(page, 'input#join_button_input'); + const conferencePage = await conferencePagePromise; + + // Prepare onJoin callback + const fillAttendance = async () => { + await page.goBack(); + await clickElementBySelector(page, 'li.attendance a'); + // TODO: actually fill the attendance + console.log('Attendance filled'); + }; + + await attendConference(conferencePage, fillAttendance); + await browser.close(); + }) + .catch(e => console.log(e)); + diff --git a/src/handlers/utils.js b/src/handlers/utils.js new file mode 100644 index 0000000..cf9fe38 --- /dev/null +++ b/src/handlers/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 +}; diff --git a/src/index.js b/src/index.js deleted file mode 100644 index 6fa3b7b..0000000 --- a/src/index.js +++ /dev/null @@ -1,54 +0,0 @@ -require('dotenv').config(); -const puppeteer = require('puppeteer'); -const attendConference = require('./attendConference.js'); -const { clickElementBySelector } = require('./utils.js'); - -const { - EDUFPMI_URL, - EDUFPMI_USERNAME, - EDUFPMI_PASSWORD, - COURSE_URL -} = process.env; - -puppeteer.launch({ headless: true }) - .then(async browser => { - const page = await browser.newPage(); - - // Login - await page.goto(EDUFPMI_URL, { waitUntil: 'domcontentloaded' }); - await page.type('input#username', EDUFPMI_USERNAME); - await page.type('input#password', EDUFPMI_PASSWORD); - await Promise.all([ - clickElementBySelector(page, 'button#loginbtn'), - page.waitForNavigation() - ]); - console.log(`Logged in as ${EDUFPMI_USERNAME}`); - - // Find the course - await page.goto(COURSE_URL); - await Promise.all([ - clickElementBySelector(page, 'li.bigbluebuttonbn a'), - page.waitForNavigation() // Wait until the next page loads - ]); - - // Launch the conference in a new tab - const conferencePagePromise = new Promise(resolve => browser.on( - 'targetcreated', - target => resolve(target.page()) - )); - await clickElementBySelector(page, 'input#join_button_input'); - const conferencePage = await conferencePagePromise; - - // Prepare onJoin callback - const fillAttendance = async () => { - await page.goBack(); - await clickElementBySelector(page, 'li.attendance a'); - // TODO: actually fill the attendance - console.log('Attendance filled'); - }; - - await attendConference(conferencePage, fillAttendance); - await browser.close(); - }) - .catch(e => console.log(e)); - diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index cf9fe38..0000000 --- a/src/utils.js +++ /dev/null @@ -1,13 +0,0 @@ -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 -}; -- cgit v1.2.3