lundi 28 octobre 2019

How to test API with Cypress?

I am trying to test our API, but each API comes with bearer token.

The test passes when I pass the entire bearer token,

but how can I regenerate the token without having to put the entire token?

// const bearerToken = 'adakdkadamdamdaksdwe232'

const urlConstructed = 'https://someAPI.com/admin/api/v1/customers?orgId=79649057-5edc9ff5b377'
describe('API TEST', function () {
  before(() => {
    preventClearStorage();
    Login(auth.admin2.email, auth.admin2.password);
    cy.reload()
  })
  it('Test API', () => {
    //const token = cy.window.localStorage.getItem('access_token')
    const token = cy.window().its('sessionStorage')
    console.log(JSON.stringify(token))
    cy.request({
        method: 'GET',
          url: urlConstructed,
          headers: {
            Accept: 'application/json',
            Authorization: 'Bearer ' + token,
          },
          timeout: 120000,
          failOnStatusCode: false
    }).then((response) => {
        expect(response).to.have.property('status', 200)
     })
  })
})

Aucun commentaire:

Enregistrer un commentaire