You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Cypress中循环执行测试直至工单ID达到24000?

Cypress负载测试:循环创建工单直到达到指定ID

你当前用Cypress._.times()批量生成测试用例的写法,次数一多就会因为重复初始化测试上下文、重复登录导致速度骤降,而且硬断言目标ID也不够灵活。下面给你两种优化方案,既能高效创建工单,又能精准停在目标ID。

方案1:一次性登录+递归循环创建

核心思路是只登录一次,用递归逻辑判断当前工单ID是否达到目标,没达到就继续创建,避免重复操作的开销。

/// <reference types="cypress" />

const TARGET_TICKET_ID = 24000; // 最终要达到的工单ID

describe('批量创建工单负载测试', () => {
  before(() => {
    // 仅执行一次登录流程,减少重复开销
    cy.visit('link');
    cy.get('#email').type('name');
    cy.get('#password').type('password');
    cy.get('._btn_1wzi7_1').click();
    // 进入工单创建页面(根据实际页面逻辑调整)
    cy.get('.css-19bb58m').type('Microsoft Outlook 2').type('{enter}');
  });

  it('循环创建工单直到达到目标ID', () => {
    const createTicket = () => {
      const timestamp = new Date().getTime();
      // 填充工单内容
      cy.get('#title').type(`Random Text ${timestamp}`);
      cy.get('#note').type(`Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} 
      Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp}
      Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp}`);
      cy.get('#checkbox').click();
      cy.get('._btn_1wzi7_1').click();

      // 获取当前创建的工单ID,判断是否继续循环
      cy.get('._title_1jitq_37').invoke('text').then(currentId => {
        const numId = parseInt(currentId, 10);
        if (numId < TARGET_TICKET_ID) {
          // 回到工单创建页面(根据实际页面操作调整,比如点击返回或重新导航)
          cy.get('.css-19bb58m').type('Microsoft Outlook 2').type('{enter}');
          createTicket(); // 递归调用继续创建
        }
      });
    };

    createTicket(); // 启动循环
  });
});

方案2:按指定数量循环创建

如果更关注需要创建的工单总数(14000个),可以直接按计数控制循环,不用依赖ID判断:

/// <reference types="cypress" />

const TOTAL_TICKETS = 14000; // 需要创建的工单总数

describe('批量创建工单负载测试', () => {
  let createdCount = 0;

  before(() => {
    // 仅登录一次
    cy.visit('link');
    cy.get('#email').type('name');
    cy.get('#password').type('password');
    cy.get('._btn_1wzi7_1').click();
    cy.get('.css-19bb58m').type('Microsoft Outlook 2').type('{enter}');
  });

  it('循环创建指定数量的工单', () => {
    const createTicket = () => {
      if (createdCount >= TOTAL_TICKETS) return;

      const timestamp = new Date().getTime();
      // 填充工单内容
      cy.get('#title').type(`Random Text ${timestamp}`);
      cy.get('#note').type(`Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} 
      Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp}
      Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp} Another Random Text ${timestamp}`);
      cy.get('#checkbox').click();
      cy.get('._btn_1wzi7_1').click();

      createdCount++;
      // 回到创建页面
      cy.get('.css-19bb58m').type('Microsoft Outlook 2').type('{enter}');
      createTicket();
    };

    createTicket();
  });
});

额外注意事项

  • 尽量替换动态类名(比如._btn_1wzi7_1)为更稳定的选择器,比如元素ID或自定义属性,避免前端构建后类名变化导致测试失败
  • 如果创建工单后页面跳转逻辑复杂,可以用cy.go('back')或直接导航到创建页面的URL,提升效率
  • 负载测试时可以调整Cypress配置,比如增大defaultCommandTimeout避免超时,或者启用experimentalSessionAndOrigin优化会话复用

内容的提问来源于stack exchange,提问作者Tural Suleymanzade

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 00:09:23