【问题标题】:Reusing test methods in other .JS files in Cypress.io在 Cypress.io 的其他 .JS 文件中重用测试方法
【发布时间】:2020-10-25 15:33:50
【问题描述】:

下面是我为测试我的应用程序而创建的 login.js 和 client creation.js 文件。

login.js 在测试运行器中执行并正常工作,但我想使用

it("Successful Login", function () {

    cy.visit('/login')
    cy.get('#username').type('admin')
    cy.get('#password').type('Qwerty@123')
    cy.get('.btn').click()

})

每次在clientCreation.js中执行测试用例的方法。

执行它的最佳方式是什么?没有把它放到 clientCreation.js 文件的 foreach 中

登录.js

    describe("Login to the system", function () {

    it("Successful Login", function () {

        cy.visit('/login')
        cy.get('#username').type('admin')
        cy.get('#password').type('Qwerty@123')
        cy.get('.btn').click()

    })

    it("Unsuccessful login", function () {

        cy.visit('/login')
        cy.get('#username').type('invalid')
        cy.get('#password').type('Invalid')
        cy.get('.btn').click()

    })
})

clientCreation.js

```
beforeEach(() => {
   
})

describe('Client Creation Test Suite', function () {


    it('Check the user can create a client', function () {

      //test code

    })


    it('Check the client creation validation', function () {

       //test code
    })


})

我也附上项目结构供大家参考。

【问题讨论】:

  • 您想让登录过程可重用吗?就像在 clientCreation.js 中使用它一样?
  • @HasipTimurtas 是的。

标签: javascript node.js npm cypress


【解决方案1】:

您可以使用 Cypress Custom Commands 编写可重用的命令。

转到cypress/support/commands.js 并编写可重复使用的命令,例如:

Cypress.Commands.add('loginSuccess', (username, password) => {
   cy.get('#username').type(username)
   cy.get('#password').type(password)
   cy.get('.btn').click()
})

Cypress.Commands.add('loginFail', (username, password) => {
   cy.get('#username').type(username)
   cy.get('#password').type(password)
   cy.get('.btn').click()
})

然后你可以在任何测试中使用它,例如clientCreation.js -

describe('Client Creation Test Suite', function() {
   beforeEach(() => {
      cy.visit('/login')
   })
   it('Check the user can create a client', function() {
      //test code
      cy.loginSuccess('admin', 'Qwerty@123')
   })
   it('Check the client creation validation', function() {
      //test code
      cy.loginFail('invalid', 'invalid')
   })
})

【讨论】:

  • 看来你的解决方案更好。
【解决方案2】:

我这样做,它很有用。创建文件登录。它将是一个带有参数的函数:cy、用户名和密码。

module.exports = function (cy, username, pass){
  cy.visit('/login')
  cy.get('#username').type(username)
  cy.get('#password').type(pass)
  cy.get('.btn').click()
}

并在你想要运行的测试文件中调用它,该文件位于 clientCreation.js 中


const login = require('./login');  // path to login.js

beforeEach(() => {
   
})

describe('Client Creation Test Suite', function () {

    it('Check the user can create a client', function () {
      login(cy, "admin", "12345");

      //test code

    })


    it('Check the client creation validation', function () {

       //test code
    })
})

【讨论】:

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