create session object to scope a user, move helper methods there

pull/21833/head
Bruno Windels 2018-08-07 16:45:34 +02:00
parent 97fa7e03d1
commit 7c91ecab7e
8 changed files with 203 additions and 216 deletions

View File

@ -1,149 +0,0 @@
/*
Copyright 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// puppeteer helpers
// TODO: rename to queryAndInnertext?
async function tryGetInnertext(page, selector) {
const field = await page.$(selector);
if (field != null) {
const text_handle = await field.getProperty('innerText');
return await text_handle.jsonValue();
}
return null;
}
async function innerText(page, field) {
const text_handle = await field.getProperty('innerText');
return await text_handle.jsonValue();
}
async function newPage() {
const page = await browser.newPage();
await page.setViewport({
width: 1280,
height: 800
});
return page;
}
function logConsole(page) {
let buffer = "";
page.on('console', msg => {
buffer += msg.text() + '\n';
});
return {
logs() {
return buffer;
}
}
}
function logXHRRequests(page) {
let buffer = "";
page.on('requestfinished', async (req) => {
const type = req.resourceType();
const response = await req.response();
//if (type === 'xhr' || type === 'fetch') {
buffer += `${type} ${response.status()} ${req.method()} ${req.url()} \n`;
// if (req.method() === "POST") {
// buffer += " Post data: " + req.postData();
// }
//}
});
return {
logs() {
return buffer;
}
}
}
async function getOuterHTML(element_handle) {
const html_handle = await element_handle.getProperty('outerHTML');
return await html_handle.jsonValue();
}
async function printElements(label, elements) {
console.log(label, await Promise.all(elements.map(getOuterHTML)));
}
async function replaceInputText(input, text) {
// click 3 times to select all text
await input.click({clickCount: 3});
// then remove it with backspace
await input.press('Backspace');
// and type the new text
await input.type(text);
}
// TODO: rename to waitAndQuery(Single)?
async function waitAndQuerySelector(page, selector, timeout = 500) {
await page.waitForSelector(selector, {visible: true, timeout});
return await page.$(selector);
}
async function waitAndQueryAll(page, selector, timeout = 500) {
await page.waitForSelector(selector, {visible: true, timeout});
return await page.$$(selector);
}
function waitForNewPage(timeout = 500) {
return new Promise((resolve, reject) => {
const timeoutHandle = setTimeout(() => {
browser.removeEventListener('targetcreated', callback);
reject(new Error(`timeout of ${timeout}ms for waitForNewPage elapsed`));
}, timeout);
const callback = async (target) => {
clearTimeout(timeoutHandle);
const page = await target.page();
resolve(page);
};
browser.once('targetcreated', callback);
});
}
// other helpers
function randomInt(max) {
return Math.ceil(Math.random()*max);
}
function riotUrl(path) {
return riotserver + path;
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = {
tryGetInnertext,
innerText,
newPage,
logConsole,
logXHRRequests,
getOuterHTML,
printElements,
replaceInputText,
waitAndQuerySelector,
waitAndQueryAll,
waitForNewPage,
randomInt,
riotUrl,
delay,
}

147
src/session.js Normal file
View File

@ -0,0 +1,147 @@
/*
Copyright 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const puppeteer = require('puppeteer');
module.exports = class RiotSession {
constructor(browser, page, username, riotserver) {
this.browser = browser;
this.page = page;
this.riotserver = riotserver;
this.username = username;
}
static async create(username, puppeteerOptions, riotserver) {
const browser = await puppeteer.launch(puppeteerOptions);
const page = await browser.newPage();
await page.setViewport({
width: 1280,
height: 800
});
return new RiotSession(browser, page, username, riotserver);
}
async tryGetInnertext(selector) {
const field = await this.page.$(selector);
if (field != null) {
const text_handle = await field.getProperty('innerText');
return await text_handle.jsonValue();
}
return null;
}
async innerText(field) {
const text_handle = await field.getProperty('innerText');
return await text_handle.jsonValue();
}
logConsole() {
let buffer = "";
this.page.on('console', msg => {
buffer += msg.text() + '\n';
});
return {
logs() {
return buffer;
}
}
}
logXHRRequests() {
let buffer = "";
this.page.on('requestfinished', async (req) => {
const type = req.resourceType();
const response = await req.response();
//if (type === 'xhr' || type === 'fetch') {
buffer += `${type} ${response.status()} ${req.method()} ${req.url()} \n`;
// if (req.method() === "POST") {
// buffer += " Post data: " + req.postData();
// }
//}
});
return {
logs() {
return buffer;
}
}
}
async getOuterHTML(element_handle) {
const html_handle = await element_handle.getProperty('outerHTML');
return await html_handle.jsonValue();
}
async printElements(label, elements) {
console.log(label, await Promise.all(elements.map(getOuterHTML)));
}
async replaceInputText(input, text) {
// click 3 times to select all text
await input.click({clickCount: 3});
// then remove it with backspace
await input.press('Backspace');
// and type the new text
await input.type(text);
}
// TODO: rename to waitAndQuery(Single)?
async waitAndQuerySelector(selector, timeout = 500) {
await this.page.waitForSelector(selector, {visible: true, timeout});
return await this.page.$(selector);
}
async waitAndQueryAll(selector, timeout = 500) {
await this.page.waitForSelector(selector, {visible: true, timeout});
return await this.page.$$(selector);
}
waitForNewPage(timeout = 500) {
return new Promise((resolve, reject) => {
const timeoutHandle = setTimeout(() => {
this.browser.removeEventListener('targetcreated', callback);
reject(new Error(`timeout of ${timeout}ms for waitForNewPage elapsed`));
}, timeout);
const callback = async (target) => {
clearTimeout(timeoutHandle);
const page = await target.page();
resolve(page);
};
this.browser.once('targetcreated', callback);
});
}
waitForSelector(selector) {
return this.page.waitForSelector(selector);
}
goto(url) {
return this.page.goto(url);
}
riotUrl(path) {
return this.riotserver + path;
}
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
close() {
return this.browser.close();
}
}

View File

@ -14,15 +14,14 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const helpers = require('../helpers');
const assert = require('assert'); const assert = require('assert');
module.exports = async function acceptTerms(page) { module.exports = async function acceptTerms(session) {
const reviewTermsButton = await helpers.waitAndQuerySelector(page, '.mx_QuestionDialog button.mx_Dialog_primary', 5000); const reviewTermsButton = await session.waitAndQuerySelector('.mx_QuestionDialog button.mx_Dialog_primary', 5000);
const termsPagePromise = helpers.waitForNewPage(); const termsPagePromise = session.waitForNewPage();
await reviewTermsButton.click(); await reviewTermsButton.click();
const termsPage = await termsPagePromise; const termsPage = await termsPagePromise;
const acceptButton = await termsPage.$('input[type=submit]'); const acceptButton = await termsPage.$('input[type=submit]');
await acceptButton.click(); await acceptButton.click();
await helpers.delay(500); //TODO yuck, timers await session.delay(500); //TODO yuck, timers
} }

View File

@ -14,19 +14,18 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const helpers = require('../helpers');
const assert = require('assert'); const assert = require('assert');
module.exports = async function createRoom(page, roomName) { module.exports = async function createRoom(session, roomName) {
//TODO: brittle selector //TODO: brittle selector
const createRoomButton = await helpers.waitAndQuerySelector(page, '.mx_RoleButton[aria-label="Create new room"]'); const createRoomButton = await session.waitAndQuerySelector('.mx_RoleButton[aria-label="Create new room"]');
await createRoomButton.click(); await createRoomButton.click();
const roomNameInput = await helpers.waitAndQuerySelector(page, '.mx_CreateRoomDialog_input'); const roomNameInput = await session.waitAndQuerySelector('.mx_CreateRoomDialog_input');
await helpers.replaceInputText(roomNameInput, roomName); await session.replaceInputText(roomNameInput, roomName);
const createButton = await helpers.waitAndQuerySelector(page, '.mx_Dialog_primary'); const createButton = await session.waitAndQuerySelector('.mx_Dialog_primary');
await createButton.click(); await createButton.click();
await page.waitForSelector('.mx_MessageComposer'); await session.waitForSelector('.mx_MessageComposer');
} }

View File

@ -14,22 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const helpers = require('../helpers');
const assert = require('assert'); const assert = require('assert');
module.exports = async function join(page, roomName) { module.exports = async function join(session, roomName) {
//TODO: brittle selector //TODO: brittle selector
const directoryButton = await helpers.waitAndQuerySelector(page, '.mx_RoleButton[aria-label="Room directory"]'); const directoryButton = await session.waitAndQuerySelector('.mx_RoleButton[aria-label="Room directory"]');
await directoryButton.click(); await directoryButton.click();
const roomInput = await helpers.waitAndQuerySelector(page, '.mx_DirectorySearchBox_input'); const roomInput = await session.waitAndQuerySelector('.mx_DirectorySearchBox_input');
await helpers.replaceInputText(roomInput, roomName); await session.replaceInputText(roomInput, roomName);
const firstRoomLabel = await helpers.waitAndQuerySelector(page, '.mx_RoomDirectory_table .mx_RoomDirectory_name:first-child'); const firstRoomLabel = await session.waitAndQuerySelector('.mx_RoomDirectory_table .mx_RoomDirectory_name:first-child');
await firstRoomLabel.click(); await firstRoomLabel.click();
const joinLink = await helpers.waitAndQuerySelector(page, '.mx_RoomPreviewBar_join_text a'); const joinLink = await session.waitAndQuerySelector('.mx_RoomPreviewBar_join_text a');
await joinLink.click(); await joinLink.click();
await page.waitForSelector('.mx_MessageComposer'); await session.waitForSelector('.mx_MessageComposer');
} }

View File

@ -14,14 +14,13 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const helpers = require('../helpers');
const assert = require('assert'); const assert = require('assert');
module.exports = async function acceptServerNoticesInviteAndConsent(page, name) { module.exports = async function acceptServerNoticesInviteAndConsent(session, name) {
//TODO: brittle selector //TODO: brittle selector
const invitesHandles = await helpers.waitAndQueryAll(page, '.mx_RoomTile_name.mx_RoomTile_invite'); const invitesHandles = await session.waitAndQueryAll('.mx_RoomTile_name.mx_RoomTile_invite');
const invitesWithText = await Promise.all(invitesHandles.map(async (inviteHandle) => { const invitesWithText = await Promise.all(invitesHandles.map(async (inviteHandle) => {
const text = await helpers.innerText(page, inviteHandle); const text = await session.innerText(inviteHandle);
return {inviteHandle, text}; return {inviteHandle, text};
})); }));
const inviteHandle = invitesWithText.find(({inviteHandle, text}) => { const inviteHandle = invitesWithText.find(({inviteHandle, text}) => {
@ -30,15 +29,15 @@ module.exports = async function acceptServerNoticesInviteAndConsent(page, name)
await inviteHandle.click(); await inviteHandle.click();
const acceptInvitationLink = await helpers.waitAndQuerySelector(page, ".mx_RoomPreviewBar_join_text a:first-child"); const acceptInvitationLink = await session.waitAndQuerySelector(".mx_RoomPreviewBar_join_text a:first-child");
await acceptInvitationLink.click(); await acceptInvitationLink.click();
const consentLink = await helpers.waitAndQuerySelector(page, ".mx_EventTile_body a", 1000); const consentLink = await session.waitAndQuerySelector(".mx_EventTile_body a", 1000);
const termsPagePromise = helpers.waitForNewPage(); const termsPagePromise = session.waitForNewPage();
await consentLink.click(); await consentLink.click();
const termsPage = await termsPagePromise; const termsPage = await termsPagePromise;
const acceptButton = await termsPage.$('input[type=submit]'); const acceptButton = await termsPage.$('input[type=submit]');
await acceptButton.click(); await acceptButton.click();
await helpers.delay(500); //TODO yuck, timers await session.delay(500); //TODO yuck, timers
} }

View File

@ -14,55 +14,54 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const helpers = require('../helpers');
const acceptTerms = require('./consent'); const acceptTerms = require('./consent');
const assert = require('assert'); const assert = require('assert');
module.exports = async function signup(page, username, password, homeserver) { module.exports = async function signup(session, username, password, homeserver) {
await page.goto(helpers.riotUrl('/#/register')); await session.goto(session.riotUrl('/#/register'));
//click 'Custom server' radio button //click 'Custom server' radio button
if (homeserver) { if (homeserver) {
const advancedRadioButton = await helpers.waitAndQuerySelector(page, '#advanced'); const advancedRadioButton = await session.waitAndQuerySelector('#advanced');
await advancedRadioButton.click(); await advancedRadioButton.click();
} }
// wait until register button is visible // wait until register button is visible
await page.waitForSelector('.mx_Login_submit[value=Register]', {visible: true, timeout: 500}); await session.waitForSelector('.mx_Login_submit[value=Register]', {visible: true, timeout: 500});
//fill out form //fill out form
const loginFields = await page.$$('.mx_Login_field'); const loginFields = await session.page.$$('.mx_Login_field');
assert.strictEqual(loginFields.length, 7); assert.strictEqual(loginFields.length, 7);
const usernameField = loginFields[2]; const usernameField = loginFields[2];
const passwordField = loginFields[3]; const passwordField = loginFields[3];
const passwordRepeatField = loginFields[4]; const passwordRepeatField = loginFields[4];
const hsurlField = loginFields[5]; const hsurlField = loginFields[5];
await helpers.replaceInputText(usernameField, username); await session.replaceInputText(usernameField, username);
await helpers.replaceInputText(passwordField, password); await session.replaceInputText(passwordField, password);
await helpers.replaceInputText(passwordRepeatField, password); await session.replaceInputText(passwordRepeatField, password);
if (homeserver) { if (homeserver) {
await page.waitForSelector('.mx_ServerConfig', {visible: true, timeout: 500}); await session.waitForSelector('.mx_ServerConfig', {visible: true, timeout: 500});
await helpers.replaceInputText(hsurlField, homeserver); await session.replaceInputText(hsurlField, homeserver);
} }
//wait over a second because Registration/ServerConfig have a 1000ms //wait over a second because Registration/ServerConfig have a 1000ms
//delay to internally set the homeserver url //delay to internally set the homeserver url
//see Registration::render and ServerConfig::props::delayTimeMs //see Registration::render and ServerConfig::props::delayTimeMs
await helpers.delay(1200); await session.delay(1200);
/// focus on the button to make sure error validation /// focus on the button to make sure error validation
/// has happened before checking the form is good to go /// has happened before checking the form is good to go
const registerButton = await page.$('.mx_Login_submit'); const registerButton = await session.page.$('.mx_Login_submit');
await registerButton.focus(); await registerButton.focus();
//check no errors //check no errors
const error_text = await helpers.tryGetInnertext(page, '.mx_Login_error'); const error_text = await session.tryGetInnertext('.mx_Login_error');
assert.strictEqual(!!error_text, false); assert.strictEqual(!!error_text, false);
//submit form //submit form
//await page.screenshot({path: "beforesubmit.png", fullPage: true}); //await page.screenshot({path: "beforesubmit.png", fullPage: true});
await registerButton.click(); await registerButton.click();
//confirm dialog saying you cant log back in without e-mail //confirm dialog saying you cant log back in without e-mail
const continueButton = await helpers.waitAndQuerySelector(page, '.mx_QuestionDialog button.mx_Dialog_primary'); const continueButton = await session.waitAndQuerySelector('.mx_QuestionDialog button.mx_Dialog_primary');
await continueButton.click(); await continueButton.click();
//wait for registration to finish so the hash gets set //wait for registration to finish so the hash gets set
//onhashchange better? //onhashchange better?
await helpers.delay(2000); await session.delay(2000);
const url = page.url(); const url = session.page.url();
assert.strictEqual(url, helpers.riotUrl('/#/home')); assert.strictEqual(url, session.riotUrl('/#/home'));
} }

View File

@ -14,19 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const puppeteer = require('puppeteer');
const helpers = require('./helpers');
const assert = require('assert'); const assert = require('assert');
const RiotSession = require('./src/session');
const signup = require('./tests/signup'); const signup = require('./src/tests/signup');
const join = require('./tests/join'); const join = require('./src/tests/join');
const createRoom = require('./tests/create-room'); const createRoom = require('./src/tests/create-room');
const acceptServerNoticesInviteAndConsent = require('./tests/server-notices-consent'); const acceptServerNoticesInviteAndConsent = require('./src/tests/server-notices-consent');
const homeserver = 'http://localhost:8008'; const homeserver = 'http://localhost:8008';
const riotserver = 'http://localhost:5000';
global.riotserver = 'http://localhost:5000';
global.browser = null;
let consoleLogs = null; let consoleLogs = null;
let xhrLogs = null; let xhrLogs = null;
@ -40,30 +37,27 @@ async function runTests() {
console.log(`(using external chrome/chromium at ${path}, make sure it's compatible with puppeteer)`); console.log(`(using external chrome/chromium at ${path}, make sure it's compatible with puppeteer)`);
options.executablePath = path; options.executablePath = path;
} }
global.browser = await puppeteer.launch(options);
const page = await helpers.newPage();
globalPage = page;
consoleLogs = helpers.logConsole(page); const alice = await RiotSession.create("alice", options, riotserver);
xhrLogs = helpers.logXHRRequests(page);
consoleLogs = alice.logConsole();
xhrLogs = alice.logXHRRequests();
const username = 'user-' + helpers.randomInt(10000); process.stdout.write(`* signing up as ${alice.username} ... `);
const password = 'testtest'; await signup(alice, alice.username, 'testtest');
process.stdout.write(`* signing up as ${username} ... `);
await signup(page, username, password);
process.stdout.write('done\n'); process.stdout.write('done\n');
const noticesName = "Server Notices"; const noticesName = "Server Notices";
process.stdout.write(`* accepting "${noticesName}" and accepting terms & conditions ... `); process.stdout.write(`* accepting "${noticesName}" and accepting terms & conditions ... `);
await acceptServerNoticesInviteAndConsent(page, noticesName); await acceptServerNoticesInviteAndConsent(alice, noticesName);
process.stdout.write('done\n'); process.stdout.write('done\n');
const room = 'test'; const room = 'test';
process.stdout.write(`* creating room ${room} ... `); process.stdout.write(`* creating room ${room} ... `);
await createRoom(page, room); await createRoom(alice, room);
process.stdout.write('done\n'); process.stdout.write('done\n');
await browser.close(); await alice.close();
} }
function onSuccess() { function onSuccess() {