script-browser.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { browser } from "k6/browser";
  2. export const options = {
  3. scenarios: {
  4. ui: {
  5. executor: "shared-iterations",
  6. vus: 1, // 使用5个虚拟用户
  7. iterations: 1,
  8. options: {
  9. browser: {
  10. type: "chromium",
  11. launchOptions: {
  12. args: ["--no-sandbox", "--disable-setuid-sandbox"],
  13. },
  14. },
  15. },
  16. },
  17. },
  18. thresholds: {
  19. checks: ["rate==1.0"],
  20. },
  21. };
  22. export default async function () {
  23. const targetUrl = __ENV.targetUrl || "http://www.baidu.com";
  24. const timeStamp = __ENV.timeStamp || "1751936544346";
  25. const page = await browser.newPage();
  26. // const targetUrlWithoutProtocol = targetUrl.replace('http://', '');
  27. // targetUrlWithoutProtocol = targetUrlWithoutProtocol.replace('https://', '');
  28. try {
  29. await page.goto(targetUrl, {
  30. timeout: 60000,
  31. waitUntil: "networkidle", // 等待网络稳定再继续(适合截图)
  32. });
  33. await page.screenshot({ path: `screenshots/screenshot_${timeStamp}.png` });
  34. } finally {
  35. await page.close();
  36. }
  37. }