as

Assign an alias for later use. Reference the alias later within a cy.get() or cy.wait() command with an @ prefix.

Syntax

.as(aliasName)

Usage

Correct Usage

cy.get('.main-nav').find('li').first().as('firstNav') // Alias element as @firstNav
cy.intercept('PUT', '/users').as('putUser') // Alias route as @putUser
cy.stub(api, 'onUnauth').as('unauth') // Alias stub as @unauth
cy.spy(win, 'fetch').as('winFetch') // Alias spy as @winFetch

Incorrect Usage

cy.as('foo') // Errors, cannot be chained off 'cy'

Arguments

aliasName (String)

The name of the alias to be referenced later within a cy.get() or cy.wait() command using an @ prefix.

Yields

  • .as() yields the same subject it was given from the previous command.

Examples

DOM element

Aliasing a DOM element and then using cy.get() to access the aliased element.

it('disables on click', () => {
  cy.get('button[type=submit]').as('submitBtn')
  cy.get('@submitBtn').click().should('be.disabled')
})

Route

Aliasing a route and then using cy.wait() to wait for the aliased route.

cy.intercept('PUT', '/users', { fixture: 'user' }).as('putUser')
cy.get('form').submit()
cy.wait('@putUser').its('url').should('contain', 'users')

Fixture

Aliasing cy.fixture() data and then using this to access it via the alias.

beforeEach(() => {
  cy.fixture('users-admins.json').as('admins')
})

it('the users fixture is bound to this.admins', function () {
  cy.log(`There are ${this.admins.length} administrators.`)
})

Notes

Aliases are reset

Reserved words

Alias names cannot match some reserved words.

Some strings are not allowed as alias names since they are reserved words in Cypress. These words include: test, runnable, timeout, slow, skip, and inspect.

as is asynchronous

Remember that Cypress commands are async, including .as().

Because of this you cannot synchronously access anything you have aliased. You must use other asynchronous commands such as .then() to access what you've aliased.

Here are some further examples of using .as() that illustrate the asynchronous behavior.

describe('A fixture', () => {
  describe('alias can be accessed', () => {
    it('via get().', () => {
      cy.fixture('admin-users.json').as('admins')
      cy.get('@admins').then((users) => {
        cy.log(`There are ${users.length} admins.`)
      })
    })

    it('via then().', function () {
      cy.fixture('admin-users.json').as('admins')
      cy.visit('/').then(() => {
        cy.log(`There are ${this.admins.length} admins.`)
      })
    })
  })

  describe('aliased in beforeEach()', () => {
    beforeEach(() => {
      cy.fixture('admin-users.json').as('admins')
    })

    it('is bound to this.', function () {
      cy.log(`There are ${this.admins.length} admins.`)
    })
  })
})

Rules

Requirements

  • .as() requires being chained off a previous command.

Assertions

  • .as() is a utility command.
  • .as() will not run assertions. Assertions will pass through as if this command did not exist.

Timeouts

  • .as() cannot time out.

Command Log

Alias several routes

cy.intercept('/company/*').as('companyGet')
cy.intercept('/roles/*').as('rolesGet')
cy.intercept('/teams/*').as('teamsGet')
cy.intercept(/users\/\d+/).as('userGet')
cy.intercept('PUT', /^\/users\/\d+/).as('userPut')

Aliases of routes display in the routes instrument panel:

Command log for route

See also

© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/as