cy.getCookie('sid').should('exist') failed. #32431
Replies: 1 comment
-
|
The issue is that Additionally,
Here's the fix — visit the domain first (even a lightweight page), then make the request: describe("HTML form submission with cy.request", () => {
it("check cookie exist", () => {
// Visit the domain first so Cypress has a page context for cookies
cy.visit("https://web.qa-xx.com")
cy.request({
url: "https://web.qa-xx.com/auth-service/signin",
method: "POST",
form: true,
body: {
username,
password,
},
})
cy.getCookie("sid").should("exist")
cy.getCookie("uid").should("exist")
})
})Alternatively, you can check cookies directly from the response headers instead of relying on cy.request({
url: "https://web.qa-xx.com/auth-service/signin",
method: "POST",
form: true,
body: { username, password },
}).then((res) => {
// Check Set-Cookie header directly
expect(res.headers["set-cookie"]).to.exist
const cookies = res.headers["set-cookie"]
const hasSid = cookies.some((c) => c.startsWith("sid="))
const hasUid = cookies.some((c) => c.startsWith("uid="))
expect(hasSid).to.be.true
expect(hasUid).to.be.true
})Also note: the reason it passes on the second run is likely because the cookies from the first test's |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My login process is similar with this example
The first test passes but the second test case always fails at first run.
/auth-service/signinAPI will return theSet-Cookieresponse header and 302 HTTP status code.E2E Test result:

cypress.config.ts:Package version:
"cypress": "^15.1.0"Beta Was this translation helpful? Give feedback.
All reactions