【问题标题】:Skip a global function on Cypress跳过 Cypress 上的全局函数
【发布时间】:2021-11-16 06:11:47
【问题描述】:

我的 cypress 测试场景中有一些全局变量。

    describe('AutoLogin Test Case',function(){
        beforeEach(function(){
            Cypress.Cookies.preserveOnce('_session_id')
        })
        afterEach(function(){
            cy.get('[id="ajax_working"]',{timeout:6000}).should('not.be.visible')
        })
    
    
        it('input login info',function(){ 
            cy.visit('https://***********.******.com/')
            cy.get('[id^=user_username]')
            .type('ChrisPbacon').should('have.value','ChrisPbacon')
            cy.get('[id^=user_password]')
            .type('welcome123').should('have.value','welcome123')
            cy.contains('Sign In Now').click()
        })
})

测试用例完成后,系统将检查“after each”函数并查找“ajax_working”...我只需要在显示的“it”测试中跳过该检查,但我仍然需要在程序的其余部分运行它。我不想在每次测试中都写后果函数,因为它很麻烦而且总体上不干净。有人有什么建议吗?

【问题讨论】:

    标签: javascript automation automated-tests cypress cypress-component-test-runner


    【解决方案1】:

    不确定这是否适合您的问题,但您可以实现此目的的一种方法是将 afterEach 钩子与测试包装在相同的上下文中,而不是使用 describe 声明,如下所示:

    beforeEach(function () {
        cy.log("Before each hook");
    });
    
    describe('AutoLogin Test Case',function() {
    
        afterEach(function () {
            cy.log("After each hook");
        });
    
        it('Some test', function () {
            cy.log("Your first test case")
        });
    
    });
    
    describe('AutoLogin Test Case 2',function() {
        it('Some other test', function() {
            cy.log("Your second test case");
        });
    });
    

    注意具有您预期行为的输出,beforeEach 钩子在两个测试中调用,而 afterEach 钩子仅在第一次测试时调用:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多