Files
devstar/tests/e2e/global-setup.ts
2025-11-14 09:53:34 +08:00

69 lines
3.7 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 page = await browser.newPage();
if (mode === 'url') {
try {
const url1=env.DEVSTAR_URL;
await page.goto(url1, { timeout: 15000 });
console.log('[GlobalSetup] 检测到安装界面!正在开始自动化安装...');
// (你提供的所有 "testuser" 安装步骤...)
await page.getByText('Server and Third-Party Service Settings').click();
await page.getByRole('checkbox', { name: 'Enable user sign-in via Wechat QR Code.' }).uncheck();
await page.getByRole('checkbox', { name: 'Require a CAPTCHA for user' }).uncheck();
await page.getByText('Administrator Account Settings').click();
await page.getByRole('textbox', { name: 'Administrator Username' }).fill('testuser');
await page.getByRole('textbox', { name: 'Email Address' }).fill('ilovcatlyn750314@gmail.com');
await page.getByRole('textbox', { name: 'Password', exact: true }).fill('12345678');
await page.getByRole('textbox', { name: 'Confirm Password' }).fill('12345678');
await page.getByRole('button', { name: 'Install Gitea'}).click();
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 if (mode === 'compose') {
console.log('[GlobalSetup] 模式: 构建(Compose). 正在执行构建模式的安装脚本...');
try {
const hostGateway = env.DOCKER_HOST_GATEWAY || '172.17.0.1';
const giteaBaseURL = `http://${hostGateway}:80`;
const serverDomain = hostGateway;
const browser = await chromium.launch();
await page.goto(baseURL, { timeout: 15000 });
console.log('[GlobalSetup] 检测到安装界面!正在开始自动化安装...');
await page.getByRole('textbox', { name: 'Server Domain *' }).fill(serverDomain);
await page.getByRole('textbox', { name: 'Gitea Base URL *' }).fill(giteaBaseURL);
// (你提供的所有 "testuser" 安装步骤...)
await page.getByText('Server and Third-Party Service Settings').click();
await page.getByRole('checkbox', { name: 'Enable user sign-in via Wechat QR Code.' }).uncheck();
await page.getByRole('checkbox', { name: 'Require a CAPTCHA for user' }).uncheck();
await page.getByText('Administrator Account Settings').click();
await page.getByRole('textbox', { name: 'Administrator Username' }).fill('testuser');
await page.getByRole('textbox', { name: 'Email Address' }).fill('ilovcatlyn750314@gmail.com');
await page.getByRole('textbox', { name: 'Password', exact: true }).fill('12345678');
await page.getByRole('textbox', { name: 'Confirm Password' }).fill('12345678');
await page.getByRole('button', { name: 'Install Gitea'}).click();
await page.waitForTimeout(90000);
} catch (error) {
console.error('[GlobalSetup] "构建模式" 安装失败:', error);
await page.screenshot({ path: 'playwright-report/global-setup-failure.png' });
throw error;
}
} else {
throw new Error(`[GlobalSetup] 未知的 E2E_MODE: "${mode}"`);
}
}
export default globalSetup;