赛普拉斯:我们如何在赛普拉斯中使用不记名令牌编写 GET 请求? [英] Cypress : How can we write GET request in with bearer token in cypress?

查看:42
本文介绍了赛普拉斯:我们如何在赛普拉斯中使用不记名令牌编写 GET 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个请求:一个是POST请求,另一个是get.首先,我通过邮寄方式获取用户访问令牌,而在其他情况下,我使用此 accessToken 获取登录信息.我的代码不起作用.

I have two requests: one is POST request and other is get. In first i get user access token by post and in other i used this accessToken to get login. My code does not work.

我使用的是 window 7 和 cypress 3.3.5

I am using window 7 and cypress 3.3.5

我的代码:

var value;
describe("Login operation", () => {
  it("Login Request with post method", () => {
    cy.request({
      method:'POST', 
      url:'https://odms.baitussalam.org:8445/api/v1/auth/login',
      body: {
        "userName": "faizanj",
        "password": "abc"
      }
    })
      .then(function(response){
        this.value = response.body.accessToken;
        console.log("Value "+this.value);

        expect(response.body.name).to.equal('Faizan');
        expect(response.status).to.equal(200);
      });
  });

  it('Second test case', function() {

    var authHeader='bearer ${'+this.value+'}';
    const options = {
      method: 'GET',
      url: `https://odms.baitussalam.org:8445/api/v1/qurbani-representative`,
      headers:{
        authorization:authHeader,
      }};

    cy.request(options)
      .then((response)=>{
        expect(response.status).to.equal(200);6+9
      });
  });
});

推荐答案

问题是您尝试在测试用例之间使用变量集,当它已经重置为存储令牌时,您需要使用全局变量(不建议)或创建在您需要访问令牌之前将调用一些登录命令.例如:

The problem is that you are trying using variable set between test cases when it's already reset to store token you need either use global variable (not advised), or create some login command that will be called before you need access to token. For example:

Cypress.Commands.Add('login', (userName, password) => {
  cy.request({
      method:'POST', 
      url:'https://odms.baitussalam.org:8445/api/v1/auth/login',
      body: {
        userName,
        password,
      }
    })
    .as('loginResponse')
    .then((response) => {
      Cypress.env('token', response.body.accessToken); // either this or some global var but remember that this will only work in one test case
      return response;
    })
    .its('status')
    .should('eq', 200);
})

然后在使用 Cypress.env('token') 之前需要登录用户时.

Then whenever you need you login user before using Cypress.env('token').

例如:

describe('testing token', () => {
  beforeEach(() => {
    cy.login();
  });

  it('test request', () => {
    const token = Cypress.env('token');
    const authorization = `bearer ${ token }`;
    const options = {
      method: 'GET',
      url: `https://odms.baitussalam.org:8445/api/v1/qurbani-representative`,
      headers: {
        authorization,
      }};

    cy.request(options)
      .its('status')
      .should('eq', 200);
  })
});

您可以更进一步并覆盖所有请求以向它们添加令牌,如下所示:

You can go further and override all request to add token to them like this:

Cypress.Commands.overwrite('request', (originalFn, ...options) => {
  const optionsObject = options[0];
  const token = Cypress.env('token');

  if (!!token && optionsObject === Object(optionsObject)) {
    optionsObject.headers = {
      authorization: 'Bearer ' + token,
      ...optionsObject.headers,
    };

    return originalFn(optionsObject);
  }

  return originalFn(...options);
});

那么上面的例子应该是这样的:

then the above example would look like this:

describe('testing token', () => {
  beforeEach(() => {
    cy.login();
  });

  it('test request', () => {
    cy.request(options)
      .its('status')
      .should('eq', 200);
  })
});

这篇关于赛普拉斯:我们如何在赛普拉斯中使用不记名令牌编写 GET 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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