cy.visit()和cy.request()的不同Cypress baseUrl [英] Different Cypress baseUrl for cy.visit() and cy.request()
问题描述
我们在本地测试的应用程序的前端和后端分别在 localhost:4200 和 127.0.0.1:8000 上运行.
Our application under test locally has a frontend and backend that run on localhost:4200 and 127.0.0.1:8000, respectively.
当调用 cy.visit('/somepage')
时,我们希望它具有与 cy.request('/someapi')
,因为 cy.visit()
将访问前端托管的页面,而 cy.request()
将向API端点上的API终端发出请求后端.
When calling cy.visit('/somepage')
we would like this to have a different baseUrl
than for cy.request('/someapi')
as cy.visit()
will visit a page hosted on the frontend, while cy.request()
will make a request to an API endpoint on the backend.
对于 cy.visit()
,我们可以使用来自 cypress.json
的默认 baseUrl
配置,但是有一种方法可以使 cy.request()
默认使用与现成使用的默认 baseUrl
不同的配置设置吗?尽量避免在所有地方都指定此名称,例如 cy.request(<完全限定域名> +'/someapi')
.谢谢!
We can use the default baseUrl
config from cypress.json
for cy.visit()
, but is there a way to have cy.request()
default to a different config setting than the default baseUrl
it uses out of the box? Trying to avoid having to specify this all over the place like cy.request(<fully qualified domain name> + '/someapi')
. Thanks!
推荐答案
我认为您可以使用配置文件cypress.env.json来存储您的API网址,并从每个测试用例中获取它.
I think you can use config file cypress.env.json to store your API url and get it from each test case.
在您的cypress.env.json中
In your cypress.env.json
"apiUrl": "http://api"
在您的测试用例中
describe('get the api variable from config file', () => {
//set up the variables
const apiUrl = Cypress.env('apiUrl');
cy.request(apiUrl + '/someapi');
这篇关于cy.visit()和cy.request()的不同Cypress baseUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!