我如何使用用户提供的变量 运行 cypress 脚本
how I can run cypress script with the variable provided by user
我有在不同环境中运行的 cypress 脚本,规范文件如下所示
describe('Whitelabel Login', () => {
it('Whitelabel Login', () => {
const whitelabelBaseUrl = whitelabelUrl =>
whitelabelUrl.replace('://', '://motivaction.')
cy.visit(whitelabelBaseUrl(Cypress.config('whitelabelUrl')))
cy.screenshot('Whitelbel Login page', { capture: 'fullPage' })
cy.visit(`${whitelabelBaseUrl(Cypress.config('whitelabelUrl'))}/logout`)
cy.screenshot('Whitelbel Logout page', { capture: 'fullPage' })
})
})```
So when I am running the script npx cypress run --env name=qa wl=seedling
then I want `motivaction` to be replaces by seedling is there any way that I can achieve it
您可以通过 Cypress.env('var-name')
在 CLI 中引用通过 --env
标志传入的变量。 Cypress documentation here
const whitelabelBaseUrl = whitelabelUrl.replace('://', `://${Cypress.env('wl')}.`)
此外,使用逗号传递多个环境变量,不使用 space。请参阅文档 here
npx cypress run --env name=qa,wl=seedling
我有在不同环境中运行的 cypress 脚本,规范文件如下所示
describe('Whitelabel Login', () => {
it('Whitelabel Login', () => {
const whitelabelBaseUrl = whitelabelUrl =>
whitelabelUrl.replace('://', '://motivaction.')
cy.visit(whitelabelBaseUrl(Cypress.config('whitelabelUrl')))
cy.screenshot('Whitelbel Login page', { capture: 'fullPage' })
cy.visit(`${whitelabelBaseUrl(Cypress.config('whitelabelUrl'))}/logout`)
cy.screenshot('Whitelbel Logout page', { capture: 'fullPage' })
})
})```
So when I am running the script npx cypress run --env name=qa wl=seedling
then I want `motivaction` to be replaces by seedling is there any way that I can achieve it
您可以通过 Cypress.env('var-name')
在 CLI 中引用通过 --env
标志传入的变量。 Cypress documentation here
const whitelabelBaseUrl = whitelabelUrl.replace('://', `://${Cypress.env('wl')}.`)
此外,使用逗号传递多个环境变量,不使用 space。请参阅文档 here
npx cypress run --env name=qa,wl=seedling