- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get and Find commands in Cypress
Cypress has the get() and find() methods to find elements based on locators on the page. The objective achieved by these two methods are almost identical. The get() method fetches one or a list of web elements with the help of the css locators specified as a parameter to that method.
Syntax
cy.get(selector, args)
The second parameter of the get() method is optional. There can be of three types of parameter as listed below −
log − The default value of log parameter is true. This determines if there will be logging of the command on the console.
cy.get('.product', { log: false });
withinSubject − The default value of withinSubject parameter is null. This determines from where the element should be searched on the page. If omitted, it starts from the element root.
cy.get('.p',{ withinSubject : document.getElementById('#id')};
timeout − The default value of timeout parameter is defaultCommandTimeout (4000 milliseconds) . This determines the waiting time to fetch the element before throwing an error.
cy.get('.p',{ timeout: 5000 });
We can get a list of elements from the get() method. Out of the list or array of elements, we have to choose one of them with the help of eq() method. The eq() method fetches the a DOM element at a particular index starting from index 0.
cy.get('.p') .eq(2).should('contain', 'Tutorialspoint');
The find() method fetches one or multiple elements matching with the selector passed as an argument. The difference between get() and find() methods is that the find() method needs to be chained with other methods like get(). It cannot be used independently with the cy object.
Syntax
.find(selector, args)
The second parameter of the find() method is optional. There can be of two types of parameter as listed below −
log − The default value of log parameter is true. This determines if there will be logging of the command on the console.
cy.get('#parent').find('img', { log: false });
timeout − The default value of timeout parameter is defaultCommandTimeout (4000 milliseconds). This determines the waiting time to fetch the element before throwing an error.
cy.get('#parent').find('img', timeout: 5000 });
A find() command helps to locate elements which are nested within another element or mostly if they have parent child relationship. The find() method helps to locate elements in a faster and efficient way.
Example
Code Implementation with get and find methods.
// test suite describe('Tutorialspoint Test', function () { // test case it('Test Case1', function (){ // test step to launch a URL cy.visit("https://www.tutorialspoint.com/index.htm"); // enter test in the edit box // assertion to validate the number of child elements cy.get('#gs_50d > tbody > tr > td'). should('have.length',2); // locate element with get and find method cy.get('#gs_50d > tbody > tr > td'). find('input') //enter test in the edit box .type('Cypress'); }); });