---
id: api/commands/as
title: as | Cypress Documentation
description: >-
  Assign an alias for later use in Cypress. Reference the alias later within a
  `cy.get()` query or `cy.wait()` command with an `@` prefix.
section: api
source_path: docs/api/commands/as.mdx
version: 3cf5b86b3403f604bdf7f3e35025c3bc3865e02c
updated_at: '2026-05-07T17:44:31.931Z'
---
# as

Assign an alias for later use. Reference the alias later within a [`cy.get()`](/llm/markdown/api/commands/get.md) query or [`cy.wait()`](/llm/markdown/api/commands/wait.md) command with an `@` prefix.

**Note:** `.as()` assumes you are already familiar with core concepts such as [aliases](/llm/markdown/app/core-concepts/variables-and-aliases.md)

## Syntax

```
.as(aliasName).as(aliasName, options)
```

### Usage

**Correct Usage**

```
cy.get('.main-nav').find('li').first().as('firstNav') // Alias element as @firstNavcy.get('input.username').invoke('val').as('username', { type: 'static' }) // Alias that references the value at the time the alias was createdcy.intercept('PUT', '/users').as('putUser') // Alias route as @putUsercy.stub(api, 'onUnauth').as('unauth') // Alias stub as @unauthcy.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()`](/llm/markdown/api/commands/get.md) or [`cy.wait()`](/llm/markdown/api/commands/wait.md) command using an `@` prefix.

**options _(Object)_**

Pass in an options object to change the default behavior of `.as()`.

| Option | Default | Description |
| --- | --- | --- |
| `type` | `query` | The type of alias to store, which impacts how the value is retrieved later in the test. Valid values are `query` and `static`. A `query` alias re-runs all queries leading up to the resulting value each time the alias is requested. A `static` alias is retrieved once when the alias is stored, and will never change. `type` has no effect when aliasing intercepts, spies, and stubs. |

### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)

*   `.as()` yields the same subject it was given.
*   It is _safe_ to chain further commands after `.as()`.

## Examples

### DOM element

Aliasing a DOM element and then using [`cy.get()`](/llm/markdown/api/commands/get.md) to access the aliased element.

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

### Intercept

Aliasing an intercepted route defined with [`cy.intercept()`](/llm/markdown/api/commands/intercept.md) and then using [`cy.wait()`](/llm/markdown/api/commands/wait.md) to wait for the aliased route.

```
// `PUT` requests on the `/users` endpoint will be stubbed with// the `user` fixture and be aliased as `editUser`cy.intercept('PUT', '/users', { fixture: 'user' }).as('editUser')// we'll assume submitting `form` triggers a matching requestcy.get('form').submit()// once a response comes back from the `editUser`// this `wait` will resolve with the subject containing `url`cy.wait('@editUser').its('url').should('contain', 'users')
```

More examples of aliasing routes can be found [here](/llm/markdown/api/commands/intercept.md#Aliasing-an-intercepted-route).

### Fixture

Aliasing [`cy.fixture()`](/llm/markdown/api/commands/fixture.md) 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.`)})
```

Note the use of the standard function syntax. Using [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) to access aliases via `this` won't work because of [the lexical binding](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#No_separate_this) of `this`.

## Notes

### Aliases are reset

**Note:** all aliases are reset before each test. See the [aliases guide](/llm/markdown/app/core-concepts/variables-and-aliases.md) for details.

### 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()`](/llm/markdown/api/commands/then.md) 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 [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)

*   `.as()` requires being chained off a previous command.

### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions)

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

### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#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:

## History

| Version | Changes |
| --- | --- |
| [12.3.0](/llm/markdown/app/references/changelog.md#12-3-0) | Added option `type` to opt into the pre-12.0.0 behavior. |
| [12.0.0](/llm/markdown/app/references/changelog.md#12-0-0) | All aliases now re-run queries leading up to them by default. |

## See also

*   [`cy.get()`](/llm/markdown/api/commands/get.md)
*   [`cy.wait()`](/llm/markdown/api/commands/wait.md)
*   [Aliases](/llm/markdown/app/core-concepts/variables-and-aliases.md)
