From 2b04f4d4e3128eac8ab21d6cfecfd0082d619453 Mon Sep 17 00:00:00 2001 From: Pedro Beschorner Marin Date: Sun, 30 May 2021 17:27:42 -0300 Subject: [PATCH] Create selector module --- lib/action/screenshare.js | 3 +- lib/selector.js | 61 ++++++++++++++++++++++ lib/util.js | 105 +++++++++----------------------------- 3 files changed, 87 insertions(+), 82 deletions(-) create mode 100644 lib/selector.js diff --git a/lib/action/screenshare.js b/lib/action/screenshare.js index f3883f9..f9fd67a 100644 --- a/lib/action/screenshare.js +++ b/lib/action/screenshare.js @@ -1,5 +1,6 @@ const conf = require('../conf'); const util = require('../util'); +const selector = require('../selector'); const perform = require('./perform'); const audio = require('./audio'); @@ -13,7 +14,7 @@ const action = { execute: async page => await util.click(page, label.share, true), test: async page => { const { locale } = page.bigbluebot; - const params = [ util.localize(locale, label.screenshare) ]; + const params = [ selector.localize(locale, label.screenshare) ]; const element = { label: label.fullscreen, params }; return await util.visible(page, element, true); }, diff --git a/lib/selector.js b/lib/selector.js new file mode 100644 index 0000000..24a68c5 --- /dev/null +++ b/lib/selector.js @@ -0,0 +1,61 @@ +const logger = require('./logger'); + +// TODO: Better handle braces +const aria = label => { + const opening = label.indexOf('{'); + if (opening === -1) { + return `[aria-label^="${label}"]`; + } else { + if (opening !== 0) { + return `[aria-label^="${label.slice(0, opening - 1)}"]`; + } + const closing = label.indexOf('}'); + + return `[aria-label$="${label.slice(closing, label.length)}"]`; + } +}; + +const compose = (label, params) => { + let composition = label; + params.forEach((param, index) => { + composition = composition.replace(`{${index}}`, param); + }); + + return composition; +}; + +const getLabel = (element) => { + if (element && element.label) return element.label; + + return element; +}; + +const getParams = (element) => { + if (element && element.params) return element.params; + + return []; +}; + +const localize = (locale, element) => { + const label = getLabel(element); + const params = getParams(element); + + let localization; + if (locale && locale[label]) { + localization = compose(locale[label], params); + } else { + logger.error(`Missing label ${locale[label]}`); + } + + return localization; +}; + +module.exports = { + get: (locale, element) => { + const localization = localize(locale, element); + const selector = aria(localization); + + return selector; + }, + localize, +}; diff --git a/lib/util.js b/lib/util.js index 30f2153..17b141d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,6 +2,7 @@ const faker = require('faker'); const conf = require('./conf'); const logger = require('./logger'); const api = require('./api'); +const selector = require('./selector'); const { config } = conf; const { timeout } = config; @@ -40,68 +41,10 @@ const generateText = lines => { return text; }; -// TODO: Better handle braces -const aria = label => { - const opening = label.indexOf('{'); - if (opening === -1) { - return `[aria-label^="${label}"]`; - } else { - if (opening !== 0) { - return `[aria-label^="${label.slice(0, opening - 1)}"]`; - } - const closing = label.indexOf('}'); - - return `[aria-label$="${label.slice(closing, label.length)}"]`; - } -}; - -const compose = (label, params) => { - let composition = label; - params.forEach((param, index) => { - composition = composition.replace(`{${index}}`, param); - }); - - return composition; -}; - -const getLabel = (element) => { - if (element && element.label) return element.label; - - return element; -}; - -const getParams = (element) => { - if (element && element.params) return element.params; - - return []; -}; - -const localize = (locale, element) => { - const label = getLabel(element); - const params = getParams(element); - - let localization; - if (locale && locale[label]) { - localization = compose(locale[label], params); - } else { - logger.error(`Missing label ${locale[label]}`); - } - - return localization; -}; - -const translate = (locale, element) => { - const localization = localize(locale, element); - const selector = aria(localization); - - return selector; -}; - module.exports = { delay, random, generateText, - localize, join: async (page, locale, options) => { const username = generateUsername(); logger.info(`${username}: join ${options.host || config.url.host} at ${options.room || config.url.meeting.name}`); @@ -109,9 +52,9 @@ module.exports = { await page.setViewport({ width, height }); const url = api.getJoinURL(username, options); await page.goto(url); - const selector = translate(locale, conf.label.main.options.button); - await page.waitForSelector(selector, { timeout: timeout.selector }); - logger.debug(`${username}: notice ${selector}`); + const slctr = selector.get(locale, conf.label.main.options.button); + logger.debug(`${username}: notice ${slctr}`); + await page.waitForSelector(slctr, { timeout: timeout.selector }); await delay(config.delay.animation); return username; @@ -130,26 +73,26 @@ module.exports = { }, click: async (page, element, animation = false) => { const { username, locale } = page.bigbluebot; - const selector = translate(locale, element); - logger.debug(`${username}: click ${selector}`); - await page.waitForSelector(selector, { timeout: timeout.selector }); - await page.click(selector); + const slctr = selector.get(locale, element); + logger.debug(`${username}: click ${slctr}`); + await page.waitForSelector(slctr, { timeout: timeout.selector }); + await page.click(slctr); if (animation) await delay(config.delay.animation); }, type: async (page, element, text, animation = false) => { const { username, locale } = page.bigbluebot; - const selector = translate(locale, element); - logger.debug(`${username}: type ${text} in ${selector}`); - await page.waitForSelector(selector, { timeout: timeout.selector }); - await page.type(selector, text); + const slctr = selector.get(locale, element); + logger.debug(`${username}: type ${text} in ${slctr}`); + await page.waitForSelector(slctr, { timeout: timeout.selector }); + await page.type(slctr, text); if (animation) await delay(config.delay.animation); }, write: async (page, element, text) => { const { username, locale } = page.bigbluebot; - const selector = translate(locale, element); - logger.debug(`${username}: write ${text} in ${selector}`); - await page.waitForSelector(selector, { timeout: timeout.selector }); - await page.type(selector, text, { delay: config.delay.type }); + const slctr = selector.get(locale, element); + logger.debug(`${username}: write ${text} in ${slctr}`); + await page.waitForSelector(slctr, { timeout: timeout.selector }); + await page.type(slctr, text, { delay: config.delay.type }); }, screenshot: async (page, status, description) => { if (config.screenshot.enabled) { @@ -173,15 +116,15 @@ module.exports = { }, visible: async (page, element, media = false) => { const { username, locale } = page.bigbluebot; - const selector = translate(locale, element); + const slctr = selector.get(locale, element); const { connection, relief } = config.delay; const timeout = media ? connection : relief; let visible; - await page.waitForSelector(selector, { timeout }).then(() => { - logger.debug(`${username}: ${selector} is visible`); + await page.waitForSelector(slctr, { timeout }).then(() => { + logger.debug(`${username}: ${slctr} is visible`); visible = true; }).catch(() => { - logger.warn(`${username}: ${selector} is not visible`); + logger.warn(`${username}: ${slctr} is not visible`); visible = false; }); @@ -189,14 +132,14 @@ module.exports = { }, hidden: async (page, element) => { const { username, locale } = page.bigbluebot; - const selector = translate(locale, element); + const slctr = selector.get(locale, element); const { relief } = config.delay; let hidden; - await page.waitForSelector(selector, { timeout: relief }).then(() => { - logger.warn(`${username}: ${selector} is not hidden`); + await page.waitForSelector(slctr, { timeout: relief }).then(() => { + logger.warn(`${username}: ${slctr} is not hidden`); hidden = false; }).catch(() => { - logger.debug(`${username}: ${selector} is hidden`); + logger.debug(`${username}: ${slctr} is hidden`); hidden = true; });