run-mcp-tests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const CONFIG = {
  2. webUrl: 'http://192.168.16.18:8082',
  3. admin: { phone: '13800138000', password: 'admin123' }
  4. };
  5. async function navigate(url) {
  6. return await skill_mcp({ mcp_name: 'playwright', tool_name: 'browser_navigate', arguments: { url } });
  7. }
  8. async function snapshot() {
  9. return await skill_mcp({ mcp_name: 'playwright', tool_name: 'browser_snapshot', arguments: {} });
  10. }
  11. async function click(ref) {
  12. return await skill_mcp({ mcp_name: 'playwright', tool_name: 'browser_click', arguments: { target: ref } });
  13. }
  14. async function type(ref, text) {
  15. return await skill_mcp({ mcp_name: 'playwright', tool_name: 'browser_type', arguments: { target: ref, text } });
  16. }
  17. async function wait(seconds) {
  18. return await skill_mcp({ mcp_name: 'playwright', tool_name: 'browser_wait_for', arguments: { time: seconds } });
  19. }
  20. async function screenshot(filename) {
  21. return await skill_mcp({ mcp_name: 'playwright', tool_name: 'browser_take_screenshot', arguments: { type: 'png', filename } });
  22. }
  23. async function loginAdmin() {
  24. console.log('[登录] 开始管理员登录...');
  25. await navigate(CONFIG.webUrl);
  26. await wait(2);
  27. const snap = await snapshot();
  28. console.log('[登录] 页面快照:', snap.substring(0, 500));
  29. await type('e11', CONFIG.admin.phone);
  30. await type('e17', CONFIG.admin.password);
  31. await wait(1);
  32. await click('e22');
  33. await wait(3);
  34. const afterSnap = await snapshot();
  35. console.log('[登录] 登录后页面:', afterSnap.substring(0, 300));
  36. if (afterSnap.includes('dashboard') || afterSnap.includes('首页')) {
  37. console.log('[登录] 登录成功 ✅');
  38. return true;
  39. }
  40. console.log('[登录] 登录后未发现 dashboard');
  41. return false;
  42. }
  43. async function testWEB001() {
  44. console.log('\n=== TEST: WEB-001 管理员登录 ===');
  45. const passed = await loginAdmin();
  46. console.log(`WEB-001: ${passed ? 'PASSED ✅' : 'FAILED ❌'}`);
  47. return passed;
  48. }
  49. async function testWEB002() {
  50. console.log('\n=== TEST: WEB-002 管理员查看用户列表 ===');
  51. const loginOk = await loginAdmin();
  52. if (!loginOk) return false;
  53. await click('e41');
  54. await wait(1);
  55. await click('e153');
  56. await wait(2);
  57. const snap = await snapshot();
  58. const passed = snap.includes('用户管理') || snap.includes('用户列表');
  59. console.log(`WEB-002: ${passed ? 'PASSED ✅' : 'FAILED ❌'}`);
  60. return passed;
  61. }
  62. async function runTests() {
  63. console.log('===== 知心益家 MCP 测试 =====');
  64. const results = [];
  65. results.push({ id: 'WEB-001', result: await testWEB001() });
  66. results.push({ id: 'WEB-002', result: await testWEB002() });
  67. console.log('\n===== 结果汇总 =====');
  68. let passed = 0, failed = 0;
  69. for (const r of results) {
  70. console.log(`${r.id}: ${r.result ? 'PASSED' : 'FAILED'}`);
  71. if (r.result) passed++; else failed++;
  72. }
  73. console.log(`总计: ${passed} 通过, ${failed} 失败`);
  74. return results;
  75. }
  76. module.exports = { runTests, testWEB001, testWEB002 };
  77. if (require.main === module) {
  78. runTests();
  79. }