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

Cypress仪表板测试优化:如何单次登录执行多测试用例

解决Cypress仪表板测试重复登录问题

问题描述

我正在编写自动化测试脚本,涉及5个页面:01.登录页(Sign in)、02.登录页(Login)、03.仪表板(Dashboard)、04.账户页(Account Page)、05.设置页(Settings Page)。每个页面都编写了独立测试用例,登录页测试已完成。当前执行仪表板的5个测试用例时,使用beforeEach方法调用登录脚本,通过5个it块对应各测试用例,但出现每次测试重复登录的问题,希望实现单次登录后运行全部5个仪表板测试用例。

现有代码

require('@cypress/xpath')
import '../../support/commands'

import {searchPage} from '../../PageObjects/Serach&FilterPage.js';


const SandF = new searchPage()

describe('Login', () => {
    beforeEach(() => {
        cy.login('username', 'password');
        cy.wait(1000); 
      });

      it('Search functionality with name', () => {
        cy.xpath("//p[contains(.,'Candidates')]").should('have.text','Candidates')
        cy.wait(2000);
      })

      it('Filter functionality with By Completion Status ', () => {
        SandF.Status().click({ force: true })
        cy.wait(5000); 
        cy.contains('Pending').focused().click({ force: true })
        cy.wait(1500); 
      })

      it('Filter functionality with By Completion Status =', () => {
        SandF.Status().click({ force: true })
        cy.wait(2000); 
        cy.contains('Passed & Completed').scrollIntoView().click({ force: true })
      })

      it('Filter functionality with By Completion Status', () => { 
        SandF.Status().click({ force: true })
        cy.wait(1000); 
        cy.contains('Failed & Completed').scrollIntoView().click({ force: true })
        SandF.Reset().click({ force: true })
    })
})

解决方案

核心是把beforeEach替换为before钩子——before只会在整个describe块执行前运行一次,而beforeEach会在每个it测试用例前重复执行。

修改后的代码如下:

require('@cypress/xpath')
import '../../support/commands'

import {searchPage} from '../../PageObjects/Serach&FilterPage.js';

const SandF = new searchPage()

// 建议把describe名称改为更贴合的"Dashboard Tests"
describe('Dashboard Tests', () => {
    // 替换beforeEach为before,仅执行一次登录
    before(() => {
        cy.login('username', 'password');
        cy.wait(1000); 
    });

    it('Search functionality with name', () => {
        cy.xpath("//p[contains(.,'Candidates')]").should('have.text','Candidates')
        cy.wait(2000);
    })

    it('Filter functionality with By Completion Status ', () => {
        SandF.Status().click({ force: true })
        cy.wait(5000); 
        cy.contains('Pending').click({ force: true })
        cy.wait(1500); 
    })

    it('Filter functionality with By Completion Status =', () => {
        SandF.Status().click({ force: true })
        cy.wait(2000); 
        cy.contains('Passed & Completed').scrollIntoView().click({ force: true })
    })

    it('Filter functionality with By Completion Status', () => { 
        SandF.Status().click({ force: true })
        cy.wait(1000); 
        cy.contains('Failed & Completed').scrollIntoView().click({ force: true })
        SandF.Reset().click({ force: true })
    })
})

额外提示

  • 测试状态隔离:如果某个测试会修改页面状态(比如最后一个测试点击了Reset),要确保后续测试的初始状态符合预期,必要时在测试前添加状态重置逻辑。
  • 优化等待逻辑:尽量用Cypress的断言自动等待(比如cy.get().should())替代固定时长的cy.wait(),提升测试稳定性和执行效率。

内容的提问来源于stack exchange,提问作者yash chandrasekara

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 15:23:25