Cypress Locator Methods | Bondar Academy
Course: Cypress UI Testing with JavaScript
Module: Interaction with Web Elements
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore Cypress locator methods , which are essential for locating web elements on a page. There are three primary methods: get , find , and contains . 1. Method Overview get : Locates elements globally on the page. find : Targets only child elements of a specified parent. contains : Finds elements based on their text content. 2. Detailed Explanation The contains method is particularly useful for locating elements by their text. For example, to click on a "Sign In" button, you can use: cy.contains('Sign In') However, it is case sensitive , meaning the text must match exactly as it appears in the DOM. You can disable this sensitivity by passing a second argument: cy.contains('Sign In', { matchCase: false }) Additionally, contains allows for partial matches . For instance, searching for just "sign" will still locate the "Sign In" button. 3. Using Method Find The find method is used to locate child elements. For example, to find a button within a form: cy.get('form').find('button') In contrast, get searches the entire page regardless of context. 4. Summary Method contains : Finds elements by text, supports partial matches, and is case sensitive. Method find : Locates child elements. Method get : Searches globally across the page. Understanding these methods enhances your ability to effectively interact with web elements in Cypress tests.