Cypress API 测试:如何在一个 it 块生成的数据中访问另一个 it 块

huangapple go评论53阅读模式
英文:

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 
...

See Environment Variables

huangapple
  • 本文由 发表于 2023年4月13日 20:54:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定