test_web_browser.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { spawn } from 'child_process';
  2. const edgePath = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe';
  3. console.log('Launching Edge headless with CDP...');
  4. const edge = spawn(edgePath, [
  5. '--headless',
  6. '--remote-debugging-port=9222',
  7. '--disable-gpu',
  8. 'http://192.168.2.79:8080/'
  9. ]);
  10. edge.on('error', (err) => console.error('Edge spawn error:', err));
  11. await new Promise(r => setTimeout(r, 2500));
  12. try {
  13. const listRes = await fetch('http://127.0.0.1:9222/json');
  14. const targets = await listRes.json();
  15. console.log('CDP targets found:', targets.length);
  16. const page = targets.find(t => t.type === 'page' && t.url.includes('192.168.2.79:8080'));
  17. if (!page) {
  18. console.error('Target page not found in CDP targets:', targets);
  19. edge.kill();
  20. process.exit(1);
  21. }
  22. const ws = new WebSocket(page.webSocketDebuggerUrl);
  23. ws.onopen = async () => {
  24. console.log('Connected to page CDP WebSocket');
  25. await new Promise(r => setTimeout(r, 2000));
  26. ws.send(JSON.stringify({
  27. id: 10,
  28. method: 'Runtime.evaluate',
  29. params: {
  30. expression: `(() => {
  31. const title = document.title;
  32. const ver = document.getElementById('sys-version')?.innerText;
  33. const mode = document.getElementById('sys-mode')?.innerText;
  34. const stationCards = Array.from(document.querySelectorAll('.station-card')).map(c => ({
  35. id: c.getAttribute('data-id'),
  36. name: c.querySelector('.station-name')?.innerText,
  37. status: c.querySelector('.status-tag')?.innerText
  38. }));
  39. // Click Start Test button to open modal
  40. const startBtn = document.getElementById('btn-open-start');
  41. startBtn?.click();
  42. const modalOpen = !document.getElementById('modal-start-run')?.classList.contains('hidden');
  43. // Close modal
  44. document.getElementById('btn-close-modal-start')?.click();
  45. const modalClosed = document.getElementById('modal-start-run')?.classList.contains('hidden');
  46. // Check 24 dimm slots in CPU0 and CPU1
  47. const cpu0Slots = document.querySelectorAll('#cpu0-grid .dimm-slot').length;
  48. const cpu1Slots = document.querySelectorAll('#cpu1-grid .dimm-slot').length;
  49. return {
  50. title,
  51. version: ver,
  52. mode: mode,
  53. stations: stationCards,
  54. modal_open_test: modalOpen,
  55. modal_closed_test: modalClosed,
  56. cpu0_slots: cpu0Slots,
  57. cpu1_slots: cpu1Slots
  58. };
  59. })()`,
  60. returnByValue: true
  61. }
  62. }));
  63. };
  64. ws.onmessage = (evt) => {
  65. const msg = JSON.parse(evt.data);
  66. if (msg.id === 10) {
  67. console.log('BROWSER_TEST_SUCCESS:');
  68. console.log(JSON.stringify(msg.result?.result?.value, null, 2));
  69. ws.close();
  70. edge.kill();
  71. process.exit(0);
  72. }
  73. };
  74. ws.onerror = (err) => {
  75. console.error('WebSocket error:', err);
  76. edge.kill();
  77. process.exit(1);
  78. };
  79. } catch (e) {
  80. console.error('Test execution error:', e);
  81. edge.kill();
  82. process.exit(1);
  83. }
  84. setTimeout(() => {
  85. console.error('Browser test timeout');
  86. edge.kill();
  87. process.exit(1);
  88. }, 12000);