Aliasing

Learn how we can simplify our selectors in Cypress using aliasing.

We’ve looked at how to select elements in the previous lesson using the cy.get command. However, there’s one problem with this.Selectors are bound to change from time to time, and using the same selectors in different places means we have to update the test suite in multiple places to avoid flakes. This is what aliases aim to solve.


What are aliases?

Aliasing selectors is a way to assign a name to an element that can be referenced for later use. This way, we only need to query using the selector once, but can use the alias throughout the test multiple times. If we need to update the selector, we will only have to do it in one place.


Creating aliases

To create aliases in Cypress, we can use the .as ...