Skip to content

Commit

Permalink
Added attaching logs for failed PW tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EvidentlyCube committed Nov 30, 2023
1 parent 7838109 commit ba1ab5c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
37 changes: 32 additions & 5 deletions tests-pw/common/core/BaseTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EditionSelector } from './EditionSelector';
import { TiddlyWikiUi } from '../ui/TiddlyWikiUi';
import { TiddlerStore } from './TiddlerStore';
import { TiddlyWikiConfig } from './TiddlyWikiConfig';
import { pageRegisterConsoleLogger } from '../utils/PageUtils';

// --------------------
// NOTES.md#002
Expand All @@ -19,18 +20,44 @@ import { TiddlyWikiConfig } from './TiddlyWikiConfig';

/** @type {base.Fixtures<TiddlyWikiTestFixtures, {}, base.PlaywrightTestArgs, base.PlaywrightWorkerArgs>} */
export const baseTestFixtures = {
store: async({page}, use) => {
page: async ({ page }, use, testInfo) => {
const getConsoleLogs = pageRegisterConsoleLogger(page);

await use(page);

if (testInfo.errors.length > 0) {
await testInfo.attach('Console Logs', {
body: getConsoleLogs().map(messageToString).join('\n\n'),
contentType: 'text/plain',
});
}
},
store: async ({ page }, use) => {
await use(new TiddlerStore(page));
},
twConfig: async({page, store}, use) => {
twConfig: async ({ page, store }, use) => {
await use(new TiddlyWikiConfig(page, store));
},
selectEdition: async ({page}, use) => {
selectEdition: async ({ page }, use) => {
await use(new EditionSelector(page));
},
ui: async ({page}, use) => {
ui: async ({ page }, use) => {
await use(new TiddlyWikiUi(page));
}
};

export const baseTest = base.test.extend(baseTestFixtures);
/**
* @param {import('../utils/PageUtils').PageConsoleMessage} data
* @returns {string}
*/
function messageToString(data) {
const { pageId, message } = data;

if (message) {
return `<Page#${pageId}> [${message.type()}] ${message.location().url}#${message.location().lineNumber}:${message.location().columnNumber}\n${message.text()}`;
} else {
return `<Page#${pageId}> Page Opened`;
}
}

export const baseTest = base.test.extend(baseTestFixtures);
43 changes: 40 additions & 3 deletions tests-pw/common/utils/PageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import test from "playwright/test";

/**
* @typedef {Object} PageConsoleMessage
* @property {number} pageId
* @property {import("@playwright/test").ConsoleMessage} [message]
*/

/**
* Calls a function that contains page interactions that should lead to a new page being open.
* It then returns the newly opened page.
*
* @param {import("playwright/test").Page} page
* @param {import("playwright/test").Page} sourcePage
* @param {function():Promise<void>} interactionCallback
* @returns {Promise<import("playwright/test").Page>}
*/
export async function getNewPage(page, interactionCallback) {
export async function getNewPage(sourcePage, interactionCallback) {
return await test.step('Attempting to extract new page that is supposed to open', async () => {
const waitForPagePromise = page.context().waitForEvent('page');
const waitForPagePromise = sourcePage.context().waitForEvent('page');

await interactionCallback();

Expand All @@ -21,4 +27,35 @@ export async function getNewPage(page, interactionCallback) {

return newPage;
});
}

/**
* Calls a function that contains page interactions that should lead to a new page being open.
* It then returns the newly opened page.
*
* @param {import("playwright/test").Page} page
* @returns {() => PageConsoleMessage[]}
*/
export function pageRegisterConsoleLogger(page) {
/**
* @type {PageConsoleMessage[]}
*/
const logs = [{ pageId: 0 }];

page.on('console', message => {
logs.push({ pageId: 0, message });
});

let pageCounter = 1;
page.context().on('page', newPage => {
const pageId = pageCounter++;

logs.push({ pageId });

newPage.on('console', message => {
logs.push({ pageId, message });
});
})

return () => logs;
}
1 change: 0 additions & 1 deletion tests-pw/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface TW_Wiki {
deleteTiddler(title: string): void;
}


interface TW {
Tiddler: typeof TW_Tiddler;
wiki: TW_Wiki;
Expand Down
2 changes: 1 addition & 1 deletion tests-pw/specs/AutoComplete/TextInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ EditionSelector.getEditions(false).forEach(edition => {
await inputSecondary.fill('');
await inputSecondary.pressSequentially('[[1');

await pageSecondary.pause();
const selectedText = (await autoCompleteSecondary.selectedLink.textContent()).trim();

await autoCompleteSecondary.selectedLink.click();
await expect(inputPrimary).toHaveValue(`[[${selectedText}]]`);
await expect(inputSecondary).toHaveValue(`[[${selectedText}]]`);
Expand Down

0 comments on commit ba1ab5c

Please sign in to comment.