英文:
Cypress API testing : How to access data generated from it block to another it block
问题
You can access the token generated in the 1st it
block and use it in the 2nd it
block by declaring the token
variable outside of both blocks so that it's accessible in both places. Here's the modified code:
let token; // Declare the token variable outside the blocks
it('Login using new user', () => {
cy.api({
method: "POST",
url: "https://api-stg.sample.com/v1/user/login",
body: {
"mobile_number": "+9912345678",
"password": "p@ssword"
}
})
.then((response) => {
expect(response.status).to.eq(200);
token = response.body.data.user.access_token.token; // Assign the token value here
cy.log("Bearer token: " + token);
});
});
it('Create New Mobile', () => {
cy.api({
method: "POST",
url: "https://api-stg.sample.com/v1/user/add_new_mobile_numbers",
headers: {
'Authorization': 'Bearer ' + token // Use the token here
},
body: {
"mobile_prefix": "+991",
"mobile_number": "9912345679"
}
});
});
By declaring let token;
outside of the blocks, you can access the token
variable in both the 1st and 2nd it
blocks. This should resolve the "token is not defined" error you were encountering.
英文:
How can I access the token generated from 1st it
block to 2nd it
block.
here is the code:
1st it block
it('Login using new user', () => {
cy.api({
method: "POST",
url: "https://api-stg.sample.com/v1/user/login",
body:
{
"mobile_number": "+9912345678"
"password": "p@ssword"
}
})
.then((response) => {
expect(response.status).to.eq(200)
})
.then((response) => {
const bearer_token = response.body.data.user.access_token.token //
*in here where I get and put the token in var bearer_token*
cy.log("Bearer token: " + token )
})
})
2nd it block
it('Create New Mobile', () => {
cy.api({
method: "POST",
url: "https://api-stg.sample.com/v1/user/add_new_mobile_numbers",
headers: {
'Authorization': 'Bearer ' + token // *in here where I put the token*
},
body:
{
"mobile_prefix": "+991",
"mobile_number": "9912345679"
}
})
I login from 1st block, then i need to use the token to add new mobile num in 2nd block.
in cypress dashboard it returns an error:
token is not defined
答案1
得分: 1
This is ideal to save to a test environment variable via Cypress.env()
, since they persist across the test boundary.
it('使用新用户登录', () => {
cy.api(...)
.then((response) => {
const token = response.body.data.user.access_token.token
// 存储环境变量
Cypress.env('token', token)
})
...
it('创建新的移动设备', () => {
cy.api({
...
headers: {
'Authorization': `Bearer ${Cypress.env('token')}` // 使用环境变量
...
查看环境变量。
英文:
This is ideal to save to a test environment variable via Cypress.env()
, since they persist across the test boundary.
it('Login using new user', () => {
cy.api(...)
.then((response) => {
const token = response.body.data.user.access_token.token
// store env var
Cypress.env('token', token)
})
...
it('Create New Mobile', () => {
cy.api({
...
headers: {
'Authorization': `Bearer ${Cypress.env('token')}` // use env var
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论