index.html 12.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>E2E 测试面板</title>
  <style>
    :root {
      color-scheme: light;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
      background: #f6f8fb;
      color: #1f2937;
    }

    body {
      margin: 0;
      padding: 28px;
    }

    main {
      max-width: 1180px;
      margin: 0 auto;
    }

    h1 {
      margin: 0 0 8px;
      font-size: 28px;
    }

    .sub {
      margin: 0 0 22px;
      color: #64748b;
    }

    section {
      margin-bottom: 20px;
    }

    .toolbar {
      display: flex;
      flex-wrap: wrap;
      gap: 12px;
      align-items: end;
      padding: 18px;
      border: 1px solid #dbe3ef;
      border-radius: 8px;
      background: #fff;
    }

    label {
      display: grid;
      gap: 6px;
      font-size: 13px;
      color: #475569;
    }

    select, button {
      height: 38px;
      border-radius: 6px;
      border: 1px solid #cbd5e1;
      background: #fff;
      color: #0f172a;
      font-size: 14px;
    }

    select {
      min-width: 150px;
      padding: 0 10px;
    }

    button {
      padding: 0 16px;
      border-color: #2563eb;
      background: #2563eb;
      color: #fff;
      cursor: pointer;
    }

    button.secondary {
      border-color: #cbd5e1;
      background: #fff;
      color: #1d4ed8;
    }

    button:disabled {
      cursor: not-allowed;
      opacity: .6;
    }

    .quick {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
    }

    .quick button {
      background: #fff;
      color: #1d4ed8;
    }

    .status {
      margin: 12px 0;
      min-height: 24px;
      font-weight: 600;
    }

    .workspace {
      display: grid;
      grid-template-columns: 1fr;
      gap: 16px;
      align-items: start;
    }


    .console {
      overflow: hidden;
      border: 1px solid #1e293b;
      border-radius: 8px;
      background: #0f172a;
      box-shadow: 0 12px 30px rgba(15, 23, 42, .12);
    }

    .console-header {
      display: flex;
      justify-content: space-between;
      gap: 12px;
      align-items: center;
      padding: 12px 14px;
      color: #e2e8f0;
      border-bottom: 1px solid #243449;
      font-size: 14px;
      font-weight: 700;
      cursor: pointer;
      user-select: none;
    }

    .console-toggle {
      color: #cbd5e1;
      font-size: 12px;
      font-weight: 600;
    }

    .console.is-collapsed pre {
      display: none;
    }

    .console-state {
      color: #93c5fd;
      font-size: 12px;
      font-weight: 600;
    }

    .console pre {
      box-sizing: border-box;
      width: 100%;
      height: 260px;
      margin: 0;
      padding: 16px;
      overflow: auto;
      color: #e5e7eb;
      font-family: Consolas, "Courier New", monospace;
      font-size: 13px;
      line-height: 1.55;
      white-space: pre-wrap;
      word-break: break-word;
    }

    .report {
      overflow: hidden;
      border: 1px solid #dbe3ef;
      border-radius: 8px;
      background: #fff;
    }

    .report-header {
      display: flex;
      justify-content: space-between;
      gap: 12px;
      align-items: center;
      padding: 12px 14px;
      border-bottom: 1px solid #e2e8f0;
    }

    .report-header strong {
      font-size: 15px;
    }

    iframe {
      width: 100%;
      height: 680px;
      border: 0;
      background: #fff;
    }

    .empty-report {
      display: grid;
      min-height: 680px;
      place-items: center;
      color: #64748b;
    }

    .is-hidden {
      display: none !important;
    }

    @media (max-width: 900px) {
      body {
        padding: 18px;
      }


      .console pre {
        height: 220px;
        min-height: 220px;
      }

      iframe,
      .empty-report {
        height: 520px;
        min-height: 520px;
      }
    }
  </style>
