Cypress で動的ドロップダウンを処理する方法

Mary-Kate Olsen
リリース: 2024-09-27 22:41:03
オリジナル
358 人が閲覧しました

How to Handle Dynamic Dropdown in Cypress

導入

動的なドロップダウンの処理は、特にドロップダウン オプションが API から動的にフェッチされる場合、またはユーザー インタラクションに基づいて読み込まれる場合に、最新の Web アプリケーションでよくある課題です。 Cypress を使用してこのようなドロップダウンのテストを自動化する場合は、たとえ遅延が発生してからレンダリングされるとしても、適切なオプションが選択されていることを確認する必要があります。

このブログでは、Cypress で動的ドロップダウンを操作するプロセスを説明し、API 応答によって設定されるドロップダウンやユーザー入力に基づいて変化するドロップダウンなどの一般的なシナリオの例を示します。

動的ドロップダウンが難しいのはなぜですか?

動的ドロップダウンは、次の理由からテストの課題を引き起こすことがよくあります。

  • オプションは最初は存在しません: ドロップダウン オプションは、ユーザー アクションまたは API 呼び出しの後に非同期で読み込まれる可能性があります。
  • ドロップダウン コンテンツの変更: ユーザー入力または操作に基づいて、ドロップダウン オプションは動的に変更される場合があります。
  • DOM の更新: Cypress は、ドロップダウンを操作する前に、DOM が更新されるのを待つ必要があります。

サイプレスは、これらの課題に対処するためのいくつかの強力なコマンドを提供し、動的なドロップダウンから適切なオプションを確実に選択できるようにします。

動的ドロップダウンを処理するためのステップバイステップ ガイド

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
});
ログイン後にコピー

ドロップダウンでネイティブの elements. These components often have their own structure and require custom handling.

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

  1. Use Proper Selectors: Always use specific selectors to avoid flaky tests. Prefer data-* attributes or IDs over generic class selectors.

  2. 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.

  3. Mock API Responses: Use cy.intercept() to mock API calls when testing dropdowns populated by dynamic data.

  4. Check Dropdown State: Ensure you verify both the closed and open states of the dropdown, especially when dealing with custom components.

  5. 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 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!