实际项目中,有时会碰到多条测试用例执行步骤和检查步骤完全一致,只有输入和输出不同的情况,如果一条一条编写测试用例的效率就会比较低下,因此我们可以根据数据动态的生成测试用例(即常说的参数化)。
以上一章登录测试为例,除了正确登录外,我们还可以验证失败的情况,比如密码错误、账号为空、密码为空等不同场景。
(一)在cypress\integration目录下新建一个文件夹LOGIN,在LOGIN内新建一个suplus_login.data.js的文件存放测试数据。
image.png
代码如下,具体需要测试的场景根据实际情况而定:
export const suplusLoginUser = [
{
summary: "Login pass",
username: "13700**",
password: "123456"
},
{
//密码错误
summary: "Login fail",
username: "137000***",
password: "1234567"
},
{
//账号错误
summary: "Login fail",
username: "13700",
password: "123456"
},
{
//账号为空
summary: "Login fail",
username: "",
password: "1234567"
},
{
//密码为空
summary: "Login fail",
username: "137000***",
password: ""
}
]
(二)在suplus.login.js文件中导入数据文件,代码如下:
import {suplusLoginUser} from "./suplus_login.data";
describe("企业平台登录",function () {
context('素+平台登录测试',function () {
for(const user of suplusLoginUser){
it(user.summary, function () {
cy.visit("/#/login")
//选择密码登录
cy.get('#tab-pwd').click()
//:nth-child(n)选择器,匹配其父元素的第n个子元素,不论类型,右键copy-copy selector
cy.get('#pane-pwd > form > div:nth-child(1) > div > div > input')
.type(user.username)
cy.get('#pane-pwd > form > div:nth-child(2) > div > div > input')
.type(user.password)
cy.get('#pane-pwd > form > div:nth-child(4) > div > button > span')
.click()
// 选择公司
cy.get('[style="cursor: pointer;"]')
.click()
cy.url().should("contain",'/workbench')
// cy.getCookie('jsessionid').should('exist')
cy.get('ul').should("contain",'工作台')
});
}
})
})
按照预期,只有第一个用例是通过的,其余都失败
image.png
注:context()是describe()的别名,其行为方式和describe()相同,使用context()只是提供一种使测试更易阅读和组织的方法。
describe():可以在里设定context(),可以包括多个测试用例it(),也可以嵌套测试套件。