</head>
<body>
  <main>
    <h1>E2E 测试面板</h1>
    <p class="sub">选择项目、环境、测试范围和浏览器,点击按钮运行测试。</p>

    <section class="toolbar">
      <label>
        项目
        <select id="appProject"></select>
      </label>
      <label>
        环境
        <select id="env"></select>
      </label>
      <label>
        用例范围
        <select id="suite"></select>
      </label>
      <label>
        浏览器
        <select id="browser"></select>
      </label>
      <button id="run">运行测试</button>
      <button id="showReport" class="secondary">查看最新报告</button>
    </section>

    <div id="status" class="status">等待运行</div>

    <div id="workspace" class="workspace">
      <section id="consolePanel" class="console is-hidden is-collapsed" aria-live="polite">
        <div id="consoleHeader" class="console-header" role="button" tabindex="0">
          <strong>运行输出</strong>
          <span><span id="consoleState" class="console-state">已收起</span> <span id="consoleToggle" class="console-toggle">展开</span></span>
        </div>
        <pre id="output">等待测试开始...</pre>
      </section>

      <section class="report">
        <div class="report-header">
          <strong>最新报告</strong>
          <button id="refreshReport" class="secondary">刷新报告</button>
        </div>
        <div id="emptyReport" class="empty-report">还没有显示报告。</div>
        <iframe id="reportFrame" class="is-hidden" title="Playwright 测试报告"></iframe>
      </section>
    </div>
  </main>

  <script src="/panel-config.js"></script>
  <script>
    const appProject = document.querySelector('#appProject');
    const env = document.querySelector('#env');
    const suite = document.querySelector('#suite');
    const browser = document.querySelector('#browser');
    const run = document.querySelector('#run');
    const showReport = document.querySelector('#showReport');
    const refreshReport = document.querySelector('#refreshReport');
    const status = document.querySelector('#status');
    const workspace = document.querySelector('#workspace');
    const consolePanel = document.querySelector('#consolePanel');
    const consoleHeader = document.querySelector('#consoleHeader');
    const consoleState = document.querySelector('#consoleState');
    const consoleToggle = document.querySelector('#consoleToggle');
    const output = document.querySelector('#output');
    const emptyReport = document.querySelector('#emptyReport');
    const reportFrame = document.querySelector('#reportFrame');

    const selectOptions = window.E2E_PANEL_SELECT_OPTIONS;

    function renderSelectOptions() {
      Object.entries(selectOptions).forEach(([selectId, options]) => {
        const select = document.querySelector(`#${selectId}`);
        select.replaceChildren(...options.map(({ value, label }) => {
          const option = document.createElement('option');
          option.value = value;
          option.textContent = label;
          return option;
        }));
      });
    }

    renderSelectOptions();
    const buttons = Array.from(document.querySelectorAll('button'));
    let progressTimer = null;
    let runStartedAt = 0;
    let lastOutputAt = 0;

    function setButtonsDisabled(disabled) {
      buttons.forEach((button) => button.disabled = disabled);
    }

    function setConsoleCollapsed(collapsed) {
      consolePanel.classList.toggle('is-collapsed', collapsed);
      consoleToggle.textContent = collapsed ? '展开' : '收起';
    }

    function showConsole() {
      workspace.classList.add('is-running');
      consolePanel.classList.remove('is-hidden');
      setConsoleCollapsed(false);
      consoleState.textContent = '运行中';
      output.textContent = '';
    }

    function startProgressHeartbeat() {
      stopProgressHeartbeat();
      runStartedAt = Date.now();
      lastOutputAt = runStartedAt;
      progressTimer = window.setInterval(() => {
        const now = Date.now();
        const seconds = Math.floor((now - runStartedAt) / 1000);
        const idleSeconds = Math.floor((now - lastOutputAt) / 1000);
        consoleState.textContent = `运行中 ${seconds}s`;
        if (idleSeconds >= 10) {
          appendOutput(`\n[面板] 测试仍在运行,已用时 ${seconds}s,正在等待页面或接口返回...\n`, { system: true });
          lastOutputAt = now;
        }
      }, 5000);
    }

    function stopProgressHeartbeat() {
      if (progressTimer) {
        window.clearInterval(progressTimer);
        progressTimer = null;
      }
    }

    function finishConsole() {
      workspace.classList.remove('is-running');
      consolePanel.classList.remove('is-hidden');
      setConsoleCollapsed(false);
      consoleState.textContent = '已完成';
    }

    function appendOutput(text, options = {}) {
      if (!options.system) {
        lastOutputAt = Date.now();
      }
      output.textContent += text;
      output.scrollTop = output.scrollHeight;
    }

    function clearReportFrame(message = '本轮测试运行中,报告完成后会自动显示。') {
      reportFrame.classList.add('is-hidden');
      reportFrame.removeAttribute('src');
      emptyReport.classList.remove('is-hidden');
      emptyReport.textContent = message;
    }

    function showReportFrame() {
      emptyReport.classList.add('is-hidden');
      reportFrame.classList.remove('is-hidden');
      reportFrame.src = `/report/?t=${Date.now()}`;
    }

    async function refreshReportStatus() {
      const response = await fetch('/report-status');
      const result = await response.json();
      showReport.disabled = !result.exists;
      refreshReport.disabled = !result.exists;
      return result.exists;
    }

    async function handleStream(response) {
      if (!response.ok) {
        const errorResult = await response.json().catch(() => ({}));
        throw new Error(errorResult.output || '测试启动失败');
      }

      const reader = response.body.getReader();
      const decoder = new TextDecoder();
      let buffer = '';
      let finalResult = null;

      while (true) {
        const { value, done } = await reader.read();
        if (done) break;

        buffer += decoder.decode(value, { stream: true });
        const lines = buffer.split('\n');
        buffer = lines.pop() || '';

        for (const line of lines) {
          if (!line.trim()) continue;
          const message = JSON.parse(line);
          if (message.type === 'output') {
            appendOutput(message.output);
          }
          if (message.type === 'done') {
            finalResult = message;
          }
        }
      }

      return finalResult || { ok: false, reportExists: false };
    }

    async function runTest(nextAppProject, nextEnv, nextBrowser, nextSuite) {
      setButtonsDisabled(true);
      clearReportFrame();
      showConsole();
      startProgressHeartbeat();
      status.textContent = '测试运行中...';

      try {
        const response = await fetch('/run-stream', {
          method: 'POST',
          headers: { 'content-type': 'application/json' },
          body: JSON.stringify({ project: nextAppProject, env: nextEnv, browser: nextBrowser, suite: nextSuite }),
        });
        const result = await handleStream(response);
        status.textContent = result.ok ? '测试通过,已显示最新报告' : '测试失败,已显示最新报告';

        await refreshReportStatus();
        if (result.reportExists) {
          showReportFrame();
        }
      } catch (error) {
        clearReportFrame('本轮测试启动失败,没有生成新报告。');
        status.textContent = `运行失败:${error.message}`;
      } finally {
        stopProgressHeartbeat();
        setButtonsDisabled(false);
        finishConsole();
        await refreshReportStatus();
      }
    }

    consoleHeader.addEventListener('click', () => setConsoleCollapsed(!consolePanel.classList.contains('is-collapsed')));
    consoleHeader.addEventListener('keydown', (event) => {
      if (event.key === 'Enter' || event.key === ' ') {
        event.preventDefault();
        setConsoleCollapsed(!consolePanel.classList.contains('is-collapsed'));
      }
    });

    run.addEventListener('click', () => runTest(appProject.value, env.value, browser.value, suite.value));
    showReport.addEventListener('click', showReportFrame);
    refreshReport.addEventListener('click', showReportFrame);
    document.querySelectorAll('[data-env]').forEach((button) => {
      button.addEventListener('click', () => runTest(button.dataset.project, button.dataset.env, button.dataset.browser, button.dataset.suite));
    });

    refreshReportStatus();
  </script>
</body>
</html>