admin管理员组

文章数量:1287858

I am writing the acceptance tests for my application's login feature. At some point, I want to double-check the cookie's expiry time.

Upon clicking on the "Login" button, a graphql query is sent to my server which responds with a Jwt. Upon reception of the jwt, the application sets the cookie with

document.cookie = ...

In my Cypress test, I check the token in the following way:

Then("sa session s'ouvre pour {SessionDurationType}", expectedDuration => {
  cy.get('@graphql').then(() => {
    cy.wait(1000)
    cy.getCookie('token').then(cookie => {
      const tokenDuration = getTokenDuration(cookie.value)
     expect(tokenDuration.asSeconds()).to.equal(expectedDuration.asSeconds())
    })
  })
})

With cy.get('@graphql'), I am waiting for the graphql query to return a response. The alias is defined like this:

cy.stub(win, 'fetch', fetch).as('graphql')

Upon reception, the application sets the cookie.

My problem is that I am not fond of the following call:

cy.wait(1000)

Without that call, I always get an undefined cookie.

Is there a way to get that cookie within some time that might be much less than 1000 ms? I tried many things without success...

I am writing the acceptance tests for my application's login feature. At some point, I want to double-check the cookie's expiry time.

Upon clicking on the "Login" button, a graphql query is sent to my server which responds with a Jwt. Upon reception of the jwt, the application sets the cookie with

document.cookie = ...

In my Cypress test, I check the token in the following way:

Then("sa session s'ouvre pour {SessionDurationType}", expectedDuration => {
  cy.get('@graphql').then(() => {
    cy.wait(1000)
    cy.getCookie('token').then(cookie => {
      const tokenDuration = getTokenDuration(cookie.value)
     expect(tokenDuration.asSeconds()).to.equal(expectedDuration.asSeconds())
    })
  })
})

With cy.get('@graphql'), I am waiting for the graphql query to return a response. The alias is defined like this:

cy.stub(win, 'fetch', fetch).as('graphql')

Upon reception, the application sets the cookie.

My problem is that I am not fond of the following call:

cy.wait(1000)

Without that call, I always get an undefined cookie.

Is there a way to get that cookie within some time that might be much less than 1000 ms? I tried many things without success...

Share Improve this question asked Feb 17, 2019 at 11:34 Laurent MichelLaurent Michel 1,3213 gold badges19 silver badges33 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You must write a recursive promise function, try the following

function checkCookie() {
  // cy.getCookie returns a thenebale
  return cy.getCookie('token').then(cookie => {
    const tokenDuration = getTokenDuration(cookie.value);
    // it checks the seconds right now, without unnecessary waitings
    if(tokenDuration.asSeconds() !== expectedDuration.asSeconds()) {
      // waits for a fixed milliseconds amount
      cy.wait(100);
      // returns the same function recursively, the next `.then()` will be the checkCookie function itself
      return checkCookie();
    }
    // only when the condition passes returns a resolving promise
    return Promise.resolve(tokenDuration.asSeconds());
  })
}

Then("sa session s'ouvre pour {SessionDurationType}", expectedDuration => {
  cy.get('@graphql').then(() => {
    checkCookie()
      .then(seconds => {
        expect(seconds).to.equal(expectedDuration.asSeconds())
      })
  })
})

Note that the function must be improved because

  • I didn't parametrize the expectedDuration etc. (it's out of the scope of showing you how to do that)
  • it waits forever without a loop counter check

But it works (I checked in another context before replying to you) and if you have some more troubles please share a "working" GitHub repo so I can clone and check it with your own solution.

Let me know if it isn't enough clear

本文标签: javascriptHow do I wait until a cookie is setStack Overflow