auth.js
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 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;
}