diff --git a/helpers.js b/helpers.js index bb4f0b20ca..3e2467e622 100644 --- a/helpers.js +++ b/helpers.js @@ -16,7 +16,7 @@ limitations under the License. // puppeteer helpers -async function try_get_innertext(page, selector) { +async function tryGetInnertext(page, selector) { const field = await page.$(selector); if (field != null) { const text_handle = await field.getProperty('innerText'); @@ -25,7 +25,7 @@ async function try_get_innertext(page, selector) { return null; } -async function new_page() { +async function newPage() { const page = await browser.newPage(); await page.setViewport({ width: 1280, @@ -34,7 +34,7 @@ async function new_page() { return page; } -function log_console(page) { +function logConsole(page) { let buffer = ""; page.on('console', msg => { buffer += msg.text() + '\n'; @@ -46,7 +46,7 @@ function log_console(page) { } } -function log_xhr_requests(page) { +function logXHRRequests(page) { let buffer = ""; page.on('request', req => { const type = req.resourceType(); @@ -64,16 +64,16 @@ function log_xhr_requests(page) { } } -async function get_outer_html(element_handle) { +async function getOuterHTML(element_handle) { const html_handle = await element_handle.getProperty('outerHTML'); return await html_handle.jsonValue(); } -async function print_elements(label, elements) { - console.log(label, await Promise.all(elements.map(get_outer_html))); +async function printElements(label, elements) { + console.log(label, await Promise.all(elements.map(getOuterHTML))); } -async function replace_input_text(input, text) { +async function replaceInputText(input, text) { // click 3 times to select all text await input.click({clickCount: 3}); // then remove it with backspace @@ -82,18 +82,18 @@ async function replace_input_text(input, text) { await input.type(text); } -async function wait_and_query_selector(page, selector, timeout = 500) { +async function waitAndQuerySelector(page, selector, timeout = 500) { await page.waitForSelector(selector, {visible: true, timeout}); return await page.$(selector); } // other helpers -function rnd_int(max) { +function randomInt(max) { return Math.ceil(Math.random()*max); } -function riot_url(path) { +function riotUrl(path) { return riotserver + path; } @@ -102,15 +102,15 @@ function delay(ms) { } module.exports = { - try_get_innertext, - new_page, - log_console, - log_xhr_requests, - get_outer_html, - print_elements, - replace_input_text, - wait_and_query_selector, - rnd_int, - riot_url, + tryGetInnertext, + newPage, + logConsole, + logXHRRequests, + getOuterHTML, + printElements, + replaceInputText, + waitAndQuerySelector, + randomInt, + riotUrl, delay, } \ No newline at end of file diff --git a/start.js b/start.js index 002019438a..82b567c566 100644 --- a/start.js +++ b/start.js @@ -17,53 +17,46 @@ limitations under the License. const puppeteer = require('puppeteer'); const helpers = require('./helpers'); const assert = require('assert'); -const do_signup = require('./tests/signup'); -const test_title = require('./tests/loads'); -const join_room = require('./tests/join_room'); + +const signup = require('./tests/signup'); +const join = require('./tests/join'); global.riotserver = 'http://localhost:8080'; global.homeserver = 'http://localhost:8008'; global.browser = null; -async function run_tests() { - await start_session(); - - process.stdout.write(`* testing riot loads ... `); - await test_title(); - process.stdout.write('done\n'); - - - - const page = await helpers.new_page(); - const username = 'bruno-' + helpers.rnd_int(10000); +async function runTests() { + await startSession(); + const page = await helpers.newPage(); + const username = 'bruno-' + helpers.randomInt(10000); const password = 'testtest'; process.stdout.write(`* signing up as ${username} ... `); - await do_signup(page, username, password, homeserver); + await signup(page, username, password, homeserver); process.stdout.write('done\n'); const room = 'test'; process.stdout.write(`* joining room ${room} ... `); - await join_room(page, room); + await join(page, room); process.stdout.write('done\n'); - await end_session(); + await endSession(); } -async function start_session() { +async function startSession() { global.browser = await puppeteer.launch(); } -function end_session() { +function endSession() { return browser.close(); } -function on_success() { +function onSuccess() { console.log('all tests finished successfully'); } -function on_failure(err) { +function onFailure(err) { console.log('failure: ', err); process.exit(-1); } -run_tests().then(on_success, on_failure); \ No newline at end of file +runTests().then(onSuccess, onFailure); \ No newline at end of file diff --git a/tests/join_room.js b/tests/join.js similarity index 51% rename from tests/join_room.js rename to tests/join.js index 7975fad648..ea16a93936 100644 --- a/tests/join_room.js +++ b/tests/join.js @@ -17,19 +17,19 @@ limitations under the License. const helpers = require('../helpers'); const assert = require('assert'); -module.exports = async function join_room(page, room_name) { +module.exports = async function join(page, roomName) { //TODO: brittle selector - const directory_button = await helpers.wait_and_query_selector(page, '.mx_RoleButton[aria-label="Room directory"]'); - await directory_button.click(); + const directoryButton = await helpers.waitAndQuerySelector(page, '.mx_RoleButton[aria-label="Room directory"]'); + await directoryButton.click(); - const room_input = await helpers.wait_and_query_selector(page, '.mx_DirectorySearchBox_input'); - await helpers.replace_input_text(room_input, room_name); + const roomInput = await helpers.waitAndQuerySelector(page, '.mx_DirectorySearchBox_input'); + await helpers.replaceInputText(roomInput, roomName); - const first_room_label = await helpers.wait_and_query_selector(page, '.mx_RoomDirectory_table .mx_RoomDirectory_name:first-child'); - await first_room_label.click(); + const firstRoomLabel = await helpers.waitAndQuerySelector(page, '.mx_RoomDirectory_table .mx_RoomDirectory_name:first-child'); + await firstRoomLabel.click(); - const join_link = await helpers.wait_and_query_selector(page, '.mx_RoomPreviewBar_join_text a'); - await join_link.click(); + const joinLink = await helpers.waitAndQuerySelector(page, '.mx_RoomPreviewBar_join_text a'); + await joinLink.click(); await page.waitForSelector('.mx_MessageComposer'); } \ No newline at end of file diff --git a/tests/loads.js b/tests/loads.js deleted file mode 100644 index 7136b934db..0000000000 --- a/tests/loads.js +++ /dev/null @@ -1,25 +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. -*/ - -const helpers = require('../helpers'); -const assert = require('assert'); - -module.exports = async function test_title() { - const page = await browser.newPage(); - await page.goto(helpers.riot_url('/')); - const title = await page.title(); - assert.strictEqual(title, "Riot"); -}; \ No newline at end of file diff --git a/tests/signup.js b/tests/signup.js index 155c5a1e0a..43fe6a87d4 100644 --- a/tests/signup.js +++ b/tests/signup.js @@ -17,62 +17,62 @@ limitations under the License. const helpers = require('../helpers'); const assert = require('assert'); -module.exports = async function do_signup(page, username, password, homeserver) { - const console_logs = helpers.log_console(page); - const xhr_logs = helpers.log_xhr_requests(page); - await page.goto(helpers.riot_url('/#/register')); +module.exports = async function signup(page, username, password, homeserver) { + const consoleLogs = helpers.logConsole(page); + const xhrLogs = helpers.logXHRRequests(page); + await page.goto(helpers.riotUrl('/#/register')); //click 'Custom server' radio button await page.waitForSelector('#advanced', {visible: true, timeout: 500}); await page.click('#advanced'); //fill out form await page.waitForSelector('.mx_ServerConfig', {visible: true, timeout: 500}); - const login_fields = await page.$$('.mx_Login_field'); - assert.strictEqual(login_fields.length, 7); - const username_field = login_fields[2]; - const password_field = login_fields[3]; - const password_repeat_field = login_fields[4]; - const hsurl_field = login_fields[5]; - await helpers.replace_input_text(username_field, username); - await helpers.replace_input_text(password_field, password); - await helpers.replace_input_text(password_repeat_field, password); - await helpers.replace_input_text(hsurl_field, homeserver); + const loginFields = await page.$$('.mx_Login_field'); + assert.strictEqual(loginFields.length, 7); + const usernameField = loginFields[2]; + const passwordField = loginFields[3]; + const passwordRepeatField = loginFields[4]; + const hsurlField = loginFields[5]; + await helpers.replaceInputText(usernameField, username); + await helpers.replaceInputText(passwordField, password); + await helpers.replaceInputText(passwordRepeatField, password); + await helpers.replaceInputText(hsurlField, homeserver); //wait over a second because Registration/ServerConfig have a 1000ms //delay to internally set the homeserver url //see Registration::render and ServerConfig::props::delayTimeMs await helpers.delay(1200); /// focus on the button to make sure error validation /// has happened before checking the form is good to go - const register_button = await page.$('.mx_Login_submit'); - await register_button.focus(); + const registerButton = await page.$('.mx_Login_submit'); + await registerButton.focus(); //check no errors - const error_text = await helpers.try_get_innertext(page, '.mx_Login_error'); + const error_text = await helpers.tryGetInnertext(page, '.mx_Login_error'); assert.strictEqual(!!error_text, false); //submit form await page.screenshot({path: "beforesubmit.png", fullPage: true}); - await register_button.click(); + await registerButton.click(); //confirm dialog saying you cant log back in without e-mail await page.waitForSelector('.mx_QuestionDialog', {visible: true, timeout: 500}); - const continue_button = await page.$('.mx_QuestionDialog button.mx_Dialog_primary'); - //await helpers.print_elements('continue_button', [continue_button]); - await continue_button.click(); + const continueButton = await page.$('.mx_QuestionDialog button.mx_Dialog_primary'); + //await helpers.printElements('continueButton', [continueButton]); + await continueButton.click(); //wait for registration to finish so the hash gets set //onhashchange better? await helpers.delay(1000); /* await page.screenshot({path: "afterlogin.png", fullPage: true}); console.log('browser console logs:'); - console.log(console_logs.logs()); + console.log(consoleLogs.logs()); console.log('xhr logs:'); - console.log(xhr_logs.logs()); + console.log(xhrLogs.logs()); */ - //print_elements('page', await page.$('#matrixchat')); + //printElements('page', await page.$('#matrixchat')); // await navigation_promise; //await page.waitForSelector('.mx_MatrixChat', {visible: true, timeout: 3000}); const url = page.url(); - assert.strictEqual(url, helpers.riot_url('/#/home')); + assert.strictEqual(url, helpers.riotUrl('/#/home')); } \ No newline at end of file