【发布时间】:2020-08-17 13:18:06
【问题描述】:
我有两个请求:一个是 POST 请求,另一个是 get。首先,我通过帖子获取用户访问令牌,而在其他情况下,我使用此 accessToken 登录。我的代码不起作用。
我正在使用窗口 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
});
});
});
【问题讨论】:
-
尝试在第二个测试中添加
console.log(this.value)以查看标头是否正在使用该值。我认为你不需要this.前面的value,但你可以这样确认。
标签: javascript networking get request cypress