带有动态主体的 Cypress 网络请求
我想创建 cypress 自定义方法,该方法将使用参数作为动态请求正文(以避免代码重复,因为 url、方法和标头始终相同),如下所示:
var bodyValue =
`abc
abc
abc`
var bodyValue2 =
`bbb
bbb
bbb`
Cypress.Commands.add("myRequest", () => {
cy.request({
url: "xxx",
method: 'PUT',
headers: {
authorization: cookies
},
body: bodyValue
})
})
它总是以 Whats lucky 结尾
The response we got was:
Status: 409 - Conflict
Headers: {
xxx
xxx
xxx
}
Body: {
"message": "Setting Deleted"
}
如果我从中获取值, 我的变量并将其粘贴到正文中:
cy.request({
url: "xxx",
method: 'PUT',
headers: {
authorization: cookies
},
body: {
abc
abc
abc
}
})
它总是有效
我也尝试过
cy.request({
url: "xxx",
method: 'PUT',
headers: {
authorization: cookies
},
body: {bodyValue}
})
这很奇怪,因为之后的正文不是,
abc
abc
abc
但这
{"bodyValue":
abc
abc
abc
}
可能是原因。我不知道如何实现我的目标。
我的实际身体值:
{
"revision": "23554252352542343",
"activePerspective": ".Beta",
"perspectives": [
{
"type": ".Beta",
"activeLayout": "1x1",
"layouts": [
{
"caption": "1x1",
"canonicalName": "1x1",
"icon": {
"source": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"18\" height=\"18\"><path fill=\"none\" stroke=\"#fff\" class=\"eveInteractiveSvgStroke\"/></svg>"
},
"placement": {
"center": {
"config_mode": ".Default",
"visualization": "object-table",
"configuration": "punkt"
}
}
}
]
}
]
}
I would like to create cypress custom method which will be using parameter as a dynamic request body (to avoid code duplicate because url, method and headers are always the same) like that:
var bodyValue =
`abc
abc
abc`
var bodyValue2 =
`bbb
bbb
bbb`
Cypress.Commands.add("myRequest", () => {
cy.request({
url: "xxx",
method: 'PUT',
headers: {
authorization: cookies
},
body: bodyValue
})
})
it always ends with
The response we got was:
Status: 409 - Conflict
Headers: {
xxx
xxx
xxx
}
Body: {
"message": "Setting Deleted"
}
Whats funny if I take value from my variable and just paste it into body:
cy.request({
url: "xxx",
method: 'PUT',
headers: {
authorization: cookies
},
body: {
abc
abc
abc
}
})
It is always working
I also have tried
cy.request({
url: "xxx",
method: 'PUT',
headers: {
authorization: cookies
},
body: {bodyValue}
})
It is weird because because body after that is not
abc
abc
abc
but
{"bodyValue":
abc
abc
abc
}
which may be the cause. I don't know how to achieve my goal.
My acutal bodyValue:
{
"revision": "23554252352542343",
"activePerspective": ".Beta",
"perspectives": [
{
"type": ".Beta",
"activeLayout": "1x1",
"layouts": [
{
"caption": "1x1",
"canonicalName": "1x1",
"icon": {
"source": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"18\" height=\"18\"><path fill=\"none\" stroke=\"#fff\" class=\"eveInteractiveSvgStroke\"/></svg>"
},
"placement": {
"center": {
"config_mode": ".Default",
"visualization": "object-table",
"configuration": "punkt"
}
}
}
]
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Fody 这与你的答案没有太大不同。
在 cy.request 里面
并且它起作用了。
@Fody this is not much different than your answer.
and inside cy.request
And it worked.
您可能希望将 bodyValue 设置为对象(在传入之前)
,然后在请求中,按照第一次尝试,
这很难说,因为即使工作“粘贴”示例也存在语法错误 - 每个属性之间应该有逗号。
You probably want to set bodyValue as an object (before passing in)
then in the request, as per first attempt
It's hard to tell because even the working "pasted in" example has syntax errors - there should be commas between each property.