bigbluebutton-Github/bigbluebutton-tests/playwright/core/page.js
2022-01-19 16:50:59 -03:00

137 lines
4.4 KiB
JavaScript

require('dotenv').config();
const { expect } = require('@playwright/test');
const yaml = require('js-yaml');
const path = require('path');
const fs = require('fs');
const parameters = require('./parameters');
const helpers = require('./helpers');
const e = require('./elements');
const { ELEMENT_WAIT_TIME, ELEMENT_WAIT_LONGER_TIME, VIDEO_LOADING_WAIT_TIME } = require('./constants');
const { checkElement } = require('./util');
class Page {
constructor(browser, page) {
this.browser = browser;
this.page = page;
this.initParameters = Object.assign({}, parameters);
}
async getSettingsYaml() {
try {
return yaml.load(fs.readFileSync(path.join(__dirname, '../../../bigbluebutton-html5/private/config/settings.yml'), 'utf8'));
} catch (err) {
console.log(err);
}
}
async bringToFront() {
await this.page.bringToFront();
}
async getLastTargetPage(context) {
const contextPages = await context.pages();
return new Page(this.browser, contextPages[contextPages.length - 1]);
}
async init(isModerator, shouldCloseAudioModal, initOptions) {
const { fullName, meetingId, customParameter } = initOptions || {};
if (!isModerator) this.initParameters.moderatorPW = '';
if (fullName) this.initParameters.fullName = fullName;
this.meetingId = (meetingId) ? meetingId : await helpers.createMeeting(parameters, customParameter);
const joinUrl = helpers.getJoinURL(this.meetingId, this.initParameters, isModerator, customParameter);
await this.page.goto(joinUrl);
if (shouldCloseAudioModal) await this.closeAudioModal();
}
async joinMicrophone() {
await this.waitForSelector(e.audioModal);
await this.waitAndClick(e.microphoneButton);
await this.waitForSelector(e.connectingStatus);
const parsedSettings = await this.getSettingsYaml();
const listenOnlyCallTimeout = parseInt(parsedSettings.public.media.listenOnlyCallTimeout);
await this.waitAndClick(e.echoYesButton, listenOnlyCallTimeout);
await this.waitForSelector(e.isTalking);
}
async leaveAudio() {
await this.waitAndClick(e.leaveAudio);
await this.waitForSelector(e.joinAudio);
}
async logoutFromMeeting() {
await this.waitAndClick(e.optionsButton);
await this.waitAndClick(e.logout);
}
async shareWebcam(shouldConfirmSharing, videoPreviewTimeout = ELEMENT_WAIT_TIME) {
await this.waitAndClick(e.joinVideo);
if (shouldConfirmSharing) {
await this.waitForSelector(e.videoPreview, videoPreviewTimeout);
await this.waitAndClick(e.startSharingWebcam);
}
await this.waitForSelector(e.webcamConnecting);
await this.waitForSelector(e.webcamVideo, VIDEO_LOADING_WAIT_TIME);
await this.waitForSelector(e.leaveVideo, VIDEO_LOADING_WAIT_TIME);
}
getLocator(selector) {
return this.page.locator(selector);
}
async getSelectorCount(selector) {
const locator = this.getLocator(selector);
return locator.count();
}
async closeAudioModal() {
await this.waitForSelector(e.audioModal, ELEMENT_WAIT_LONGER_TIME);
await this.waitAndClick(e.closeAudioButton);
}
async waitForSelector(selector, timeout = ELEMENT_WAIT_TIME) {
await this.page.waitForSelector(selector, { timeout });
}
async type(selector, text) {
const handle = this.getLocator(selector);
await handle.focus();
await handle.type(text);
}
async waitAndClickElement(element, index = 0, timeout = ELEMENT_WAIT_TIME) {
await this.waitForSelector(element, timeout);
await this.page.evaluate(([elem, i]) => {
document.querySelectorAll(elem)[i].click();
}, [element, index]);
}
async waitAndClick(selector, timeout = ELEMENT_WAIT_TIME) {
await this.waitForSelector(selector, timeout);
await this.page.focus(selector);
await this.page.click(selector, { timeout });
}
async checkElement(selector, index = 0) {
return this.page.evaluate(checkElement, [selector, index]);
}
async wasRemoved(selector, timeout = ELEMENT_WAIT_TIME) {
const locator = this.getLocator(selector);
await expect(locator).toBeHidden({ timeout });
}
async hasElement(selector, timeout = ELEMENT_WAIT_TIME) {
const locator = this.getLocator(selector);
await expect(locator).toBeVisible({ timeout });
}
async hasText(selector, text, timeout = ELEMENT_WAIT_TIME) {
const locator = this.getLocator(selector);
await expect(locator).toContainText(text, { timeout });
}
}
module.exports = exports = Page;