Browse Source

feat: add timeout

周玉环 2 tuần trước cách đây
mục cha
commit
4ebae738ac
1 tập tin đã thay đổi với 16 bổ sung13 xóa
  1. 16 13
      server/script-browser.js

+ 16 - 13
server/script-browser.js

@@ -1,36 +1,39 @@
-import { browser } from 'k6/browser';
+import { browser } from "k6/browser";
 
 export const options = {
   scenarios: {
     ui: {
-      executor: 'shared-iterations',
-      vus: 1,              // 使用5个虚拟用户
+      executor: "shared-iterations",
+      vus: 1, // 使用5个虚拟用户
       iterations: 1,
       options: {
         browser: {
-          type: 'chromium',
+          type: "chromium",
           launchOptions: {
-            args: ['--no-sandbox', '--disable-setuid-sandbox']
-          }
-        }
+            args: ["--no-sandbox", "--disable-setuid-sandbox"],
+          },
+        },
       },
     },
   },
   thresholds: {
-    checks: ['rate==1.0'],
-  }
+    checks: ["rate==1.0"],
+  },
 };
 
 export default async function () {
-  const targetUrl = __ENV.targetUrl || 'http://www.baidu.com';
-  const timeStamp = __ENV.timeStamp || '1751936544346';
+  const targetUrl = __ENV.targetUrl || "http://www.baidu.com";
+  const timeStamp = __ENV.timeStamp || "1751936544346";
   const page = await browser.newPage();
   // const targetUrlWithoutProtocol = targetUrl.replace('http://', '');
   // targetUrlWithoutProtocol = targetUrlWithoutProtocol.replace('https://', '');
   try {
-    await page.goto(targetUrl);
+    await page.goto(targetUrl, {
+      timeout: 60000,
+      waitUntil: "networkidle", // 等待网络稳定再继续(适合截图)
+    });
     await page.screenshot({ path: `screenshots/screenshot_${timeStamp}.png` });
   } finally {
     await page.close();
   }
-}
+}