yas*_*ana 6 javascript chai cypress
此代码块用于读取 Excel 文件并按给定用户角色获取用户数据。
但如果excel文件中不存在该用户角色,则会返回一个未定义的值。
我们如何检查变量user是否未定义或为空?
cy.task('getExcelData', Cypress.env('usersFilePath')).then((users) => {
const user = users.find(user => {
return user.userRole === 'userRole';
});
cy.wrap(user).should('not.be.empty');
cy.wrap(user).should('not.be.a',undefined)
cy.wrap(user).should('not.be.a',null)
signIn(user.username, user.password);
});
Run Code Online (Sandbox Code Playgroud)
cy.wrap(user).should('not.be.empty')(这部分有效,但其他部分无效)
这是我收到的错误:
Tes*_*ick 10
请参阅 chaijs .a(type[, msg])
\n\n\n断言 target\xe2\x80\x99s 类型等于给定的string type。类型不区分大小写。
\nRun Code Online (Sandbox Code Playgroud)\nexpect(undefined).to.be.an(\'undefined\')\n
or正在执行.a()返回字符串的检查,因此您只需引用该类型.an()typeof"undefined"
cy.wrap(user).should(\'not.be.a\', "undefined")\nRun Code Online (Sandbox Code Playgroud)\n或删除.a进行参考检查
cy.wrap(user).should(\'not.be\', undefined)\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
如果要检查搜索 users 数组的结果,请在函数外部进行.find()。
如果未找到具有所需角色的用户,则该.find()函数返回undefined(绝不为 null 或空字符串或空数组)。
cy.task('getExcelData', Cypress.env('usersFilePath')).then((users) => {
/*
e.g users = [
{ userRole: 'admin', username: '...', password: '...' },
{ userRole: 'userRole', username: '...', password: '...' },
]
*/
const user = users.find(user => user.userRole === 'userRole')
if (!user) {
throw new Error('user is undefined')
}
signIn(user.username, user.password);
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2498 次 |
| 最近记录: |