When using Cypress for automated testing, handling dropdown lists is a common task. If you want to locate and select a specific item within a scrollable dropdown list, you can follow these steps:
-
Locate the dropdown list element: First, identify the dropdown list element using commands like
cy.get()orcy.find(), depending on your selector (e.g.,id,class). -
Trigger the dropdown list: Some dropdowns only display options after user interaction. Use
cy.click()to simulate this action. -
Scroll to the specific option: Before selecting an item, scroll to it using
cy.scrollTo()orcy.scrollIntoView()on the dropdown element. -
Select the target item: Once the item is visible, use
cy.contains()to locate and click it based on text content.
Below is an example code snippet demonstrating how to locate and select an item within a scrollable dropdown list:
javascriptdescribe('Selecting an item from a dropdown list', () => { it('Selects a specific item', () => { cy.visit('https://example.com'); // Replace with your test page URL cy.get('.dropdown').click(); // Assuming '.dropdown' is the class name of the dropdown list cy.get('.dropdown-menu').scrollTo('bottom'); // Assuming '.dropdown-menu' is the container for dropdown content cy.contains('.dropdown-item', 'Specific Item').click(); // Assuming '.dropdown-item' is the class name for dropdown options, 'Specific Item' is the target item }); });
In this example, we visit a page, click the dropdown, scroll to the bottom, and select the specific item. This is a typical approach for handling scrollable dropdown lists with Cypress.