2.9 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| as | true |
Assign an alias to a route or DOM element for later use. Reference the alias later within a .get() or .wait() command with a @ prefix.
{% note info New to Cypress? %} Read about Using Aliases first. {% endnote %}
Syntax
.as(aliasName)
Usage
.as() requires being chained off another cy command that yields a DOM element, .stub(), .spy() or .route().
{% fa fa-check-circle green %} Valid Usage
cy.get('.main-nav').find('li').first().as('firstNav')
cy.route('PUT', 'users', 'fx:user').as('putUser')
cy.stub(api, 'onUnauth').as('unauth')
cy.spy(win, 'fetch').as('winFetch')
{% fa fa-exclamation-triangle red %} Invalid Usage
cy.as('foo') // Errors, cannot be chained off 'cy'
cy.title().as('pageTitle') // Errors, 'title' yields a string
Arguments
{% fa fa-angle-right %} aliasName (String)
The name of the alias to be referenced later within a .get() or .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 defaultCommandTimeout
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: