Cypress - Basic Commands

Cypress basic commands are listed below −
It is used to create an assertion and is an alias of .should ().
The usage is as follows −
//element is visible & enabled
cy.get('#txt').should('be.visible').and('be.enabled')
//element is checked
cy.contains('Subject').and('be.checked')

It provides an alias for later usage.
The usage is as follows −
//alias element as parent
cy.get('#txt').find('li').first().as('parent')

It blurs an element in focus.
The usage is as follows −
//blur input
cy.get('#txt'). type('abc').blur()

It checks radio buttons or checkboxes and is applied to elements having input tags.
The usage is as follows −
//checks element having class attribute chkbox
cy.get('.chkbox').check()

It obtains the sub elements of an element.
The usage is as follows −
//obtains children of element n
cy.get('n').children()

It removes the value from textarea or input.
The usage is as follows −
//removes input abc
cy.get('#txt'). type('abc').clear()

It removes a particular browser cookie.
The usage is as follows −
//clear abc cookie
cy.clearCookie('abc')

It removes the browser cookies from an existing domain and subdomain.
The usage is as follows −
//clear all cookies
cy.clearCookies()

It removes the local Storage data from an existing domain and subdomain.
The usage is as follows −
//clear all local storage
cy. clearLocalStorage ()

It clicks an element in Document Object Model (DOM).
The usage is as follows −
//click on element with id txt
cy.get('#txt').click()

It obtains an element having a specific text. The element can have more than the text and
still match.
The usage is as follows −
//returns element in #txt having Tutor text
cy.get('#txt').contains('Tutor')

It double-clicks an element in Document Object Model (DOM).
The usage is as follows −
//double clicks element with id txt
cy.get('#txt').dblclick()

It fixes a debugger and log values are returned by prior command.
The usage is as follows −
//pause to debug at start of command
cy.get('#txt').debug()

It obtains window.document on the active page.
The usage is as follows −
cy.document()

It iterates through an array having the property length.
The usage is as follows −
//iterate through individual li
cy.get('li').each(() => {...})

It ends a command chain.
The usage is as follows −
//obtain null instead of input
cy.contains('input').end()

It refers to an element at a particular index in an array of elements.
The usage is as follows −
//obtain third td in tr
cy.get('tr>td').eq(2)

It runs a system command.
The usage is as follows −
cy.exec('npm init')

It obtains the descendant elements of a particular locator.
The usage is as follows −
//obtain td from tr
cy.get('tr').find('td')

It obtains the first element from a group of elements.
The usage is as follows −
//obtain first td in tr
cy.get('tr>td').first()

It obtains single or multiple elements by locator.
The usage is as follows −
//obtain td from tr

It obtains the descendant elements of a particular locator.
The usage is as follows −
//obtain all td from tr in list
cy.get('tr>td')

It obtains a particular browser cookie by its name.
The usage is as follows −
cy.getCookie('abc')

It obtains all the cookies
The usage is as follows −
cy.getCookies()

It moves forward or backward to the next or previous URL in browser history.
The usage is as follows −
//like clicking back button
cy.go('back')
//like clicking forward button
cy.go('forward')

It launches an URL.
The usage is as follows −
cy.visit('https://www.tutorialspoint.com/index.htm')

It obtains the immediate sibling of an element within a group of elements in Document
Object Model (DOM).
The usage is as follows −
//gives the following link in element l.
cy.get('l a:first').next()

It obtains the parent element from a group of elements in DOM.
The usage is as follows −
//get parent of element with class h
cy.get('.h').parent()

It is used to create an assertion and is an alias of .and ().
The usage is as follows −
//assert element is visible & enabled
cy.get('#txt').should('be.visible').and('be.enabled')

Wait for a certain time in milliseconds or for an aliased element prior to moving the following step.
The usage is as follows −
cy.wait(1000)

It obtains the document.title of the active page.
The usage is as follows −
cy.title()

It manages the dimension and positioning of the screen.
The usage is as follows −
// viewport to 100px and 500px
cy.viewport(100, 500)

It prints the messages to the Command Log.
The usage is as follows −
cy.log('Cypress logging ')

It is used for page reloading.
The usage is as follows −
cy.reload()


    73 Lectures 
     12 hours 

 Rahul Shetty
 Print
 Add Notes
 Bookmark this page