debug-page.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { chromium } from '@playwright/test';
  2. async function debugPage() {
  3. const browser = await chromium.launch({ headless: true });
  4. const page = await browser.newPage();
  5. // 捕获页面错误
  6. page.on('console', msg => {
  7. console.log(`页面控制台: ${msg.type()} ${msg.text()}`);
  8. });
  9. page.on('pageerror', error => {
  10. console.log(`页面错误: ${error.message}`);
  11. });
  12. try {
  13. console.log('导航到登录页面...');
  14. await page.goto('http://localhost:8080/admin/login');
  15. console.log('填写登录信息...');
  16. await page.getByPlaceholder('请输入用户名').fill('admin');
  17. await page.getByPlaceholder('请输入密码').fill('admin123');
  18. console.log('点击登录按钮...');
  19. await page.getByRole('button', { name: '登录' }).click();
  20. console.log('等待登录完成...');
  21. await page.waitForLoadState('networkidle');
  22. // 等待更长时间确保页面完全加载
  23. await page.waitForTimeout(5000);
  24. // 检查页面是否完全加载
  25. const loadingIndicator = await page.locator('text=Loading...').count();
  26. console.log('Loading指示器数量:', loadingIndicator);
  27. if (loadingIndicator > 0) {
  28. console.log('页面仍在加载,等待更多时间...');
  29. await page.waitForTimeout(3000);
  30. }
  31. console.log('当前URL:', page.url());
  32. console.log('页面标题:', await page.title());
  33. // 尝试直接访问用户管理页面
  34. console.log('尝试直接访问用户管理页面...');
  35. await page.goto('http://localhost:8080/admin/users');
  36. await page.waitForLoadState('networkidle');
  37. await page.waitForTimeout(5000);
  38. console.log('用户管理页面URL:', page.url());
  39. console.log('用户管理页面标题:', await page.title());
  40. // 检查用户管理页面元素
  41. const headings = await page.getByRole('heading').all();
  42. console.log('页面标题数量:', headings.length);
  43. for (let i = 0; i < headings.length; i++) {
  44. const text = await headings[i].textContent();
  45. console.log(`标题 ${i}:`, text);
  46. }
  47. // 检查按钮
  48. const buttons = await page.getByRole('button').all();
  49. console.log('按钮数量:', buttons.length);
  50. for (let i = 0; i < Math.min(buttons.length, 10); i++) {
  51. const text = await buttons[i].textContent();
  52. console.log(`按钮 ${i}:`, text);
  53. }
  54. // 检查页面内容
  55. const bodyText = await page.textContent('body');
  56. console.log('页面内容前500字符:', bodyText.substring(0, 500));
  57. } catch (error) {
  58. console.error('调试出错:', error);
  59. } finally {
  60. await browser.close();
  61. }
  62. }
  63. debugPage();