我有两个测试:首先:登录->转到页面A->单击复选框A ->单击按钮A - >执行操作。第二步:登录->转到页面A->点击复选框A ->点击按钮B ->执行操作。由于登录,转到页面A并点击复选框A是相同的步骤。有没有一种方法,我可以写这两个测试在同一个命令,而不是写两个不同的命令。谢谢你
6za6bjd01#
You can use Cypress' beforeEach() functionality在每个测试运行之前执行一组命令。
beforeEach()
describe('My tests', () => { beforeEach(() => { // login // go to page A // click checkbox A }); it('clicks button A', () => { // click button A // other actions }); it('clicks button B', () => { // clicks button B // other actions }); });
当在共享的describe或context块中运行相同的测试时,这是最有用的。如果您的测试将在单独的describe/context块或单独的spec文件中,您可能需要考虑创建一个Cypress自定义命令,以便在不同的文件中重用。
describe
context
Cypress.Commands.add('myFunction', () => { // login // go to Page A // click checkbox A }); describe('My First Tests', () => { it('clicks button A', () => { cy.myFunction(); // click button A // other actions }); }); describe('My Second Tests', () => { it('clicks button B', () => { cy.myFunction() // clicks button B // other actions }); });
lf3rwulv2#
该命令可以根据需要使用参数来更改其测试序列。通过参数,您可以组合命令以适应测试的不同部分。
Cypress.Commands.add('customTestSequence'), (buttonSelector, actions) => { cy.visit('pageA') // fixed step cy.get('checkboxA').check() // fixed step // variable steps cy.get(buttonSelector) cy.then(() => { // put actions call inside .then for correct sequence actions() }) }) cy.customTestSequence('buttonB', () => { // actions in this callback can be js functions or further Cypress commands })
2条答案
按热度按时间6za6bjd01#
You can use Cypress'
beforeEach()
functionality在每个测试运行之前执行一组命令。当在共享的
describe
或context
块中运行相同的测试时,这是最有用的。如果您的测试将在单独的
describe
/context
块或单独的spec文件中,您可能需要考虑创建一个Cypress自定义命令,以便在不同的文件中重用。lf3rwulv2#
该命令可以根据需要使用参数来更改其测试序列。
通过参数,您可以组合命令以适应测试的不同部分。