相关疑难解决方法(0)

我如何等待 cookie 设置?

我正在为我的应用程序的登录功能编写验收测试。在某些时候,我想仔细检查 cookie 的过期时间。

单击“登录”按钮后,一个 graphql 查询将发送到我的服务器,该服务器以 Jwt 进行响应。收到 jwt 后,应用程序将设置 cookie

document.cookie = ...
Run Code Online (Sandbox Code Playgroud)

在我的 Cypress 测试中,我通过以下方式检查令牌:

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())
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

使用cy.get('@graphql'),我正在等待 graphql 查询返回响应。别名定义如下:

cy.stub(win, 'fetch', fetch).as('graphql')
Run Code Online (Sandbox Code Playgroud)

收到后,应用程序会设置 cookie。

我的问题是我不喜欢以下调用:

cy.wait(1000)
Run Code Online (Sandbox Code Playgroud)

如果没有那个调用,我总是会得到一个未定义的 cookie。

有没有办法在远小于 1000 毫秒的时间内获取该 cookie?我尝试了很多事情但没有成功......

javascript cookies cucumber graphql cypress

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

cookies ×1

cucumber ×1

cypress ×1

graphql ×1

javascript ×1