如何在Cypress中有多个配置文件时调用我的baseUrl?

huangapple go评论55阅读模式
英文:

How do I call my baseUrl when I have multiple configuration files in Cypress?

问题

I have a Cypress framework where I'm testing a user page (where they can interact with the page, download files, update their profile, etc.), and an admin page for that user page (separate login, and can actually, as an admin, modify what's in the user page).
For that reason, I have three configuration files:

  1. cypress.config-admin.ts, where I have the baseUrl for the admin page (as in www.mypage.admin.com). I pass the --config-file tag in the package.json file for headless tests.
  2. cypress.config-user.ts where I have the baseUrl for the user page (www.mypage.com). I pass the --config-file tag in the package.json file for headless tests.
  3. cypress-config.ts, which I use to run the tests manually via Cypress dashboard.

There are times where I need to use both URLs in the same test case (do something as an admin, verify as a user).

My issue is, when I use Cypress.config().baseUrl, it only brings up the values on cypress.config.ts. I've tried multiple things, but I haven't found a way to call the baseUrl on the admin or user config files.

https://docs.cypress.io/api/cypress-api/config seems to suggest I can pass the name of the configuration file, but I haven't been able to make that work =/

Any ideas?

英文:

I have a Cypress framework where I'm testing a user page (where they can interact with the page, download files, update their profile, etc.), and an admin page for that user page (separate login, and can actually, as an admin, modify what's in the user page).
For that reason, I have three configuration files:

  1. cypress.config-admin.ts, where I have the baseUrl for the admin page (as in www.mypage.admin.com). I pass the --config-file tag in the package.json file for headless tests.
  2. cypress.config-user.ts where I have the baseUrl for the user page (www.mypage.com). I pass the --config-file tag in the package.json file for headless tests.
  3. cypress-config.ts, which I use to run the tests manually via Cypress dashboard.

There are times where I need to use both URLs in the same test case (do something as an admin, verify as a user).

My issue is, when I use Cypress.config().baseUrl, it only brings up the values on cypress.config.ts. I've tried multiple things, but I haven't found a way to call the baseUrl on the admin or user config files.

https://docs.cypress.io/api/cypress-api/config seems to suggest I can pass the name of the configuration file, but I haven't been able to make that work =/

Any ideas?

答案1

得分: 3

你可以将各种 baseUrl 定义为环境变量条目,然后根据需要在测试中设置它们。

这样可以为你提供单一的配置,但可以访问多个版本的 baseUrl(假设这是各种配置版本中唯一更改的内容)。

cypress.config.js

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:1234',   // 默认值
  },
  env: {
    userBaseUrl: 'www.mypage.com',
    adminBaseUrl: 'www.mypage.admin.com',
  },
})

测试:

it('测试管理员页面',
  { baseUrl: Cypress.env('adminBaseUrl') },   // 每个测试的配置

  () => {
    const userBaseUrl = Cypress.env('userBaseUrl')    // 访问测试的 URL
    const adminBaseUrl = Cypress.env('adminBaseUrl')  

    // 或者实时配置
    Cypress.config('baseUrl', adminBaseUrl)
    cy.visit('/')                           // 转到 adminBaseUrl

    // 或者直接使用环境设置
    cy.visit(Cypress.env('adminBaseUrl'))

    ...
英文:

You define the various baseUrl as env entries, then set them in the test as required.

This gives you a single config but access to multiple versions of baseUrl (presuming that's all you change in the various config versions).

cypress.config.js

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    baseUrl: 'http://localhost:1234',   // default
  },
  env: {
    userBaseUrl: 'www.mypage.com',
    adminBaseUrl: 'www.mypage.admin.com',
  },
})

Test:

it('tests with the admin page', 
  {baseUrl: Cypress.env('adminBaseUrl')},   // per-test configuration

  () => {
    const userBaseUrl = Cypress.env('userBaseUrl')    // access urls for test
    const adminBaseUrl = Cypress.env('adminBaseUrl')  

    // or configure on the fly
    Cypress.config('baseUrl', adminBaseUrl)
    cy.visit('/')                           // goes to adminBaseUrl

    // or use the env setting directly
    cy.visit(Cypress.env('adminBaseUrl'))

    ...

huangapple
  • 本文由 发表于 2023年5月18日 04:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定