【问题标题】:How to update Cookie into headers in cy.request in cypress如何将 Cookie 更新为 cy.request in cypress 中的标头
【发布时间】:2023-01-11 11:13:10
【问题描述】:

当我发出 cy.request 时,cookie 不会更新为标头。我试图在调用 cy.setCookie 之前清除 cookie 但仍然得到不同的 cookie 值

 putCallAdmin(endpoint, requestBody) {
    let cookie;
    cy.getCookie("Cookie")
      .should("have.property", "value", cookieToken)
      .then((c) => {=
        cookie = c;
      });

    cy.request({
      method: "PUT",
      url: endpoint,
      headers: {
        Cookie: cookie,
        "Content-Type": "application/json",
      },
      body: requestBody,
      failOnStatusCode: false,
    }).then((data) => {
      this.printLogs(data.body);
    });
}

【问题讨论】:

    标签: javascript cypress


    【解决方案1】:

    在您的测试生命周期中,cookie 设置的时间可能太晚了。您传递给 cy.getCookie 的 .then 回调是异步调用的,因此 cy.request 命令可能在设置 cookie 之前执行。

    您可以尝试的一件事是在发出请求之前使用 cy.wait 等待设置 cookie。例如:

    cy.getCookie("Cookie").then((c) => {
        cookie = c.value;
        cy.wait(1000).then(() => {
            cy.request({
                method: "PUT",
                url: endpoint,
                headers: {
                    Cookie: cookie,
                    "Content-Type": "application/json"
                },
                body: requestBody,
                failOnStatusCode: false
            }).then((data) => {
                this.printLogs(data.body);
            });
        });
    });
    

    或者,您可以使用 async 和 await 使其更具可读性

    async function putCallAdmin(endpoint, requestBody) {
        const cookie = await cy.getCookie("Cookie").then((c) => c.value);
        const data = await cy.request({
          method: "PUT",
          url: endpoint,
          headers: {
            Cookie: cookie,
            "Content-Type": "application/json",
          },
          body: requestBody,
          failOnStatusCode: false,
        });
        this.printLogs(data.body);
     }
    

    您还可以在执行 cy.setCookie('Cookie', cookieToken) 之前使用 cy.clearCookie('Cookie') 以确保将正确的 cookie 传递到标头。

    它将确保在发出请求之前清除以前的 cookie 并设置新的 cookie。

    【讨论】:

      【解决方案2】:

      您提供的代码的问题是 cy.getCookie("Cookie") 命令是异步的并返回一个承诺。所以 let cookie 变量在承诺被解决之前不会被设置。当你的cy.request被调用时,cookie的初始值仍然是undefined

      要解决此问题,您需要将 cy.request 调用移动到 .then 回调函数中,以便它仅在解决 cy.getCookie 承诺并设置 cookie 变量后执行。

      putCallAdmin(endpoint, requestBody) {
        cy.getCookie("Cookie")
          .should("have.property", "value", cookieToken)
          .then((c) => {
            cy.request({
              method: "PUT",
              url: endpoint,
              headers: {
                Cookie: c.value,
                "Content-Type": "application/json",
              },
              body: requestBody,
              failOnStatusCode: false,
            }).then((data) => {
              this.printLogs(data.body);
            });
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-17
        • 1970-01-01
        • 2020-01-24
        • 1970-01-01
        相关资源
        最近更新 更多