36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import { APIRequestContext } from '@playwright/test'
|
|
|
|
export const ADMIN = { username: 'admin', password: 'Admin@2026' }
|
|
export const API_BASE = 'http://localhost:8080/api/v1'
|
|
|
|
export interface LoginData {
|
|
accessToken: string
|
|
refreshToken?: string
|
|
username?: string
|
|
roleName?: string
|
|
userId?: number
|
|
}
|
|
|
|
export async function apiLogin(request: APIRequestContext, user = ADMIN): Promise<LoginData> {
|
|
const res = await request.post(`${API_BASE}/auth/login`, {
|
|
data: { username: user.username, password: user.password },
|
|
})
|
|
if (!res.ok()) {
|
|
throw new Error(`login failed: ${res.status()} ${await res.text()}`)
|
|
}
|
|
const json = await res.json()
|
|
return json.data
|
|
}
|
|
|
|
export async function uiLogin(page: import('@playwright/test').Page, user = ADMIN) {
|
|
await page.goto('/login')
|
|
await page.getByPlaceholder(/用户名|账号|username/i).fill(user.username)
|
|
await page.getByPlaceholder(/密码|password/i).fill(user.password)
|
|
await page.getByRole('button', { name: /登\s*录/i }).click()
|
|
await page.waitForURL(/\/dashboard/)
|
|
}
|
|
|
|
export function authHeaders(token: string) {
|
|
return { Authorization: `Bearer ${token}` }
|
|
}
|