赛普拉斯 - 为各种语言使用环境变量
Cypress - use environment variables for various languages
我想使用 Cypress 环境变量来强制浏览器使用不同的语言。
这是我的示例 cypress.json 的样子:
{
"env":{
"baseUrl": "localhost",
"language": {
"en": "5.44.16.0",
"se": "31.211.192.0"
},
}
}
我试过这个:
const market = Cypress.env(language.se) // se here is just an example, I want it to be dynamic
cy.server({
onAnyRequest: function (route, proxy) {
proxy.xhr.setRequestHeader(
"x-forwarded-for",
market
);
},
});
cy.visit(Cypress.env('baseUrl'))
但是没有用。
我想要实现的是能够通过从命令行强制使用语言来执行 Cypress,如下所示:
cypress run --env language=se
其中 'se' 存储来自 cypress.json 文件的 IP 值
我是赛普拉斯的新手,所以非常感谢任何帮助。
你需要从环境变量中读取 'ip' 的方式是错误的。
在 package.json
脚本中:
"test": "cypress run",
When calling a command using npm run, you need to pass the command’s arguments using the -- string.
npm
命令:npm run test-local -- --env language=se
direct cypress command
: cypress run --env language=se
在config.json
中:
{
"env":{
"baseUrl": "localhost",
"ip": {
"en": "5.44.16.0",
"se": "31.211.192.0"
}
}
}
在 Test
规范中:
const language = Cypress.env('language');
const market = Cypress.env('ip')[language] // se here is just an example, I want it to be dynamic
cy.log(`All: ${JSON.stringify(Cypress.env('ip'))}`);
cy.server({
onAnyRequest: function (route, proxy) {
cy.log(`For language: ${language} => ${market}`);
proxy.xhr.setRequestHeader(
"x-forwarded-for",
market
);
},
});
cy.visit(Cypress.env('baseUrl'))
注意:我使用 cypress open --env language=se
截图
我想使用 Cypress 环境变量来强制浏览器使用不同的语言。
这是我的示例 cypress.json 的样子:
{
"env":{
"baseUrl": "localhost",
"language": {
"en": "5.44.16.0",
"se": "31.211.192.0"
},
}
}
我试过这个:
const market = Cypress.env(language.se) // se here is just an example, I want it to be dynamic
cy.server({
onAnyRequest: function (route, proxy) {
proxy.xhr.setRequestHeader(
"x-forwarded-for",
market
);
},
});
cy.visit(Cypress.env('baseUrl'))
但是没有用。
我想要实现的是能够通过从命令行强制使用语言来执行 Cypress,如下所示:
cypress run --env language=se
其中 'se' 存储来自 cypress.json 文件的 IP 值 我是赛普拉斯的新手,所以非常感谢任何帮助。
你需要从环境变量中读取 'ip' 的方式是错误的。
在 package.json
脚本中:
"test": "cypress run",
When calling a command using npm run, you need to pass the command’s arguments using the -- string.
npm
命令:npm run test-local -- --env language=se
direct cypress command
: cypress run --env language=se
在config.json
中:
{
"env":{
"baseUrl": "localhost",
"ip": {
"en": "5.44.16.0",
"se": "31.211.192.0"
}
}
}
在 Test
规范中:
const language = Cypress.env('language');
const market = Cypress.env('ip')[language] // se here is just an example, I want it to be dynamic
cy.log(`All: ${JSON.stringify(Cypress.env('ip'))}`);
cy.server({
onAnyRequest: function (route, proxy) {
cy.log(`For language: ${language} => ${market}`);
proxy.xhr.setRequestHeader(
"x-forwarded-for",
market
);
},
});
cy.visit(Cypress.env('baseUrl'))
注意:我使用 cypress open --env language=se
截图