【问题标题】:how I can run cypress script with the variable provided by user如何使用用户提供的变量运行 cypress 脚本
【发布时间】:2021-11-30 21:01:38
【问题描述】:

我有在不同环境中运行的 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

【问题讨论】:

  • 你必须使用逗号来分隔环境变量,而不是空格:npx cypress run --env name=qa,wl=seedling.

标签: cypress


【解决方案1】:

您可以通过Cypress.env('var-name') 在 CLI 中引用通过 --env 标志传入的变量。 Cypress documentation here

const whitelabelBaseUrl = whitelabelUrl.replace('://', `://${Cypress.env('wl')}.`)

另外,使用逗号而不是空格传递多个环境变量。请参阅文档here

npx cypress run --env name=qa,wl=seedling

【讨论】:

  • 感谢您的解决方案,它有效
  • @RitaBasu-ghosh 你介意选择这个答案作为正确答案吗?它有助于了解未来的问题。
猜你喜欢
  • 1970-01-01
  • 2010-09-13
  • 2017-08-23
  • 2022-01-23
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多