Files
cypress/docs/source/api/commands/as.md

2.6 KiB

title, comments
title comments
as false

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

{% note info %} Note: .as() assumes you are already familiar with core concepts such as {% url 'aliases' aliases %} {% endnote %}

Syntax

.as(aliasName)

Usage

.as() should be chained off another cy command.

{% fa fa-check-circle green %} Valid Usage

cy.get('.main-nav').find('li').first().as('firstNav') // Alias first 'li' as @firstNav
cy.route('PUT', 'users', 'fx:user').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  

{% fa fa-exclamation-triangle red %} Invalid Usage

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

Arguments

{% fa fa-angle-right %} aliasName (String)

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

Yields

.as() yields the DOM element or route chained from the previous command.

Timeout

.as() will retry the chain of commands before the .as() command for the duration of the {% url defaultCommandTimeout configuration#Timeouts %}

Examples

DOM Element

cy
  .route('PUT', /^\/users\/\d+/, 'fixture:user').as('userPut')
  .get('form').submit()
  .wait('@userPut')
    .its('url').should('contain', 'users')

Route

cy
  .route('PUT', 'users', 'fx:user').as('userPut')
  .get('form').submit()
  .wait('@userPut')
    .its('url').should('contain', 'users')

Notes

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.

Command Log

Alias several routes

cy
  .route(/company/, 'fixture:company').as('companyGet')
  .route(/roles/, 'fixture:roles').as('rolesGet')
  .route(/teams/, 'fixture:teams').as('teamsGet')
  .route(/users\/\d+/, 'fixture:user').as('userGet')
  .route('PUT', /^\/users\/\d+/, 'fixture:user').as('userPut')

Aliases of routes display in the routes instrument panel:

Command log for route

See also

  • {% url cy.get() get %}
  • {% url 'Aliases' aliases %}
  • {% url cy.wait() wait %}