Cypress most common commands

Back to Blog page

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.

Author Pirasanthan Jesugeevegan

Pirasanthan Jesugeevegan

Dec 27, 2022 • 2 min read

Cypress basic commands are listed below in no particular order −

get

Get single or multiple elements by the locator.

The usage is as follows −

javascript
cy.get('.list > li') // Yield the <li>'s in .list
first

Get the first elements of a particular locator.

The usage is as follows −

javascript
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 −

javascript
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 −

javascript
cy.get('element').contains('Tutor')
and

It is used to create an assertion and is an alias of .should ().

The usage is as follows −

javascript
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 −

javascript
cy.visit('https://www.pirasanth.com/blog')
as

It provides an alias for later usage.

The usage is as follows −

javascript
cy.get('element').find('li').first().as('parent')
clear

It removes the value from textarea or input.

The usage is as follows −

javascript
cy.get('element'). type('abc').clear()
clearCookie

It removes a particular browser cookie.

The usage is as follows −

javascript
cy.clearCookie('cookie_name')
clearCookies

It removes the browser cookies from an existing domain and subdomain.

The usage is as follows −

javascript
cy.clearCookies()
each

It iterates through an array having the property length.

The usage is as follows −

javascript
cy.get('li').each(() => {...})
find

It obtains the descendant elements of a particular locator.

The usage is as follows −

javascript
cy.get('tr').find('td')
should

It is used to create an assertion and is an alias of .and ().

The usage is as follows −

javascript
cy.get('element').should('be.visible').and('be.enabled')
getCookie

It obtains a particular browser cookie by its name.

The usage is as follows −

javascript
cy.getCookie('cookie_name')
getCookies

It obtains all the cookies

The usage is as follows −

javascript
cy.getCookies()

If you feel like there is a command that should be on this list, please leave a comment below!

Tagged with:

Cypress

Cheat Sheets

0

Enjoyed this article?

Leave a comment below!