{
  "doc": {
    "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": "/llm/markdown/api/commands/as.md",
    "version": "3cf5b86b3403f604bdf7f3e35025c3bc3865e02c",
    "updated_at": "2026-05-07T17:44:31.931Z",
    "headings": [
      {
        "id": "api/commands/as#as",
        "text": "as",
        "level": 1
      },
      {
        "id": "api/commands/as#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/as#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/as#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/as#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/as#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/as#dom-element",
        "text": "DOM element",
        "level": 3
      },
      {
        "id": "api/commands/as#intercept",
        "text": "Intercept",
        "level": 3
      },
      {
        "id": "api/commands/as#fixture",
        "text": "Fixture",
        "level": 3
      },
      {
        "id": "api/commands/as#notes",
        "text": "Notes",
        "level": 2
      },
      {
        "id": "api/commands/as#aliases-are-reset",
        "text": "Aliases are reset",
        "level": 3
      },
      {
        "id": "api/commands/as#reserved-words",
        "text": "Reserved words",
        "level": 3
      },
      {
        "id": "api/commands/as#alias-names-cannot-match-some-reserved-words",
        "text": "Alias names cannot match some reserved words.",
        "level": 4
      },
      {
        "id": "api/commands/as#as-is-asynchronous",
        "text": "as is asynchronous",
        "level": 3
      },
      {
        "id": "api/commands/as#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/as#requirements-learn-about-chaining-commands",
        "text": "Requirements Learn about chaining commands",
        "level": 3
      },
      {
        "id": "api/commands/as#assertions-learn-about-assertions",
        "text": "Assertions Learn about assertions",
        "level": 3
      },
      {
        "id": "api/commands/as#timeouts-learn-about-timeouts",
        "text": "Timeouts Learn about timeouts",
        "level": 3
      },
      {
        "id": "api/commands/as#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/as#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/as#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "content": {
    "type": "root",
    "children": [
      {
        "type": "heading",
        "depth": 1,
        "children": [
          {
            "type": "text",
            "value": "as"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Assign an alias for later use. Reference the alias later within a "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/get.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.get()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " query or "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/wait.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.wait()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " command with an `@` prefix."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Note: `.as()` assumes you are already familiar with core concepts such as "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/variables-and-aliases.md",
            "children": [
              {
                "type": "text",
                "value": "aliases"
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Syntax"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": ".as(aliasName).as(aliasName, options)"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Usage"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Correct Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "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"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Incorrect Usage"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "cy.as('foo') // Errors, cannot be chained off 'cy'"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Arguments"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "aliasName (String)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "The name of the alias to be referenced later within a "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/get.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.get()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " or "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/wait.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.wait()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " command using an `@` prefix."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "options (Object)"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Pass in an options object to change the default behavior of `.as()`."
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Option"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Default"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Description"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`type`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "`query`"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "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."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Yields "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management",
            "children": [
              {
                "type": "text",
                "value": "Learn about subject management"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.as()` yields the same subject it was given."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "It is safe to chain further commands after `.as()`."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Examples"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "DOM element"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Aliasing a DOM element and then using "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/get.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.get()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " to access the aliased element."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "it('disables on click', () => {  cy.get('button[type=submit]').as('submitBtn')  cy.get('@submitBtn').click().should('be.disabled')})"
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Intercept"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Aliasing an intercepted route defined with "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/intercept.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.intercept()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " and then using "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/wait.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.wait()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " to wait for the aliased route."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "// `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')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "More examples of aliasing routes can be found "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/intercept.md#Aliasing-an-intercepted-route",
            "children": [
              {
                "type": "text",
                "value": "here"
              }
            ]
          },
          {
            "type": "text",
            "value": "."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Fixture"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Aliasing "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/fixture.md",
            "children": [
              {
                "type": "text",
                "value": "`cy.fixture()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " data and then using `this` to access it via the alias."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "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.`)})"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Note the use of the standard function syntax. Using "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions",
            "children": [
              {
                "type": "text",
                "value": "arrow functions"
              }
            ]
          },
          {
            "type": "text",
            "value": " to access aliases via `this` won't work because of "
          },
          {
            "type": "link",
            "title": null,
            "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#No_separate_this",
            "children": [
              {
                "type": "text",
                "value": "the lexical binding"
              }
            ]
          },
          {
            "type": "text",
            "value": " of `this`."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Notes"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Aliases are reset"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Note: all aliases are reset before each test. See the "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/variables-and-aliases.md",
            "children": [
              {
                "type": "text",
                "value": "aliases guide"
              }
            ]
          },
          {
            "type": "text",
            "value": " for details."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Reserved words"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 4,
        "children": [
          {
            "type": "text",
            "value": "Alias names cannot match some reserved words."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "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`."
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "`as` is asynchronous"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Remember that Cypress commands are async, including `.as()`."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Because of this you cannot synchronously access anything you have aliased. You must use other asynchronous commands such as "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/api/commands/then.md",
            "children": [
              {
                "type": "text",
                "value": "`.then()`"
              }
            ]
          },
          {
            "type": "text",
            "value": " to access what you've aliased."
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Here are some further examples of using `.as()` that illustrate the asynchronous behavior."
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "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.`)    })  })})"
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Rules"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Requirements "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands",
            "children": [
              {
                "type": "text",
                "value": "Learn about chaining commands"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.as()` requires being chained off a previous command."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Assertions "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions",
            "children": [
              {
                "type": "text",
                "value": "Learn about assertions"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.as()` is a utility command."
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.as()` will not run assertions. Assertions will pass through as if this command did not exist."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 3,
        "children": [
          {
            "type": "text",
            "value": "Timeouts "
          },
          {
            "type": "link",
            "title": null,
            "url": "/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts",
            "children": [
              {
                "type": "text",
                "value": "Learn about timeouts"
              }
            ]
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "text",
                    "value": "`.as()` cannot time out."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "Command Log"
          }
        ]
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Alias several routes"
          }
        ]
      },
      {
        "type": "code",
        "lang": null,
        "meta": null,
        "value": "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')"
      },
      {
        "type": "paragraph",
        "children": [
          {
            "type": "text",
            "value": "Aliases of routes display in the routes instrument panel:"
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "History"
          }
        ]
      },
      {
        "type": "table",
        "align": [
          null,
          null
        ],
        "children": [
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Version"
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Changes"
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#12-3-0",
                    "children": [
                      {
                        "type": "text",
                        "value": "12.3.0"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "Added option `type` to opt into the pre-12.0.0 behavior."
                  }
                ]
              }
            ]
          },
          {
            "type": "tableRow",
            "children": [
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/references/changelog.md#12-0-0",
                    "children": [
                      {
                        "type": "text",
                        "value": "12.0.0"
                      }
                    ]
                  }
                ]
              },
              {
                "type": "tableCell",
                "children": [
                  {
                    "type": "text",
                    "value": "All aliases now re-run queries leading up to them by default."
                  }
                ]
              }
            ]
          }
        ]
      },
      {
        "type": "heading",
        "depth": 2,
        "children": [
          {
            "type": "text",
            "value": "See also"
          }
        ]
      },
      {
        "type": "list",
        "ordered": false,
        "start": null,
        "spread": false,
        "children": [
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/get.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`cy.get()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/api/commands/wait.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "`cy.wait()`"
                      }
                    ]
                  }
                ]
              }
            ]
          },
          {
            "type": "listItem",
            "spread": false,
            "checked": null,
            "children": [
              {
                "type": "paragraph",
                "children": [
                  {
                    "type": "link",
                    "title": null,
                    "url": "/llm/markdown/app/core-concepts/variables-and-aliases.md",
                    "children": [
                      {
                        "type": "text",
                        "value": "Aliases"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  "token_estimate": 952
}