Fetching external api with cypress

Viewed 54

I would like to fetch an email from an email provider and i know that i could use axios or any other http client however since i added cypress can i just use cy.request to get data and store it or it should not be used for such purposes.

I can use axios as it shows here but why if i have cy.request

context('Verify email', () => {
    let inbox;
    before((done) => {
      // Configure timeout
      this.timeout(1000*60*5); // five minutes
      // Query the inbox
      axios.get(`${ENDPOINT}&tag=${TAG}&timestamp_from=${startTimestamp}&livequery=true`).then((response) => {
        inbox = response.data;
        done();
      }).catch((err) => {
        done(err);
      });
    });
1 Answers

You could store the response in a Cypress environment variable.

context('Verify email', () => {
  before(() => {
    cy.request(`${ENDPOINT}}&tag=${TAG}&timestamp_from=${startTimestamp}&livequery=true`)
      .then((response) => {
        Cypress.env('inbox', response.body);
    });
  });

  it('test', () => {
    // reference the variable by Cypress.env('inbox')
  });
});
Related