Cypress – Basic Commands

Cypress basic commands are listed below โˆ’

and

It is used to create an assertion and is an alias .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')

as

It provides an alias for later usage.

The usage is as follows โˆ’

//alias element as parent
cy.get('#txt').find('li').first().as('parent')

blur

It blurs an element in focus.

The usage is as follows โˆ’

//blur input
cy.get('#txt'). type('abc').blur()

check

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()

children

It obtains the sub-elements of an element.

The usage is as follows โˆ’

//obtains children of element n
cy.get('n').children()

clear

It removes the value from the text area or input.

The usage is as follows โˆ’

//removes input abc
cy.get('#txt'). type('abc').clear()

clearCookie

It removes a particular browser cookie.

The usage is as follows โˆ’

//clear abc cookie
cy.clearCookie('abc')

clearCookies

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

The usage is as follows โˆ’

//clear all cookies
cy.clearCookies()

clearLocalStorage

It removes the local Storage data from an existing domain and subdomain.

The usage is as follows โˆ’

//clear all local storage
cy. clearLocalStorage ()

click

It clicks an element in Document Object Model (DOM).

The usage is as follows โˆ’

//click on element with id txt
cy.get('#txt').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 โˆ’

//returns element in #txt having Tutor text
cy.get('#txt').contains('Tutor')

dblclick

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()

debug

It fixes a debugger and log values are returned by the prior command.

The usage is as follows โˆ’

//pause to debug at start of command
cy.get('#txt').debug()

document

It obtains a window .document on the active page.

The usage is as follows โˆ’

cy.document()

each

It iterates through an array having the property length.

The usage is as follows โˆ’

//iterate through individual li
cy.get('li').each(() => {...})

end

It ends a command chain.

The usage is as follows โˆ’

//obtain null instead of input
cy.contains('input').end()

eq

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)

exec

It runs a system command.

The usage is as follows โˆ’

cy.exec('npm init')

find

It obtains the descendant elements of a particular locator.

The usage is as follows โˆ’

//obtain td from tr
cy.get('tr').find('td')

first

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()

get

It obtains single or multiple elements by the locator.

The usage is as follows โˆ’

//obtain td from tr

find

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')

getCookie

It obtains a particular browser cookie by its name.

The usage is as follows โˆ’

cy.getCookie('abc')

getCookies

It obtains all the cookies

The usage is as follows โˆ’

cy.getCookies()

go

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')

visit

It launches an URL.

The usage is as follows โˆ’

cy.visit('https://adglob.in/index.htm')

next

It obtains the immediate sibling of an element within a group of elements in the Document Object Model (DOM).

The usage is as follows โˆ’

//gives the following link in element l.
cy.get('l a:first').next()

parent

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()

should

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

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)

title

It obtains the document.title of the active page.

The usage is as follows โˆ’

cy.title()

viewport

It manages the dimension and positioning of the screen.

The usage is as follows โˆ’

// viewport to 100px and 500px
cy.viewport(100, 500)

log

It prints the messages to the Command Log.

The usage is as follows โˆ’

cy.log('Cypress logging ')

reload

It is used for page reloading.

The usage is as follows โˆ’

cy.reload()

This Post Has One Comment

  1. home based business 4u

    Hey there! I realize this is kind of off-topic however I had
    to ask. Does building a well-established blog like yours require a massive amount work?
    I am completely new to blogging however I do write in my journal
    daily. I’d like to start a blog so I can easily share my own experience and views online.

    Please let me know if you have any kind of suggestions or tips for new
    aspiring bloggers. Appreciate it!

Leave a Reply