0

I have an alias defined in 1 test and I would like to use the result in a different test:

it('fills in the login form', () => {
    cy.intercept({
        method: 'POST',
        url: `${Cypress.env('apiURL')}/api/v1/user/login`,
    }).as('login');
    cy.get('[data-cy="inputEmailAddress"]').type(company.users[0].email);
    cy.get('[data-cy="inputPassword"]').type(company.users[0].password);
    cy.get('[data-cy="buttonLogin"]').click();
});

it('does stuff', () => {
    cy.get('@login')
        .its('response')
        .then((res) => {
            expect(res.statusCode).to.eq(200);
        });
});

But I'm getting an error:

cy.get() could not find a registered alias for: @login. You have not aliased anything yet.

Any suggestions on how to make the alias available in a different test?

Peter Boomsma
  • 8,851
  • 16
  • 93
  • 185

2 Answers2

2

You can't use an alias across tests, Cypress clears them to keep state clean.

However it's just javascript - closure variables can be used.

let login;

it('fills in the login form', () => {
    cy.intercept(...)
      .then(interception => login = interception);
    ... 
});

it('does stuff', () => {

    cy.wrap(login)
      .its('response')
      .then((res) => {
          expect(res.statusCode).to.eq(200);
      });
});

Shared context

Also, the .as('login') command set's a variable on the Mocha context (same name as alias) which is not cleared between tests.

it('fills in the login form', () => {
    cy.intercept(...)
      .as('login');
    ... 
});

it('does stuff', function() {    // function syntax to access "this" scope

    cy.wrap(this.login)          // access persistent scoped variable
      .its('response')
      .then((res) => {
          expect(res.statusCode).to.eq(200);
      });
});
Fody
  • 23,754
  • 3
  • 20
  • 37
  • the first is bad practice according to the docs https://docs.cypress.io/guides/core-concepts/variables-and-aliases#Aliases the second doesn't work for me either, this.login is undefined in the following test – ihor.eth Oct 28 '22 at 16:26
  • 2
    `this.login` ***is*** actually defined, if you follow the code properly. – Ferando Oct 29 '22 at 04:42
  • 1
    There are examples in the [cypress examples documention](https://github.com/cypress-io/cypress-example-recipes/blob/cc13866e55bd28e1d1323ba6d498d85204f292b5/examples/fundamentals__fixtures/cypress/e2e/load-fixtures-spec.cy.js) that use **closure variables**. The advice is way out of date and should actually be removed from the docs. You will struggle to adequately explain ***why*** it used to be considered bad advice. – Ferando Oct 29 '22 at 04:46
0

From the Cypress Docs

Mocha automatically shares contexts for us across all applicable hooks for each test. Additionally, these aliases and properties are automatically cleaned up after each test.

So basically at the end of each test cypress clears all the aliases. So for the above code to work, you have move the intercept method to beforeEach(). Something like:

describe('Test Suite', () => {
  beforeEach(() => {
    cy.intercept({
      method: 'POST',
      url: `${Cypress.env('apiURL')}/api/v1/user/login`,
    }).as('login')
  })

  it('fills in the login form', () => {
    cy.get('[data-cy="inputEmailAddress"]').type(company.users[0].email)
    cy.get('[data-cy="inputPassword"]').type(company.users[0].password)
    cy.get('[data-cy="buttonLogin"]').click()
  })

  it('does stuff', () => {
    cy.get('@login')
      .its('response')
      .then((res) => {
        expect(res.statusCode).to.eq(200)
      })
  })
})
Alapan Das
  • 17,144
  • 3
  • 29
  • 52