auth.js 1.21 KB
/**
 * E2E 登录辅助(Vuex 内存登录态)
 *
 * 本项目路由守卫依赖 store 内存,不能用 storageState 跨 context。
 * 新用例请在 beforeAll 中调用 createLoggedInPage,整文件复用返回的 page。
 */

export function getBaseUrl() {
	return (process.env.BASE_URL || "http://192.168.1.146:8080/dj-carlife-web/").replace(
		/\/$/,
		"",
	);
}

export async function login(page, {
	account = "glsadmin",
	password = "123456",
} = {}) {
	const baseUrl = getBaseUrl();
	await page.goto(`${baseUrl}/#/login`);
	await page.getByPlaceholder("账户").fill(account);
	await page.getByPlaceholder("密码").fill(password);
	await page.getByRole("button", { name: "登录" }).click();
	await page.waitForURL(/#\/index/);
	await page.waitForLoadState("networkidle");
}

/**
 * 创建已登录的共享 page(beforeAll 调用一次即可)
 * @returns {Promise<import('@playwright/test').Page>}
 */
export async function createLoggedInPage(browser, options = {}) {
	const context = await browser.newContext();
	const page = await context.newPage();
	page.setDefaultTimeout(options.timeout || 10000);
	page.setDefaultNavigationTimeout(options.navigationTimeout || 30000);
	await login(page, options);
	return page;
}