Showing posts with label Count Elements in Playwright. Show all posts
Showing posts with label Count Elements in Playwright. Show all posts

Count Multiple Elements in Dropdown in Playwright

















Dropdown in Playwright: How to Count Multiple Elements in a Dropdown

Dropdowns are one of the most common UI elements in modern web applications. Whether you are selecting a country, choosing a payment method, filtering products, or selecting user roles, dropdowns play a crucial role in user interaction.

When working with Playwright, automation engineers often need not only to select values from dropdowns but also to count how many options are available inside them. This becomes especially important in validation testing, dynamic content verification, and data-driven automation.

In this detailed, SEO-optimized, AdSense-friendly guide, you will learn:

  • What is a dropdown in Playwright?

  • Types of dropdowns in web applications

  • How to locate dropdown elements

  • How to count multiple elements in a dropdown

  • Real-world testing use cases

  • Complete Java code example

  • Best practices for handling dropdowns in Playwright

This article is written in a clear, human-like style so that beginners and experienced automation testers can understand and implement it easily.


What is a Dropdown in Playwright?

A dropdown is typically represented in HTML using:

<select> <option>Option 1</option> <option>Option 2</option> </select>

However, modern web applications often use custom dropdowns built with:

  • <div>

  • <li>

  • <ul>

  • <span>

From Playwright’s perspective, a dropdown is simply a collection of elements that can be located and interacted with using Locators.


Types of Dropdowns in Web Applications

Understanding dropdown types is important before automating them.

1. Standard HTML Dropdown (<select>)

  • Uses <select> and <option> tags

  • Easy to automate

  • Supports built-in selection methods

2. Custom Dropdown

  • Built using <div> or <li>

  • Often styled with JavaScript frameworks

  • Requires clicking to expand before selecting options

3. Dynamic Dropdown

  • Options change based on user input

  • Example: Selecting country loads states dynamically

Each type requires slightly different automation strategies.


Why Count Dropdown Options in Automation Testing?

Many beginners only focus on selecting dropdown values. However, counting dropdown options is equally important.

Here’s why:

1. Validation Testing

Ensure the dropdown contains the expected number of items.

Example:

  • Country dropdown should contain 195 countries.

  • Month dropdown should contain 12 months.


2. Dynamic Dropdown Verification

If selecting “India” loads 28 states, your automation must verify:

  • Correct number of states loaded

  • No missing or duplicate entries


3. Data Integrity Testing

Ensure:

  • No duplicate options exist

  • No blank values are present

  • Required options are available


4. Regression Testing

When UI changes occur, dropdown options may accidentally be removed or duplicated. Counting ensures UI consistency.


Steps to Count Multiple Elements in a Dropdown Using Playwright

Let’s break it down into simple steps.


Step 1: Locate the Dropdown

First, identify the dropdown element using a Locator.

Example:

page.locator("#dropdown-class-example");


Step 2: Identify Dropdown Options

For <select> dropdowns:

  • Options are <option> elements.

For custom dropdowns:

  • Options may be <li> or <div> elements.


Step 3: Retrieve All Matching Elements

Use Playwright’s locator to capture all child elements.


Step 4: Count the Elements

Use:

locator.count();

This returns the total number of matching elements.


Step 5: Validate and Use in Test

You can:

  • Assert expected count

  • Print each option text

  • Validate dropdown data


Example Website for Practice

We will use:

Rahul Shetty Academy Automation Practice

URL:
https://rahulshettyacademy.com/AutomationPractice/

This site contains a sample dropdown perfect for automation practice.


Complete Java Code Example: Count Dropdown Options

import com.microsoft.playwright.*; public class DropdownCountExample { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium() .launch(new BrowserType.LaunchOptions().setHeadless(false)); BrowserContext context = browser.newContext(); Page page = context.newPage(); // Navigate to demo site page.navigate("https://rahulshettyacademy.com/AutomationPractice/"); // Locate dropdown options Locator dropdownOptions = page.locator("#dropdown-class-example option"); // Count options int optionCount = dropdownOptions.count(); System.out.println("Total options in dropdown: " + optionCount); // Print each option text for (int i = 0; i < optionCount; i++) { System.out.println("Option " + (i + 1) + ": " + dropdownOptions.nth(i).textContent()); } browser.close(); } } }


Detailed Code Explanation

Let’s understand this step-by-step.


1. Launch the Browser

Browser browser = playwright.chromium() .launch(new BrowserType.LaunchOptions().setHeadless(false));
  • Launches Chromium browser

  • Headless false allows visual execution


2. Create BrowserContext

BrowserContext context = browser.newContext();

Creates a fresh session.


3. Navigate to Website

page.navigate("https://rahulshettyacademy.com/AutomationPractice/");

Opens practice website.


4. Locate Dropdown Options

Locator dropdownOptions = page.locator("#dropdown-class-example option");

Important correction:

We use option to target individual dropdown options.


5. Count Elements

int optionCount = dropdownOptions.count();

Returns total number of <option> elements.


6. Print Each Option

dropdownOptions.nth(i).textContent();

Fetches text of each dropdown option.


Handling Custom Dropdowns

For custom dropdowns:

Example HTML:

<ul> <li>Option 1</li> <li>Option 2</li> </ul>

You must:

  1. Click dropdown to expand

  2. Locate <li> elements

  3. Count them

Example:

page.locator("#customDropdown").click(); Locator options = page.locator("//ul/li"); int count = options.count();


Validating Dropdown Count with Assertion (TestNG Example)

If using TestNG:

Assert.assertEquals(optionCount, 3, "Dropdown count mismatch!");

This ensures expected number of options.


Real-World Automation Scenarios

E-Commerce Platform

  • Verify category dropdown contains expected product categories.

  • Ensure no duplicate category entries.


Banking Application

  • Validate account type dropdown contains:

    • Savings

    • Current

    • Fixed Deposit


Government Portal

  • Verify state list updates based on country selection.


Best Practices for Dropdown Automation

  1. Always target specific child elements (like option).

  2. Avoid using overly generic locators.

  3. Validate both count and content.

  4. Handle dynamic loading properly.

  5. Wait for dropdown to be visible before counting.

  6. Use Page Object Model for better maintainability.


Common Mistakes to Avoid

  • Counting the dropdown container instead of options.

  • Not expanding custom dropdown before counting.

  • Ignoring dynamic content loading.

  • Not validating option text.


Advantages of Using Playwright for Dropdown Handling

  • Powerful Locator API

  • Built-in waiting mechanisms

  • Supports dynamic content

  • Cross-browser testing capability

  • Fast execution


SEO Keywords Covered

  • Dropdown in Playwright

  • Count dropdown options in Playwright Java

  • Playwright locator count method

  • Handle dropdown in Playwright

  • Playwright automation tutorial

  • Playwright Java example

  • Count multiple elements Playwright

  • Dropdown validation in automation


Suggested Posts:

1. Handle IFrames in Playwright
2. Automate Login Page in Playwright
3. Comma Selectors in Playwright
4. Handle Alerts in Playwright
5. Page Object Model in Playwright