moving testsuite console log to helpers.js

This commit is contained in:
Gabriel Porfirio 2023-07-14 11:57:42 -03:00
parent 29c86b5300
commit 972b907f5c
2 changed files with 83 additions and 84 deletions

View File

@ -6,6 +6,9 @@ const xml2js = require('xml2js');
const { runScript } = require('./util');
const { env } = require('node:process');
const { format } = require('node:util');
// This is version 4 of chalk, not version 5, which uses ESM
const chalk = require('chalk');
const parameters = require('./parameters');
function getRandomInt(min, max) {
@ -50,19 +53,6 @@ async function createMeeting(params, createParameter, page) {
const response = await promise;
expect(response.status).toEqual(200);
const xmlResponse = await xml2js.parseStringPromise(response.data);
if (env.CONSOLE !== undefined) {
const CONSOLE_strings = env.CONSOLE.split(',').map(opt => opt.trim().toLowerCase());
const CONSOLE_options = {
colorize: CONSOLE_strings.includes('color') || CONSOLE_strings.includes('colour'),
drop_references: CONSOLE_strings.includes('norefs'),
drop_timestamps: CONSOLE_strings.includes('nots'),
line_label: CONSOLE_strings.includes('label') ? this.username + " " : undefined,
noClientLogger: CONSOLE_strings.includes('nocl') || CONSOLE_strings.includes('noclientlogger'),
};
page.on('console', async (msg) => console.log(await console_format(msg, CONSOLE_options)));
}
return xmlResponse.response.meetingID[0];
}
@ -82,6 +72,83 @@ async function checkRootPermission() {
await expect(checkSudo, 'Sudo failed: need to run this test with root permission (can be fixed by running "sudo -v" and entering the password)').toBeTruthy();
}
async function console_format(msg, CONSOLE_options) {
const args = await Promise.all(msg.args().map(itm => itm.jsonValue()));
// For Chrome, args[0] is a format string that we will process using
// node.js's util.format, but that function discards css style
// information from "%c" format specifiers. So first loop over the
// format string, replacing every "%c" with "%s" and replacing the
// corresponding css style with an ANSI color sequence.
//
// See https://console.spec.whatwg.org/ sections 2.2.1 and 2.3.4
let split_arg0 = args[0].split("%");
for (let i = 1, j = 1; i < split_arg0.length; i++, j++) {
if (split_arg0[i].startsWith('c')) {
split_arg0[i] = 's' + split_arg0[i].substr(1);
const styles = args[j].split(';');
args[j] = '';
for (const style of styles) {
const stdStyle = style.trim().toLowerCase();
if (stdStyle.startsWith('color:') && CONSOLE_options.colorize) {
const color = stdStyle.substr(6).trim();
args[j] = chalk.keyword(color)._styler.open;
} else if (stdStyle.startsWith('font-size:') && CONSOLE_options.drop_references) {
// For Chrome, we "drop references" by discarding everything after a font size change
split_arg0.length = i;
args.length = j;
}
}
} else if (split_arg0[i] == "") {
// format is "%%", so don't do special processing for
// split_arg0[i+1], and only increment i, not j
i++; // NOSONAR
}
}
args[0] = split_arg0.join('%');
// see playwright consoleMessage class documentation
let result = format(...args);
if (CONSOLE_options.drop_references) {
// For Firefox, we "drop references" by discarding a URL at the end of the line
result = result.replace(/https:\/\/\S*$/, '');
}
if (CONSOLE_options.noClientLogger) {
result = result.replace(/clientLogger: /, '');
}
if (CONSOLE_options.drop_timestamps) {
// timestamp formatting is a bit complicated, with four "%s" fields and corresponding arguments,
// so just filter them out (if requested) after all the other formatting is done
result = result.replace(/\[\d\d:\d\d:\d\d:\d\d\d\d\] /, '');
}
if (CONSOLE_options.line_label) {
if (CONSOLE_options.colorize) {
result = chalk.keyword('green')(CONSOLE_options.line_label) + result;
} else {
result = CONSOLE_options.line_label + result;
}
}
return result;
}
async function setBrowserLogs(page) {
const CONSOLE_strings = env.CONSOLE.split(',').map(opt => opt.trim().toLowerCase());
const CONSOLE_options = {
colorize: CONSOLE_strings.includes('color') || CONSOLE_strings.includes('colour'),
drop_references: CONSOLE_strings.includes('norefs'),
drop_timestamps: CONSOLE_strings.includes('nots'),
line_label: CONSOLE_strings.includes('label') ? this.username + " " : undefined,
noClientLogger: CONSOLE_strings.includes('nocl') || CONSOLE_strings.includes('noclientlogger'),
};
page.on('console', async (msg) => console.log(await console_format(msg, CONSOLE_options)));
}
function linkIssue(issueNumber) {
test.info().annotations.push({
type: 'Issue/PR',
@ -105,3 +172,4 @@ exports.getJoinURL = getJoinURL;
exports.checkRootPermission = checkRootPermission;
exports.linkIssue = linkIssue;
exports.sleep = sleep;
exports.setBrowserLogs = setBrowserLogs;

View File

@ -1,10 +1,6 @@
require('dotenv').config();
const { expect, default: test } = require('@playwright/test');
const { readFileSync } = require('fs');
const { format } = require('node:util');
// This is version 4 of chalk, not version 5, which uses ESM
const chalk = require('chalk');
const parameters = require('./parameters');
const helpers = require('./helpers');
@ -14,73 +10,6 @@ const { ELEMENT_WAIT_TIME, ELEMENT_WAIT_LONGER_TIME, VIDEO_LOADING_WAIT_TIME } =
const { checkElement, checkElementLengthEqualTo } = require('./util');
const { generateSettingsData, getSettings } = require('./settings');
function formatWithCss(CONSOLE_options, ...args) {
// For Chrome, args[0] is a format string that we will process using
// node.js's util.format, but that function discards css style
// information from "%c" format specifiers. So first loop over the
// format string, replacing every "%c" with "%s" and replacing the
// corresponding css style with an ANSI color sequence.
//
// See https://console.spec.whatwg.org/ sections 2.2.1 and 2.3.4
let split_arg0 = args[0].split("%");
for (let i = 1, j = 1; i < split_arg0.length; i++, j++) {
if (split_arg0[i].startsWith('c')) {
split_arg0[i] = 's' + split_arg0[i].substr(1);
const styles = args[j].split(';');
args[j] = '';
for (const style of styles) {
const stdStyle = style.trim().toLowerCase();
if (stdStyle.startsWith('color:') && CONSOLE_options.colorize) {
const color = stdStyle.substr(6).trim();
args[j] = chalk.keyword(color)._styler.open;
} else if (stdStyle.startsWith('font-size:') && CONSOLE_options.drop_references) {
// For Chrome, we "drop references" by discarding everything after a font size change
split_arg0.length = i;
args.length = j;
}
}
} else if (split_arg0[i] == "") {
// format is "%%", so don't do special processing for
// split_arg0[i+1], and only increment i, not j
i++; // NOSONAR
}
}
args[0] = split_arg0.join('%');
return format(...args);
}
async function console_format(msg, CONSOLE_options) {
// see playwright consoleMessage class documentation
const args = await Promise.all(msg.args().map(itm => itm.jsonValue()));
let result = formatWithCss(CONSOLE_options, ...args);
if (CONSOLE_options.drop_references) {
// For Firefox, we "drop references" by discarding a URL at the end of the line
result = result.replace(/https:\/\/\S*$/, '');
}
if (CONSOLE_options.noClientLogger) {
result = result.replace(/clientLogger: /, '');
}
if (CONSOLE_options.drop_timestamps) {
// timestamp formatting is a bit complicated, with four "%s" fields and corresponding arguments,
// so just filter them out (if requested) after all the other formatting is done
result = result.replace(/\[\d\d:\d\d:\d\d:\d\d\d\d\] /, '');
}
if (CONSOLE_options.line_label) {
if (CONSOLE_options.colorize) {
result = chalk.keyword('green')(CONSOLE_options.line_label) + result;
} else {
result = CONSOLE_options.line_label + result;
}
}
return result;
}
class Page {
constructor(browser, page) {
this.browser = browser;
@ -103,6 +32,8 @@ class Page {
if (!isModerator) this.initParameters.moderatorPW = '';
if (fullName) this.initParameters.fullName = fullName;
this.username = this.initParameters.fullName;
if (env.CONSOLE !== undefined) await helpers.setBrowserLogs(this.page);
this.meetingId = (meetingId) ? meetingId : await helpers.createMeeting(parameters, createParameter, customMeetingId, this.page);
const joinUrl = helpers.getJoinURL(this.meetingId, this.initParameters, isModerator, joinParameter);