Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Cypress Difference between Get and Find command

Profile picture for user arilio666
Written by arilio666 on 10/08/2021 - 15:06

In cypress, get() and find() commands are very much identical but have their own distinguish. The objectives which are achieved by these two methods are also pretty identical. Both the commands have args that are optional but also vary in type. 

Let's see the differences between these two.

1. Used directly with cy object

Get

Get can be used directly with cy object. This is perfectly fine.

 cy.find("ul#sub-menu li a");

Find

The main difference is that find() can be chained with other methods and cannot be used directly with the object cy. It can only be chained with methods sticking with object such as get(). If you will try to use find directly, you will get message "A child command must be chained after a parent because it operates on a previous subject." 

 cy.find("ul#sub-menu li a");

2. Syntax

 Get

cy.get(selector)
cy.get(selector, options)
cy.get(alias)
cy.get(alias, options)

Find

.find(selector)
.find(selector, options)

You can use cy.get() for aliases of primitives, regular objects, or even DOM elements which is not possible with Find command. Also, get command has one additional option withinSubject.

3. withinSubject

Get

The default value for this is null, as this determines the origin point of the elements to be searched. If omitted, the origin of the search will be from the root. 

cy.get('.s',{ withinSubject : document.getElementById('#Prod_id')};

Find

Find does not support withinSubject option.

4. Timeout

Get

 With Get you have common timeout for all the parent and child elements which are used as a locator

cy.get('ul#sub-menu li a',{ timeout: 9000 });

Here you can not use separate timeout for li and a. Below implementation is wrong. Here li and a will refer to all the elements of the complete page not the child of ul#sub-menu

cy.get("ul#sub-menu", {timeout: 2000}).get('li', {timeout: 7000}).get('a',{timeout: 10000})

Find

In below example timeout for ul#sub-menu will be 2000, for li 7000 and for a it will be 10000.

cy.get("ul#sub-menu", {timeout: 2000}).find('li', {timeout: 7000}).find('a',{timeout: 10000})

5. Log

Get

 With get you can set log to true or false for complete locator or parent-child elements

cy.get('ul#sub-menu li a', { log: false });

Here you can not create separate log for li and a element.

Find

 Using find along with get we can set true and false for parent and child element separately.

cy.get("ul#sub-menu", {log:true}).find('li', {log:false}).find('a',{log:true});

In below output you can observe log is recorded for ul#sub-menu and a element but it is skipped for li.

logs for find command

Final Verdict

Get - So finally, as we can see, the get() command is much used to fetch the elements in list or group and is mainly used to chain with cy object tracked using CSS selector.

Find - The find() command is useful in fetching the nested elements present within a tag, mostly in a parent-child concept, so it is a faster and more isolated way to fetch the unique child element efficiently.

Related Content
Cypress Tutorial
Cypress Get Command
Cypress find command
Tags
Cypress
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use