我想为所选应用程序的测试定义全局变量。我想将它们输入到一个文件中 - 在阅读了文档后我决定使用 cypress.env.json
.
https://docs.cypress.io/guides/guides/environment-variables.html#Option-1-cypress-json
正如我看到的变量被正确导入,但在测试过程中,我得到了错误:
"TypeError: Cannot read property 'env' of undefined"
有关如何解决该问题的任何建议?
https://i.imgur.com/uEVg24I.png
测试文件:
describe('/register', () => {
beforeEach(() => {
cy.visit('/#/register')
})
it.only('requires username', () => {
cy.get('input[type="email"]').Cypress.env('correctEmail')
cy.get('input[type="password"]').Cypress.env('correctPassword')
cy.get('button').contains('Sign in').click()
cy.get('.error-messages').should('contain', 'username can\'t be blankis too short (minimum is 1 character)')
})
cypress.env.json:
{
"correctName": "Bob Ross",
"incorrectName": "Bobbbbb",
"correctEmail": "bob@ross.eu",
"incorrectEmail": "b@bbb",
"correctPassword": "bobrosss",
"incorrectPassword": "Oooooo"
}
最佳答案
要回答您最初的问题,cypress.env.json
文件需要放在项目的根目录中,紧邻 cypress.json
文件。
通过创建一个单独的 cypress.env.json 文件,您可以访问嵌套值,如下所示:
//cypress.env.js
{
"user": {
"username": "Jane",
"password": "SuperPassword",
"someOtherData": "veryImportantData"
}
}
接着//somewhere in myCypressTest.spec.ts
Cypress.env("user").username //returns "Jane"
Cypress.env("user").password //returns "SuperPassword"
Cypress.env("user").someOtherData //returns "veryImportantData"
不幸的是,它并没有真正准确地记录在任何地方。我不需要更改任何路径,只需移动 cypress.env.json
在我最初放置它的 Cypress 文件夹之外,它开始工作。
关于javascript - 在 cypress.env.json 中定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56832494/