重用Cypress.io中其他.JS文件中的测试方法

问题描述

下面是我用来测试应用程序的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文件的前言中

Login.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
    })


})

我还将附上项目结构以供您参考。

enter image description here

解决方法

您可以使用赛普拉斯Custom Commands编写可重复使用的命令。

转到ItemTouchHelper并编写可重复使用的命令,例如:

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',password) => { cy.get('#username').type(username) cy.get('#password').type(password) cy.get('.btn').click() }) -

clientCreation.js
,

我这样做,这很有用。创建文件登录名。它将是一个具有以下参数的函数: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
    })
})

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...