我正尝试在某些测试中使用从Cypress命令接收的承载人身份验证令牌,但我得到了一个错误的Cypress error
下面是我的命令代码:
Cypress.Commands.add("getToken", () => {
cy.request({
method: 'POST',
url: 'https://login.microsoftonline.com/abcc1234-abc12345-abcc1234-abc12345/oauth2/v2.0/token',
form: true,
body: {
'client_id': 'abcc1234-abc12345-abcc1234-abc12345abcc1234-abc12345/'
'client_secret' : 'J2Kq.XXXXXXXX.Xt_-XXXXXX',
'grant_type': 'client_credentials'
}
})
.then((response) =>{
const tokenAuth = response.body.access_token;
window.localStorage.setItem('auth', tokenAuth);
下面是我的测试:
describe('API Testing', ()=>{
beforeEach(() => {
cy.getToken()
})
it('Connector - GET', () =>{
let a = "Bearer " + localStorage.getItem('auth')
cy.log(a);
cy.request({
method:'GET',
url:'https://XXXX-XXXXX-XXXXXXXXX.azurewebsites.net/api/v1/connectors/cost-max-and-power-intervals',
auth : 'a'
})
.then((response) =>{
expect(response).to.have.property('status', 200)
expect(response.body).to.not.be.null
expect(response.body).to.have.property('costMax', 35)
})
})
我也尝试过在request中设置一个头,但没有成功。:(
提前谢谢你!
发布于 2021-04-22 18:35:17
看起来我搞错了,auth和Autorization是不一样的。我设法通过用Autorization替换auth来使它工作:a
https://stackoverflow.com/questions/67209524
复制相似问题