Skip to content

Commit

Permalink
Create selector module
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobmarin committed May 30, 2021
1 parent e0c156d commit 2b04f4d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 82 deletions.
3 changes: 2 additions & 1 deletion lib/action/screenshare.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const conf = require('../conf');
const util = require('../util');
const selector = require('../selector');
const perform = require('./perform');
const audio = require('./audio');

Expand All @@ -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);
},
Expand Down
61 changes: 61 additions & 0 deletions lib/selector.js
Original file line number Diff line number Diff line change
@@ -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,
};
105 changes: 24 additions & 81 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,78 +41,20 @@ 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}`);
const { width, height } = config.browser.window;
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;
Expand All @@ -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) {
Expand All @@ -173,30 +116,30 @@ 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;
});

return visible;
},
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;
});

Expand Down

0 comments on commit 2b04f4d

Please sign in to comment.