在 Cypress 中是否有某种包,我怎么能为我正在制作的每个测试用例只有一个代码?
例如:我有一个相同的点击浏览我的网站的过程,但我需要大约 20 个测试用例。该网站仍在升级中,因此如果网站发生变化,我不想对每个测试用例的代码进行更改,而只是在一个地方进行更改。
感谢您的帮助。
最佳答案
这是我在 commands.js 文件中添加的常见 login
示例。在我的测试要求中,对于每个测试套件,我都需要调用一个 login()
函数。在这里,我在 before({})
标记内调用 login() ,它在 block 中的所有测试之前运行一次。我想,您可以通过类似的方式添加 click through my website
,但需要更详细地了解您的程序流程。
/集成/示例
测试套件和测试
describe('Some name of the test suite', function(){
before('Before the test clear previous Cookies', () => {
cy.wait(2000)
cy.clearCookies()
cy.login(Cypress.env('username'), Cypress.env('password'))
});
/* Test case 1*/
it('Name of first test case', function(){
// test steps to add
})
/* Test case 2*/
it('Name of second test case', function () {
// test steps to add
})
})
/support/commands.js 文件
Cypress.Commands.add('login', (username, password) => {
cy.visit('/')
cy.get("#Loginuser").type(username)
cy.get("#Loginpass").type(password, {log:false})
cy.get("button[type='submit']").contains("Login").click()
});
关于javascript - Cypress - 如何为每个测试用例编写一个代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53582046/