我正在尝试使用固定装置来保存不同测试的数据,特别是用户凭据。这是代码的示例。当进行第二次测试时,我得到了'Cannot read properties of undefined (reading 'data')'
.
有什么想法以及如何解决这个问题吗?这是错误的吗?
before(function () {
cy.fixture('credentials').then(function (data) {
this.data = data;
})
})
it('Login correct', () => {
cy.visit('/')
loginPage.signIn(this.data.admin.username,this.data.admin.password)
cy.wait(5000)
// assertion
cy.contains('Dashboard').should('be.visible')
})
这是我的credentials.json
file:
{
"admin": {
"username": "*****",
"password": "*****"
}
}
尝试使用闭包变量来分配夹具数据。
describe('Some Test', () => {
let data; //closure variable
before(() => {
cy.fixture('credentials').then((fData) => {
data = fData;
});
});
it('Login correct', () => {
cy.visit('/')
loginPage.signIn(data.admin.username, data.admin.password) //usage of closure variable to get the values from the fixtures
cy.wait(5000)
// assertion
cy.contains('Dashboard').should('be.visible')
});
});
Gleb Bahmutov 还建议使用闭包变量。 https://glebbahmutov.com/blog/import-cypress-fixtures/
我强烈建议使用闭包变量而不是这个属性。闭包变量清晰可见,不依赖于function
vs () => {}
syntax.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)