2022-09-02 19:18:23 +00:00
|
|
|
import {expect} from '@playwright/test';
|
2024-06-28 16:15:51 +00:00
|
|
|
import {env} from 'node:process';
|
2022-09-02 19:18:23 +00: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.
|
|
|
|
export async function login_user(browser, workerInfo, user) {
|
|
|
|
// Set up a new context
|
|
|
|
const context = await browser.newContext();
|
|
|
|
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-07-03 15:48:14 +00:00
|
|
|
expect(response?.status()).toBe(200); // Status OK
|
2022-09-02 19:18:23 +00:00
|
|
|
|
|
|
|
// Fill out form
|
|
|
|
await page.type('input[name=user_name]', user);
|
|
|
|
await page.type('input[name=password]', LOGIN_PASSWORD);
|
2023-09-18 22:05:31 +00:00
|
|
|
await page.click('form button.ui.primary.button:visible');
|
2022-09-02 19:18:23 +00:00
|
|
|
|
2024-06-28 16:15:51 +00:00
|
|
|
await page.waitForLoadState('networkidle'); // eslint-disable-line playwright/no-networkidle
|
2022-09-02 19:18:23 +00:00
|
|
|
|
2024-07-03 15:48:14 +00:00
|
|
|
expect(page.url(), {message: `Failed to login user ${user}`}).toBe(`${workerInfo.project.use.baseURL}/`);
|
2022-09-02 19:18:23 +00:00
|
|
|
|
|
|
|
// Save state
|
|
|
|
await context.storageState({path: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`});
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function load_logged_in_context(browser, workerInfo, user) {
|
|
|
|
let context;
|
|
|
|
try {
|
|
|
|
context = await browser.newContext({storageState: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`});
|
|
|
|
} 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()?`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function save_visual(page) {
|
|
|
|
// Optionally include visual testing
|
2024-06-28 16:15:51 +00:00
|
|
|
if (env.VISUAL_TEST) {
|
|
|
|
await page.waitForLoadState('networkidle'); // eslint-disable-line playwright/no-networkidle
|
2022-09-02 19:18:23 +00: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-22 23:54:09 +00:00
|
|
|
page.locator('.secondary-nav span>img.ui.avatar'),
|
2023-03-24 10:35:38 +00:00
|
|
|
page.locator('.ui.dropdown.jump.item span>img.ui.avatar'),
|
2022-09-02 19:18:23 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|