code.gitea.io/gitea@v1.21.7/tests/e2e/utils_e2e.js (about) 1 import {expect} from '@playwright/test'; 2 3 const ARTIFACTS_PATH = `tests/e2e/test-artifacts`; 4 const LOGIN_PASSWORD = 'password'; 5 6 // log in user and store session info. This should generally be 7 // run in test.beforeAll(), then the session can be loaded in tests. 8 export async function login_user(browser, workerInfo, user) { 9 // Set up a new context 10 const context = await browser.newContext(); 11 const page = await context.newPage(); 12 13 // Route to login page 14 // Note: this could probably be done more quickly with a POST 15 const response = await page.goto('/user/login'); 16 await expect(response?.status()).toBe(200); // Status OK 17 18 // Fill out form 19 await page.type('input[name=user_name]', user); 20 await page.type('input[name=password]', LOGIN_PASSWORD); 21 await page.click('form button.ui.primary.button:visible'); 22 23 await page.waitForLoadState('networkidle'); 24 25 await expect(page.url(), {message: `Failed to login user ${user}`}).toBe(`${workerInfo.project.use.baseURL}/`); 26 27 // Save state 28 await context.storageState({path: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`}); 29 30 return context; 31 } 32 33 export async function load_logged_in_context(browser, workerInfo, user) { 34 let context; 35 try { 36 context = await browser.newContext({storageState: `${ARTIFACTS_PATH}/state-${user}-${workerInfo.workerIndex}.json`}); 37 } catch (err) { 38 if (err.code === 'ENOENT') { 39 throw new Error(`Could not find state for '${user}'. Did you call login_user(browser, workerInfo, '${user}') in test.beforeAll()?`); 40 } 41 } 42 return context; 43 } 44 45 export async function save_visual(page) { 46 // Optionally include visual testing 47 if (process.env.VISUAL_TEST) { 48 await page.waitForLoadState('networkidle'); 49 // Mock page/version string 50 await page.locator('footer div.ui.left').evaluate((node) => node.innerHTML = 'MOCK'); 51 await expect(page).toHaveScreenshot({ 52 fullPage: true, 53 timeout: 20000, 54 mask: [ 55 page.locator('.dashboard-navbar span>img.ui.avatar'), 56 page.locator('.ui.dropdown.jump.item span>img.ui.avatar'), 57 ], 58 }); 59 } 60 }