在赛普拉斯中,在测试之前在localStorage中设置一个令牌 [英] In Cypress, set a token in localStorage before test

查看:98
本文介绍了在赛普拉斯中,在测试之前在localStorage中设置一个令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要登录并在客户端上设置 localStorage 令牌(特别是 jwt

I want to login and set a localStorage token on the client (specifically jwt)

如何使用 cy.request ,如赛普拉斯文档中所建议?

How can I accomplish this using cy.request, as suggested in the Cypress Documentation?

推荐答案

下面是添加您可以在任何赛普拉斯测试中使用的命令 cy.login(),或放入 beforeEach 钩子。

Here's an example of adding a command cy.login() that you can use in any Cypress test, or put in a beforeEach hook.

Cypress.Commands.add('login', () => { 
  cy.request({
    method: 'POST',
    url: 'http://localhost:3000/api/users/login',
    body: {
      user: {
        email: 'jake@jake.jake',
        password: 'jakejake',
      }
    }
  })
  .then((resp) => {
    window.localStorage.setItem('jwt', resp.body.user.token)
  })

})

然后在您的测试:

beforeEach(() => {
  cy.login()
})

这篇关于在赛普拉斯中,在测试之前在localStorage中设置一个令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