如何从 cypress 中的先前测试用例中提取

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

我目前正在研究 cypress,当我阅读教程时,我的脑海中突然浮现出一件事。例如,如果我编写一个小测试用例来打开一个网站,单击登录并输入凭据,该怎么办?然后我想启动一个新的测试用例,以便在登录后在该网站上执行其他操作,但在不同的测试用例上。我可以从第一个中提取信息吗?

我的意思是这样的:

describe('My first test', function () {
it('login as user', function () {
   
    cy.visit('https://uat2.myregus.com/home')

    cy
        .get('#header_LogIn').click()
        .get('#Username').click()
        .type('[email protected]')
        .type('{enter}')
        .get('.password').click()
        .type('Test_123!')
        .type('{enter}')
})

})

然后我的第二个案例将从上面提取信息,以便我可以继续。

describe('My second test', function () {
it('book a meeting', function () {
   
    cy.visit('https://someWebsite.com')
  
})

})

我想这样做的原因是因为我不想每次都运行这两个测试。本质上,它每次都会使用这些凭据让我登录。我正在考虑制作一个单独的 .js 文件来提取,但我不知道如何做。请帮忙。

cypress
1个回答
1
投票

将多个测试耦合在一起是一种反模式。您的测试应该始终能够彼此独立运行并且仍然能够通过。

请参阅赛普拉斯最佳实践文档此处

第一个测试中的代码应包装在

beforeEach
挂钩中,以便可以在每个
it()
:

之前运行
describe('My second test', function () {
    beforeEach(() => {
        cy
            .get('#header_LogIn').click()
            .get('#Username').click()
            .type('[email protected]')
            .type('{enter}')
            .get('.password').click()
            .type('Test_123!')
            .type('{enter}')
    })

    it('book a meeting', function () {
   
        cy.visit('https://uat2.myregus.com/home')
  
    })

    it('does something else', function () {
        cy.visit('...')
    })
})

您还应该查看这些登录食谱以执行后端身份验证,而不是在每次测试之前通过 UI 登录。

© www.soinside.com 2019 - 2024. All rights reserved.