Cypress is a front-end testing tool. It allows us developers and QA Engineers who build web applications using the modern JavaScript framework to set up tests, write tests, run tests, and debug tests. It is popular as it lets us write faster, easier, and more reliable tests. Cypress can test anything that runs in a browser.
Let's learn more about the most basic component - aliases in Cypress.
What are Aliases in Cypress?
An alias is a name or label that is assigned to an element, a file, a user, or a command in a computer system which serves as an alternative way or a shortcut to refer to a more complicated command or name.
Aliases are simply the synonyms of commands for convenience. They are set with the .as() alias command.
An alias can be defined using the .as() command. It is referenced with the @ character and the name of the alias. For aliases of Primitive, regular objects, or even DOM elements, you can use cy.get().
Use of Cypress Aliases
An alias is an essential component that has many uses.
The various uses of Cypress Aliases are:
Use of Cypress in AliasesLet's discuss all the above uses individually and view its implementation as well.
1. Sharing Context
The simplest way to use aliases is to share contests. To alias something we use the .as(). We use the Mocha context objects to alias objects and primitives. The this.* is used to access the alias object. By default, mocha shares context for all the hooks that are applicable for the test and the alias properties are flushed post-execution of a test.
Code:
JavaScript
describe('element', () => {
beforeEach(() => {
cy.wrap('ele1').as('x')
})
context('subelement', () => {
beforeEach(() => {
cy.wrap('ele2').as('y')
})
it('aliases properties', function () {
expect(this.x).to.eq(' ele1 ')
expect(this.y).to.eq(' ele2 ')
})
})
})
By sharing context we can handle fixtures. Instead of using this. We can use @ character to access an alias. We use cy.get() command (an asynchronous command) to access an alias using @, this is a synchronous command.
Code
JavaScript
beforeEach(() => {
//alias fixtures
cy.fixture('users.json').as('u')
})
it('scenario', function () {
//'@' to handle aliases
cy.get('@u').then((u) => {
//access element argument
const i = u[0]
//verification
cy.get('header').should('contain', u.name)
})
})
2. Elements
We can use aliases with DOM elements and then reuse them later. (DOM stands for Document Object Model.)
By default, Cypress makes reference to td collection obtained as the alias cols. We need to used the cy.get() command to use the same cols. Since we use @ in cy.get() in the code below, Cypress will search for the present alias cols and gets its reference.
Code
JavaScript
//alias td in tr
cy.get('tr').find('td').as('cols')
cy.get('@cols').first().click()
3. Routes
We can use aliases with routes. This makes sure that the application has made request. Then it awaits a response from server and accesses the request for verification.
Code:
JavaScript
cy.intercept('POST', '/fruits', { SrNo: 12 }).as('s')
cy.get('#btn').click()
cy.wait('@s').then(({ request }) =>{
//assertion
expect(request.body).to.have.property('Color', 'Fruit')
})
cy.contains('Fruit added')
4. Requests
We can use aliases with requests too. Here, we alias a request and use its properties later.
Code
JavaScript
cy.request('https://jsonplaceholder.cypress.io/comments').as('com')
// other implementations (...if any)
cy.get('@com').should((response) => {
if (response.status === 404) {
// assertion
expect(response).to.have.property('duration')
} else {
// code to do something otherwise
}
})
Conclusion
Aliases help reduce the complexity of code and helps us simplify it to make it more efficient and accessible. It helps to essentially streamline the access to frequently used item. Learning its uses and using it in the right way increases program readability.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING