我将使用Cypress.io测试REST,但是使用链接请求,它希望这样工作,第一个API上的JSON响应体将在下一个API头上使用,用于授权
我已经尝试使用cypress命令并在console.log上打印,但是它似乎没有在日志中捕获,或者对此有任何线索,或者我只是使用另一个命令,比如cy.route?
Cypress.Commands.add("session", () => {
return cy.request({
method: 'POST',
url: '/auth/',
headers: {
'Content-Type': 'application/json',
},
body: {
"client_secret" : ""+config.clientSecret_staging,
"username": ""+config.email_staging,
"password": ""+config.password_staging
}
}).then(response => {
const target = (response.body)
})
})
it('GET /capture', () => {
cy.session().then(abc =>{
cy.request({
method: 'GET',
url: '/capture/'+target
})
})
})
目标是从target = (response.body)
捕获JSON数组的解析
发布于 2019-01-27 23:27:17
你有两个选择:
.then(response => {
const target = (response.body)
})
代码没有返回任何内容,所以cy.session().then(abc =>{ ...
代码得到了整个response
(abc
是第一个.then
的响应)
.then(response => {
const target = (response.body)
return target // I added this return
})
然后您的abc
参数将等于response.body
,而不是response
这是因为如果您不从可链式调用返回一个主题,默认的主题将被传递给下一个.then
函数。
如果它能满足你的问题,请告诉我。
附注:欢迎
https://stackoverflow.com/questions/54345371
复制