Files
devstar/tests/e2e/global-setup.ts

48 lines
2.1 KiB
TypeScript

import { chromium, type FullConfig } from '@playwright/test';
import { env } from 'node:process';
import { URL } from 'url';
async function globalSetup(config: FullConfig) {
const mode = env.E2E_MODE;
const {baseURL} = config.projects[0].use;
const isInstalledMode=env.E2E_SKIP_INSTALL;
const DEFAULT_E2E_USER = 'testuser';
const DEFAULT_E2E_PASS = '12345678';
if (!baseURL) {
throw new Error('[GlobalSetup] 致命错误: baseURL 或 storageState 未定义!');
}
const browser = await chromium.launch();
const context = await browser.newContext({
locale: 'zh-CN', // 强制中文
timezoneId: 'Asia/Shanghai', // 强制时区
});
const page = await context.newPage();
if (mode === 'url') {
try {
const url1=env.DEVSTAR_URL;
await page.goto(url1, { timeout: 15000 });
console.log('[GlobalSetup] 检测到安装界面!正在开始自动化安装...');
await page.getByText('服务器和第三方服务设置').click();
await page.getByRole('checkbox', { name: '启用通过 微信二维码 登录' }).uncheck();
await page.getByRole('checkbox', { name: '要求在用户注册时输入预验证码' }).uncheck();
await page.getByText('管理员帐号设置').click();
await page.getByRole('textbox', { name: '管理员用户名' }).fill('testuser');
await page.getByRole('textbox', { name: '邮箱地址' }).fill('ilovcatlyn750314@gmail.com');
await page.getByRole('textbox', { name: '管理员密码', exact: true }).fill('12345678');
await page.getByRole('textbox', { name: '确认密码' }).fill('12345678');
await page.getByRole('button', { name: '立即安装'}).click({ timeout: 10000, noWaitAfter: true });
console.log("安装中,请耐心等待");
await page.waitForTimeout(90000);
} catch (error) {
console.error('[GlobalSetup] "URL 模式" 登录失败:', error);
await page.screenshot({ path: 'playwright-report/global-setup-login-failure.png' });
throw error;
}
}
else {
throw new Error(`[GlobalSetup] 未知的 E2E_MODE: "${mode}"`);
}
await browser.close();
}
export default globalSetup;