小编Sha*_*med的帖子

Cypress:我们如何在 cypress 中使用不记名令牌编写 GET 请求?

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

我使用的是window 7和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
      });
  });
});
Run Code Online (Sandbox Code Playgroud)

javascript networking get request cypress

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

标签 统计

cypress ×1

get ×1

javascript ×1

networking ×1

request ×1