80 lines
2.4 KiB
JavaScript
80 lines
2.4 KiB
JavaScript
/**
|
|
* uni-app H5 + Playwright 公共操作(登录页起复用)
|
|
*/
|
|
export function env() {
|
|
return {
|
|
base: process.env.E2E_BASE || 'http://localhost:5173',
|
|
user: process.env.E2E_USER || 'admin',
|
|
pass: process.env.E2E_PASS || 'admin123'
|
|
}
|
|
}
|
|
|
|
export async function clearSession(page) {
|
|
await page.goto(env().base + '/', { waitUntil: 'domcontentloaded' })
|
|
await page.evaluate(() => {
|
|
try {
|
|
localStorage.clear()
|
|
sessionStorage.clear()
|
|
} catch (_) {}
|
|
})
|
|
}
|
|
|
|
export async function openLoginFresh(page) {
|
|
await clearSession(page)
|
|
await page.goto(env().base + '/', { waitUntil: 'networkidle' })
|
|
await page.waitForTimeout(800)
|
|
// 若仍被跳到工作台,再清一次
|
|
if (page.url().includes('workbench')) {
|
|
await clearSession(page)
|
|
await page.goto(env().base + '/pages/login/login', { waitUntil: 'networkidle' })
|
|
await page.waitForTimeout(800)
|
|
}
|
|
}
|
|
|
|
export function userInput(page) {
|
|
return page.locator('input.uni-input-input[type="text"]').first()
|
|
}
|
|
|
|
export function passInput(page) {
|
|
return page.locator('input.uni-input-input[type="password"]').first()
|
|
}
|
|
|
|
export async function fillLogin(page, username, password) {
|
|
await userInput(page).fill(username ?? '')
|
|
await passInput(page).fill(password ?? '')
|
|
}
|
|
|
|
export async function clickLoginButton(page) {
|
|
await page.locator('uni-button.btn-login, button.btn-login').first().click({ force: true })
|
|
}
|
|
|
|
export async function clickAgreeCheck(page) {
|
|
await page.locator('.agree-check').first().click({ force: true })
|
|
}
|
|
|
|
export async function waitToast(page, textRe, timeout = 5000) {
|
|
// uni-app H5 toast 常见:.uni-sample-toast / .uni-toast / uni-toast
|
|
const toast = page.locator('.uni-sample-toast, .uni-toast, uni-toast, .uni-toast__content').filter({
|
|
hasText: textRe
|
|
})
|
|
await toast.first().waitFor({ state: 'visible', timeout })
|
|
return toast.first()
|
|
}
|
|
|
|
export async function isOnLogin(page) {
|
|
const url = page.url()
|
|
if (url.includes('/pages/login/login') || url.endsWith('/') || url.match(/:5173\/?$/)) {
|
|
const btn = await page.getByText('账号密码登录').first().isVisible().catch(() => false)
|
|
const loading = await page.getByText('登录中...').first().isVisible().catch(() => false)
|
|
return btn || loading
|
|
}
|
|
return false
|
|
}
|
|
|
|
export async function isOnWorkbench(page) {
|
|
return (
|
|
page.url().includes('/pages/workbench/workbench') &&
|
|
(await page.getByText('工作台', { exact: true }).first().isVisible().catch(() => false))
|
|
)
|
|
}
|