赛普拉斯 - 如何为每个测试用例编写一个代码

Cypress - how to have one code for every test case

在 Cypress 中是否有某种包,我怎么能为我正在制作的每个测试用例只有一个代码?

例如:我有一个相同的点击浏览我的网站的过程,但我需要大约 20 个测试用例。该网站仍在升级中,因此如果网站发生变化,我不想对每个测试用例的代码进行更改,而只是在一个地方进行更改。

感谢您的帮助。

这是我在 commands.js 文件中添加的常见 login 示例。在我的测试要求中,对于每个测试套件,我都需要调用一个 login() 函数。在这里,我在 before({}) 标记内调用 login() ,它在块中的所有测试之前运行一次。我想,你可以用类似的方式添加 click through my website,但需要更详细地了解你的程序流程。

/integration/examples

测试套件和测试

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()
});