Cypress most common commands
Cypress is a fast, easy and reliable testing tool for anything that runs in a browser. It’s becoming more popular choice of a tool than any other. I have collected some of the most common commands I use on a daily bases.
Cypress basic commands are listed below in no particular order −
get
Get single or multiple elements by the locator.
The usage is as follows −
1
cy.get('.list > li') // Yield the <li>'s in .list
first
Get the first elements of a particular locator.
The usage is as follows −
1
cy.get('nav a').first() // Yield first link in nav
click
It clicks an element in Document Object Model (DOM).
The usage is as follows −
1
cy.get('element').click()
contains
It obtains an element having a specific text. The element can have more than the text and still match.
The usage is as follows −
1
cy.get('element').contains('Tutor')
and
It is used to create an assertion and is an alias of .should ().
The usage is as follows −
1
2
cy.get('element').should('be.visible').and('be.enabled')
cy.contains('Subject').and('be.checked')
visit
It launches an URL.
The usage is as follows −
1
cy.visit('https://www.pirasanth.com/blog')
as
It provides an alias for later usage.
The usage is as follows −
1
cy.get('element').find('li').first().as('parent')
clear
It removes the value from textarea or input.
The usage is as follows −
1
cy.get('element'). type('abc').clear()
clearCookie
It removes a particular browser cookie.
The usage is as follows −
1
cy.clearCookie('cookie_name')
clearCookies
It removes the browser cookies from an existing domain and subdomain.
The usage is as follows −
1
cy.clearCookies()
each
It iterates through an array having the property length.
The usage is as follows −
1
cy.get('li').each(() => {...})
find
It obtains the descendant elements of a particular locator.
The usage is as follows −
1
cy.get('tr').find('td')
should
It is used to create an assertion and is an alias of .and ().
The usage is as follows −
1
cy.get('element').should('be.visible').and('be.enabled')
getCookie
It obtains a particular browser cookie by its name.
The usage is as follows −
1
cy.getCookie('cookie_name')
getCookies
It obtains all the cookies
The usage is as follows −
1
cy.getCookies()
If you feel like there is a command that should be on this list, please leave a comment below!