如何从Cypress的beforeEach中删除所有cy.route?

问题描述 投票:0回答:1

我正在为我公司的应用程序创建很多测试。在每个测试之前,我需要创建一个状态来工作,它总是一样的,所以我在我自己的方法中创建了一些路由,然后在supportindex.js文件中,我创建了beforeEach,它看起来是这样的

beforeEach(() => {
    cy.server();
    cy.mockSearches(SHORTEN_SEARCHES); // this only creates mocks
    cy.loginAdmin();
});

而且在99%的测试中都能正常工作,但是有一个测试,需要在真实数据上工作。我应该怎么做?有没有办法忽略全局的beforeEach?我想我可以把这部分代码移到每个测试之前,但这是代码重复?或者我应该用空响应覆盖这个cy.route?

javascript testing cypress e2e-testing
1个回答
0
投票

你可以在你的应用程序中添加一个条件 beforeEach() 以在设置前退出。

beforeEach(() => {
    if (shouldUseRealData) return;
    cy.server();
    cy.mockSearches(SHORTEN_SEARCHES); // this only creates mocks
    cy.loginAdmin();
});

正如文档中所说的 环境变量你可以用不同的方式设置环境变量。一种方法是在调用cypress run时在命令行中设置它。

cypress run --env use_mock=true

然后你可以用 Cypress.env('use_mock').

beforeEach(() => {
    if (Cypress.env('use_mock')) {
        cy.server();
        cy.mockSearches(SHORTEN_SEARCHES); // this only creates mocks
        cy.loginAdmin();
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.