如何处理 Cypress 中的动态下拉菜单
介紹
處理動態下拉選單是現代 Web 應用程式中的常見挑戰,特別是當下拉選項是從 API 動態取得或基於使用者互動載入時。當使用 Cypress 對此類下拉式選單進行自動化測試時,您需要確保選擇正確的選項,即使它們是在延遲一段時間後渲染的。
本部落格將引導您完成與 Cypress 中的動態下拉清單互動的過程,並提供常見場景的範例,包括由 API 回應填入的下拉清單和根據使用者輸入變更的下拉清單。
為什麼動態下拉選單具有挑戰性?
動態下拉清單通常會帶來測試挑戰,因為:
- 選項最初不存在:下拉選項可能會在使用者操作或 API 呼叫後非同步載入。
- 下拉內容變更:根據使用者輸入或交互,下拉選項可能會動態變更。
- DOM 更新: Cypress 需要等待 DOM 更新才能與下拉式選單互動。
Cypress 提供了多個強大的命令來應對這些挑戰,確保您可以可靠地從動態下拉清單中選擇正確的選項。
處理動態下拉選單的逐步指南
讓我們透過一個基本範例來了解 Cypress 如何處理動態下拉式選單。
第 1 步:與下拉觸發器互動
大多數動態下拉選單最初是隱藏的,僅在使用者點擊按鈕或輸入欄位時才顯示。首先,您需要與觸發元素進行互動。
範例 HTML:
<select id="country-dropdown"> <option value="" disabled selected>Select a country</option> </select> <button id="load-countries">Load Countries</button>
模擬使用者互動:
it('should click the button to load dropdown options', () => { cy.visit('/dropdown-page'); // Visit the page with the dynamic dropdown cy.get('#load-countries').click(); // Click the button to load the dropdown options });
這會按一下按鈕,在此範例中會觸發 API 呼叫或另一個程序來動態填入下拉選項。
第 2 步:等待下拉式選單填充
在動態下拉式選單中,選項可能無法立即可用。 Cypress 可以使用諸如 should('exist') 之類的斷言或等待元素變得可用。
填充後處理下拉清單的範例:
it('should wait for dropdown options to be populated', () => { cy.get('#country-dropdown').should('exist').click(); // Click to open the dropdown // Wait for the dropdown options to populate cy.get('#country-dropdown option').should('have.length.greaterThan', 1); });
在這裡,Cypress 會等到下拉選項可用後再繼續。
第 3 步:動態選擇選項
填入下拉清單後,您可以使用 cy.select() 或直接與 DOM 元素互動來選擇所需的選項。
選擇國家的範例:
it('should select a country from the dynamic dropdown', () => { cy.get('#country-dropdown').select('India'); // Select by visible text });
如果您的下拉式選單不使用本機
it('should manually select a country from a custom dropdown', () => { cy.get('#country-dropdown').click(); // Open the dropdown // Select the desired option by clicking on the visible text cy.contains('li', 'India').click(); });
處理類型和搜尋動態下拉列表
許多現代應用程式使用類型和搜尋下拉清單,使用者在輸入欄位中鍵入內容,下拉選項會根據輸入的文字動態過濾。讓我們看看 Cypress 中如何處理此類場景。
型別與搜尋動態下拉清單範例
HTML 結構範例:
<div class="search-dropdown"> <input type="text" id="search-input" placeholder="Search countries..."> <ul id="dropdown-options"> <li>USA</li> <li>Canada</li> <li>Australia</li> </ul> </div>
在這種情況下,下拉清單中的選項將根據使用者的輸入進行過濾。
第 1 步:輸入與篩選選項
當在輸入欄位中輸入內容時,Cypress 可以模擬使用者輸入並動態過濾選項。
it('should filter and select a country from a type-and-search dropdown', () => { cy.get('#search-input').type('Can'); // Type into the input field to filter options // Verify that the filtered result appears cy.get('#dropdown-options li').should('have.length', 1); // Verify that the option matches the search term cy.get('#dropdown-options li').first().should('contain.text', 'Canada'); // Click to select the filtered option cy.get('#dropdown-options li').first().click(); });
在這種情況下,下拉清單中的選項將根據使用者的輸入進行過濾。
第 1 步:輸入與篩選選項
當在輸入欄位中輸入內容時,Cypress 可以模擬使用者輸入並動態過濾選項。
it('should filter and select a country from a type-and-search dropdown', () => { cy.get('#search-input').type('Can'); // Type into the input field to filter options // Verify that the filtered result appears cy.get('#dropdown-options li').should('have.length', 1); // Verify that the option matches the search term cy.get('#dropdown-options li').first().should('contain.text', 'Canada'); // Click to select the filtered option cy.get('#dropdown-options li').first().click(); });
此程式碼模擬在搜尋框中輸入 Can,驗證下拉清單是否已過濾為僅顯示“加拿大”,然後選擇該選項。
第 2 步:等待下拉選項動態載入(API 驅動程式)
有時,類型和搜尋下拉清單由 API 支持,該 API 根據使用者的輸入返回選項。 Cypress 可以等待 API 回應並驗證選項。
it('should handle type-and-search dropdown populated by API', () => { // Intercept the API call triggered by typing cy.intercept('GET', '/api/countries?search=Can', { fixture: 'filtered-countries.json' // Mocked API response with filtered data }).as('searchCountries'); // Type into the input to trigger the API call cy.get('#search-input').type('Can'); // Wait for the API response cy.wait('@searchCountries'); // Validate the filtered results cy.get('#dropdown-options li').should('have.length', 1); cy.get('#dropdown-options li').first().should('contain.text', 'Canada'); // Select the option cy.get('#dropdown-options li').first().click(); });
在這裡,我們使用 cy.intercept() 來攔截和模擬 API 請求,該請求根據鍵入的輸入獲取過濾選項。
處理由 API 呼叫填充的下拉式選單
動態下拉清單通常由 API 呼叫填充,這表示在伺服器回應之前資料不可用。為了處理這些下拉式選單,Cypress 提供了 cy.intercept() 來模擬或攔截網路呼叫。
以下是攔截 API 回應並從動態填入的下拉清單中選擇值的範例:
it('should handle dropdown populated by API', () => { // Intercept the API call cy.intercept('GET', '/api/countries', { fixture: 'countries.json' }).as('getCountries'); cy.get('#load-countries').click(); // Trigger the API call // Wait for the API call to complete cy.wait('@getCountries'); // Now select an option from the populated dropdown cy.get('#country-dropdown').select('Australia'); });
In this case, we use cy.intercept() to mock the /api/countries endpoint and provide a fixture (countries.json) with predefined data. This ensures that the dropdown is populated with the expected values, even in a test environment.
Handling Custom Dropdown Components
Many modern frameworks (like React, Angular, or Vue) use custom dropdown components that don’t use native
Here’s an example with a custom dropdown built using div and li elements:
<div class="dropdown"> <div class="dropdown-trigger">Select a country</div> <ul class="dropdown-options"> <li>USA</li> <li>Canada</li> <li>Australia</li> </ul> </div>
Here’s how to interact with this type of custom dropdown in Cypress:
it('should select an option from a custom dropdown', () => { cy.get('.dropdown-trigger').click(); // Open the custom dropdown cy.contains('.dropdown-options li', 'Canada').click(); // Select the option });
Best Practices for Handling Dynamic Dropdowns in Cypress
Use Proper Selectors: Always use specific selectors to avoid flaky tests. Prefer data-* attributes or IDs over generic class selectors.
Handle Delays and Dynamic Content: Cypress automatically waits for elements to appear, but you may still need to use .should() or cy.wait() for AJAX-based dropdowns.
Mock API Responses: Use cy.intercept() to mock API calls when testing dropdowns populated by dynamic data.
Check Dropdown State: Ensure you verify both the closed and open states of the dropdown, especially when dealing with custom components.
Avoid Hard-Coding Delays: Instead of using cy.wait(time), leverage cy.intercept() and cy.wait() for API responses to ensure that tests wait for the actual data rather than arbitrary timeouts.
Conclusion
Handling dynamic dropdowns in Cypress doesn’t have to be complicated. With Cypress’s built-in commands like cy.get(), cy.select(), and cy.intercept(), you can easily interact with both native and custom dropdowns, regardless of whether the content is rendered dynamically. By following best practices and using appropriate selectors and waits, you can make your tests more robust, reliable, and maintainable.
Try out these techniques in your Cypress tests to handle dynamic dropdowns effortlessly!
以上是如何处理 Cypress 中的动态下拉菜单的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

从C/C 转向JavaScript需要适应动态类型、垃圾回收和异步编程等特点。1)C/C 是静态类型语言,需手动管理内存,而JavaScript是动态类型,垃圾回收自动处理。2)C/C 需编译成机器码,JavaScript则为解释型语言。3)JavaScript引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

Python和JavaScript在开发环境上的选择都很重要。1)Python的开发环境包括PyCharm、JupyterNotebook和Anaconda,适合数据科学和快速原型开发。2)JavaScript的开发环境包括Node.js、VSCode和Webpack,适用于前端和后端开发。根据项目需求选择合适的工具可以提高开发效率和项目成功率。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。
