英文:
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:
cypress.config-admin.ts
, where I have the baseUrl for the admin page (as inwww.mypage.admin.com
). I pass the --config-file tag in thepackage.json
file for headless tests.cypress.config-user.ts
where I have the baseUrl for the user page (www.mypage.com
). I pass the --config-file tag in thepackage.json
file for headless tests.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:
cypress.config-admin.ts
, where I have the baseUrl for the admin page (as inwww.mypage.admin.com
). I pass the --config-file tag in thepackage.json
file for headless tests.cypress.config-user.ts
where I have the baseUrl for the user page (www.mypage.com
). I pass the --config-file tag in thepackage.json
file for headless tests.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'))
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论