如何永久更改 cypress.json 中的环境变量?
How can I change permanently env variable in cypress.json?
我有各种各样的 spec.js 文件要 运行。
为了提高测试性能和速度,我创建了第一个测试文件,它登录到我的 Web 服务并将令牌存储到我在 cypress.json
中声明的名为 TOKEN 的环境变量中
{
"env": {
"TOKEN": ""
},
"viewportWidth": 1920,
"viewportHeight": 1080,
"responseTimeout": 90000,
"defaultCommandTimeout": 60000,
"requestTimeout": 60000
}
为此,我将以下代码写入第一个测试文件:
describe('Setup', () => {
it('Should setup token', () => {
cy.request({
method: 'POST',
url: 'path/to/endpoint',
headers: {
'api_version': Cypress.env('API_VERSION')
},
body: {
userName: Cypress.env('USERNAME'),
password: Cypress.env('PASSWORD')
}
}).then((response) => {
Cypress.config('TOKEN', response.body.token)
});
});
});
POST 调用有效,它 return 并将令牌保存在 cypress.json 中,但它仅适用于第一个测试文件,当我继续其他测试文件时(根据文档)存储的令牌值被删除,因为 Cypress.config('VAR_NAME', var_value)
仅适用于使用该语句的测试文件。
我尝试将令牌值存储到 cookie 中并永久保存 cookie,但令牌太长,无法存储到 cookie 中。
有没有办法将令牌永久保存到 cypress.json env 中?
我认为克服“仅当前规范”问题的最简单方法是 运行 您在 before()
中的 /cypress/support/index.js
中的请求
// support/index.js
before(() => {
cy.request({
method: 'POST',
url: 'path/to/endpoint',
...
}).then((response) => {
Cypress.config('TOKEN', response.body.token)
});
})
当我 运行 几个简单的验证测试时
// support/index.js
Cypress.config('test', 'test1')
// tests 1 & 2
it('sees globally set config', () => {
expect(Cypress.config('test')).to.eq('test1')
})
它通过了
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ √ config1.spec.js 89ms 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ √ config2.spec.js 136ms 1 1 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
√ All specs passed! 225ms 2 2 - - -
我有各种各样的 spec.js 文件要 运行。 为了提高测试性能和速度,我创建了第一个测试文件,它登录到我的 Web 服务并将令牌存储到我在 cypress.json
中声明的名为 TOKEN 的环境变量中{
"env": {
"TOKEN": ""
},
"viewportWidth": 1920,
"viewportHeight": 1080,
"responseTimeout": 90000,
"defaultCommandTimeout": 60000,
"requestTimeout": 60000
}
为此,我将以下代码写入第一个测试文件:
describe('Setup', () => {
it('Should setup token', () => {
cy.request({
method: 'POST',
url: 'path/to/endpoint',
headers: {
'api_version': Cypress.env('API_VERSION')
},
body: {
userName: Cypress.env('USERNAME'),
password: Cypress.env('PASSWORD')
}
}).then((response) => {
Cypress.config('TOKEN', response.body.token)
});
});
});
POST 调用有效,它 return 并将令牌保存在 cypress.json 中,但它仅适用于第一个测试文件,当我继续其他测试文件时(根据文档)存储的令牌值被删除,因为 Cypress.config('VAR_NAME', var_value)
仅适用于使用该语句的测试文件。
我尝试将令牌值存储到 cookie 中并永久保存 cookie,但令牌太长,无法存储到 cookie 中。
有没有办法将令牌永久保存到 cypress.json env 中?
我认为克服“仅当前规范”问题的最简单方法是 运行 您在 before()
中的 /cypress/support/index.js
// support/index.js
before(() => {
cy.request({
method: 'POST',
url: 'path/to/endpoint',
...
}).then((response) => {
Cypress.config('TOKEN', response.body.token)
});
})
当我 运行 几个简单的验证测试时
// support/index.js
Cypress.config('test', 'test1')
// tests 1 & 2
it('sees globally set config', () => {
expect(Cypress.config('test')).to.eq('test1')
})
它通过了
Spec Tests Passing Failing Pending Skipped
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ √ config1.spec.js 89ms 1 1 - - - │
├────────────────────────────────────────────────────────────────────────────────────────────────┤
│ √ config2.spec.js 136ms 1 1 - - - │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
√ All specs passed! 225ms 2 2 - - -