2024-11-08 11:55:54 +03:00
|
|
|
import {expect, test as baseTest, type Browser, type BrowserContextOptions, type APIRequestContext, type TestInfo, type Page} from '@playwright/test';
|
2024-08-15 00:34:36 +03:00
|
|
|
|
|
|
|
export const test = baseTest.extend({
|
|
|
|
context: async ({browser}, use) => {
|
|
|
|
return use(await test_context(browser));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-11-08 11:55:54 +03:00
|
|
|
async function test_context(browser: Browser, options?: BrowserContextOptions) {
|
2024-08-15 00:34:36 +03:00
|
|
|
const context = await browser.newContext(options);
|
|
|
|
|
|
|
|
context.on('page', (page) => {
|
|
|
|
page.on('pageerror', (err) => expect(err).toBeUndefined());
|
|
|
|
});
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
2022-09-02 22:18:23 +03:00
|
|
|
|
|
|
|
const ARTIFACTS_PATH = `tests/e2e/test-artifacts`;
|
|
|
|
const LOGIN_PASSWORD = 'password';
|
|
|
|
|
|
|
|
// log in user and store session info. This should generally be
|
|
|
|
// run in test.beforeAll(), then the session can be loaded in tests.
|
2024-11-08 11:55:54 +03:00
|
|
|
export async function login_user(browser: Browser, workerInfo: TestInfo, user: string) {
|
2024-08-27 16:36:39 +03:00
|
|
|
test.setTimeout(60000);
|
2022-09-02 22:18:23 +03:00
|
|
|
// Set up a new context
|
2024-08-15 00:34:36 +03:00
|
|
|
const context = await test_context(browser);
|
2022-09-02 22:18:23 +03:00
|
|
|
const page = await context.newPage();
|
|
|
|
|
|
|
|
// Route to login page
|
|
|
|
// Note: this could probably be done more quickly with a POST
|
|
|
|
const response = await page.goto('/user/login');
|
2024-10-23 17:22:25 +03:00
|
|
|
expect(response?.status()).toBe(200); // Status OK
|
2022-09-02 22:18:23 +03:00
|
|
|
|
|
|
|
// Fill out form
|
2024-11-15 03:41:55 +03:00
|
|
|
await page.fill('input[name=user_name]', user);
|
|
|
|
await page.fill('input[name=password]', LOGIN_PASSWORD);
|
2023-09-19 01:05:31 +03:00
|
|
|
await page.click('form button.ui.primary.button:visible');
|
2022-09-02 22:18:23 +03:00
|
|
|
|
2024-11-12 23:07:09 +03:00
|
|
|
await page.waitForLoadState();
|
2022-09-02 22:18:23 +03:00
|
|
|
|
2024-10-23 17:22:25 +03:00
|
|
|
expect(page.url(), {message: `Failed to login user ${user}`}).toBe(`${workerInfo.project.use.baseURL}/`);
|
2022-09-02 22:18:23 +03:00
|
|
|
|
|
|
|
// Save state
|
|
|
|
await context.storageState({path: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`});
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2024-11-08 11:55:54 +03:00
|
|
|
export async function load_logged_in_context(browser: Browser, workerInfo: TestInfo, user: string) {
|
2022-09-02 22:18:23 +03:00
|
|
|
try {
|
2024-11-15 03:41:55 +03:00
|
|
|
return await test_context(browser, {storageState: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`});
|
2022-09-02 22:18:23 +03:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
throw new Error(`Could not find state for '${user}'. Did you call login_user(browser, workerInfo, '${user}') in test.beforeAll()?`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 11:55:54 +03:00
|
|
|
export async function login({browser}: {browser: Browser}, workerInfo: TestInfo) {
|
2024-08-17 21:24:31 +03:00
|
|
|
const context = await load_logged_in_context(browser, workerInfo, 'user2');
|
2024-11-08 11:55:54 +03:00
|
|
|
return await context?.newPage();
|
2024-08-17 21:24:31 +03:00
|
|
|
}
|
|
|
|
|
2024-11-08 11:55:54 +03:00
|
|
|
export async function save_visual(page: Page) {
|
2022-09-02 22:18:23 +03:00
|
|
|
// Optionally include visual testing
|
|
|
|
if (process.env.VISUAL_TEST) {
|
2024-11-12 23:07:09 +03:00
|
|
|
await page.waitForLoadState('domcontentloaded');
|
2022-09-02 22:18:23 +03:00
|
|
|
// Mock page/version string
|
|
|
|
await page.locator('footer div.ui.left').evaluate((node) => node.innerHTML = 'MOCK');
|
|
|
|
await expect(page).toHaveScreenshot({
|
|
|
|
fullPage: true,
|
|
|
|
timeout: 20000,
|
|
|
|
mask: [
|
2024-03-23 02:54:09 +03:00
|
|
|
page.locator('.secondary-nav span>img.ui.avatar'),
|
2023-03-24 13:35:38 +03:00
|
|
|
page.locator('.ui.dropdown.jump.item span>img.ui.avatar'),
|
2022-09-02 22:18:23 +03:00
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-10-19 23:40:08 +03:00
|
|
|
|
|
|
|
// Create a temporary user and login to that user and store session info.
|
|
|
|
// This should ideally run on a per test basis.
|
2024-11-08 11:55:54 +03:00
|
|
|
export async function create_temp_user(browser: Browser, workerInfo: TestInfo, request: APIRequestContext) {
|
2024-10-19 23:40:08 +03:00
|
|
|
const username = globalThis.crypto.randomUUID();
|
|
|
|
const newUser = await request.post(`/api/v1/admin/users`, {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Basic ${btoa(`user1:${LOGIN_PASSWORD}`)}`,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
username,
|
|
|
|
email: `${username}@host.invalid`,
|
|
|
|
password: LOGIN_PASSWORD,
|
|
|
|
must_change_password: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(newUser.ok()).toBeTruthy();
|
|
|
|
|
|
|
|
return {context: await login_user(browser, workerInfo, username), username};
|
|
|
|
}
|