带有动态主体的 Cypress 网络请求

发布于 2025-01-10 21:27:43 字数 2212 浏览 0 评论 0原文

我想创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2025-01-17 21:27:43

@Fody 这与你的答案没有太大不同。

var body = {abc, abc, abc}

在 cy.request 里面

  url: "xxx",
method: 'PUT',

headers: {
  authorization: cookies
},

body

并且它起作用了。

@Fody this is not much different than your answer.

var body = {abc, abc, abc}

and inside cy.request

  url: "xxx",
method: 'PUT',

headers: {
  authorization: cookies
},

body

And it worked.

我的影子我的梦 2025-01-17 21:27:43

您可能希望将 bodyValue 设置为对象(在传入之前)

const bodyValue = { abc, abc, abc }

,然后在请求中,按照第一次尝试,

body: bodyValue

这很难说,因为即使工作“粘贴”示例也存在语法错误 - 每个属性之间应该有逗号。

You probably want to set bodyValue as an object (before passing in)

const bodyValue = { abc, abc, abc }

then in the request, as per first attempt

body: bodyValue

It's hard to tell because even the working "pasted in" example has syntax errors - there should be commas between each property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文