- baseURL 改为 127.0.0.1 解决 CORS 脚本加载问题 - 统一使用 domcontentloaded 替代 load(Playground 特性) - admin 页面测试串行化避免并发压力 - 过滤 Playground CORS 控制台错误 - Markdown Feed 改用 request API 避免下载触发 - 认证测试兼容 Playground 自动登录 全部 25 测试通过。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
88 lines
3.1 KiB
JavaScript
88 lines
3.1 KiB
JavaScript
// WPMind 设置页面 + 模块管理测试
|
||
// @ts-check
|
||
const { test, expect } = require('@playwright/test');
|
||
|
||
const ADMIN_URL = '/wp-admin/admin.php?page=wpmind';
|
||
const GOTO_OPTS = { waitUntil: 'domcontentloaded', timeout: 60000 };
|
||
|
||
async function login(page) {
|
||
await page.goto('/wp-login.php', GOTO_OPTS);
|
||
await page.fill('#user_login', 'admin');
|
||
await page.fill('#user_pass', 'password');
|
||
await page.click('#wp-submit');
|
||
await page.waitForLoadState('domcontentloaded');
|
||
await page.waitForTimeout(1000);
|
||
}
|
||
|
||
// 串行执行,共享登录上下文,避免 Playground 并发压力
|
||
test.describe.serial('设置页面基础', () => {
|
||
test.beforeEach(async ({ page }) => {
|
||
await login(page);
|
||
});
|
||
|
||
test('设置页面可访问', async ({ page }) => {
|
||
// 第一次访问 admin 页面可能很慢(PHP 冷启动)
|
||
await page.goto(ADMIN_URL, { waitUntil: 'domcontentloaded', timeout: 90000 });
|
||
await page.waitForTimeout(2000);
|
||
await expect(page.locator('h1, .wrap h1, .wrap h2').first()).toBeVisible();
|
||
await expect(page).not.toHaveTitle(/错误|Error/i);
|
||
});
|
||
|
||
test('所有标签页可切换', async ({ page }) => {
|
||
await page.goto(ADMIN_URL, GOTO_OPTS);
|
||
await page.waitForTimeout(2000);
|
||
|
||
const tabs = ['overview', 'services', 'images', 'routing', 'modules'];
|
||
for (const tab of tabs) {
|
||
const tabEl = page.locator(`[href*="#${tab}"], [data-tab="${tab}"], .nav-tab[href*="${tab}"]`).first();
|
||
if (await tabEl.isVisible()) {
|
||
await tabEl.click();
|
||
await page.waitForTimeout(500);
|
||
expect(await page.locator('body').innerText()).not.toBe('');
|
||
}
|
||
}
|
||
});
|
||
|
||
test('概览标签页显示插件信息', async ({ page }) => {
|
||
await page.goto(ADMIN_URL, GOTO_OPTS);
|
||
await page.waitForTimeout(2000);
|
||
const content = await page.content();
|
||
expect(content).toMatch(/WPMind|wpmind|0\.11/i);
|
||
});
|
||
});
|
||
|
||
test.describe.serial('模块管理', () => {
|
||
test.beforeEach(async ({ page }) => {
|
||
await login(page);
|
||
});
|
||
|
||
test('模块列表可见', async ({ page }) => {
|
||
await page.goto(ADMIN_URL, GOTO_OPTS);
|
||
await page.waitForTimeout(2000);
|
||
const modulesTab = page.locator('[href*="#modules"], [data-tab="modules"]').first();
|
||
if (await modulesTab.isVisible()) {
|
||
await modulesTab.click();
|
||
await page.waitForTimeout(1000);
|
||
const content = await page.content();
|
||
expect(content).toMatch(/api.gateway|analytics|cost.control|exact.cache|geo|media.intelligence|auto.meta/i);
|
||
}
|
||
});
|
||
|
||
test('模块切换 AJAX 端点可用', async ({ page }) => {
|
||
await page.goto(ADMIN_URL, GOTO_OPTS);
|
||
await page.waitForTimeout(2000);
|
||
const response = await page.evaluate(async () => {
|
||
try {
|
||
const res = await fetch('/wp-admin/admin-ajax.php', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||
body: 'action=wpmind_toggle_module&module_id=test&enable=0',
|
||
});
|
||
return { status: res.status, ok: res.ok };
|
||
} catch (e) {
|
||
return { error: e.message };
|
||
}
|
||
});
|
||
expect(response.status).toBeDefined();
|
||
});
|
||
});
|