Skip to main content

Cypress find command

In cypress, there is a unique way to find web elements using the find() command, Querying behavior of find() command is exactly same as JQuery find() method.

So typically, a find() command returns one or two DOM elements based on the CSS selector or any other parameter.

Now you may be wondering we have get() command for that why we need to see find()?

The only difference in find() and get() is that find() can be chained with other methods and cannot be used directly with the object cyfind() can only be chained with methods sticking with cy object such as get().

Syntax

.find('selector')
.find(selector, options)
  • Selector is used to filter matching descendent DOM element.
  • You can use log, timeout and includeShadowDOM options to change the default behavior of .find().

Example

describe('Automate AutoPract',()=>{
    it('Should load the url and find the clothing section and assert',()=>{
        
        // visit the site
        cy.visit('http://www.autopract.com/#/home/fashion')
        
        // close a popup that occurs immediately after the load.
        cy.get('.close').click()
        
        // navigating to the sites side list section where all the categories are placed.
        cy.get('.bar-style').click()
        cy.get('#sub-menu').should('be.visible')
        cy.get("ul[id='sub-menu']").find('li a').contains(' clothing ').should('be.visible')
        
    })

})

Above command can also be written as:

cy.get("ul[id='sub-menu']").find('li').find('a').contains(' clothing ').should('be.visible')

 However if you will replace get with find here, it will return error "Oops, it looks like you are trying to call a child command before running a parent command"

cy.find("ul[id='sub-menu'] li a").contains(' clothing ').should('be.visible')

but you can get the same result using get

cy.get("ul[id='sub-menu'] li a").contains(' clothing ').should('be.visible')
  • Here We are automating a site called http://www.autopract.com/#/home/fashion
  • We are using the get() to fetch the web element of the entire sub-menu section using CSS selector
  • Then go to ul->li, and inside li, go the 'a' tag which is the subtag or the child of li, and here We have used the find command to fetch the title.
  • And finally checking whether the clothing section is visible with should().
  • .find() will automatically retry until the element(s) exist in the DOM. Also, It will automatically retry until all chained exception pass.

As the find command is always chained, it is often chained with the get() command to be chained for the child element from a parent.

cy.get(parent).find(child)


  • This is the dom of the browser.
  • Here We have fetched the 'a' tag, which is the subtag of li, and li, which is the subtag of ul.
  • Thus, it is clear that the find() command is used to search for the web elements that are nested or have parent-child relation types of features.
  • That way, it'll isolate the child and gives out the result as quickly as possible.

Tags
Submitted by arilio666 on October 8, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.