0
0
Selenium Javatesting~15 mins

findElements for multiple matches in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify multiple product items are displayed on the search results page
Preconditions (1)
Step 1: Enter 'laptop' in the search input field with id 'search-box'
Step 2: Click the search button with id 'search-button'
Step 3: Wait for the search results page to load
Step 4: Locate all product items displayed with the class name 'product-item'
✅ Expected Result: At least 3 product items are displayed on the search results page
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the number of elements found with class 'product-item' is 3 or more
Best Practices:
Use explicit waits to wait for search results to load
Use By.className locator for multiple elements
Avoid hardcoded Thread.sleep
Use assertions from a testing framework like TestNG or JUnit
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.List;

public class SearchResultsTest {
    WebDriver driver;
    WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver executable if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.manage().window().maximize();
    }

    @Test
    public void testMultipleProductItemsDisplayed() {
        driver.get("https://example-ecommerce.com");

        // Enter 'laptop' in search box
        WebElement searchBox = wait.until(ExpectedConditions.elementToBeClickable(By.id("search-box")));
        searchBox.sendKeys("laptop");

        // Click search button
        WebElement searchButton = driver.findElement(By.id("search-button"));
        searchButton.click();

        // Wait for product items to be visible
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("product-item")));

        // Find all product items
        List<WebElement> productItems = driver.findElements(By.className("product-item"));

        // Assert at least 3 product items are displayed
        Assert.assertTrue(productItems.size() >= 3, "Expected at least 3 product items, but found " + productItems.size());
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java and TestNG for assertions.

Setup: The @BeforeClass method initializes the ChromeDriver and WebDriverWait for explicit waits.

Test steps: The test navigates to the homepage, enters 'laptop' in the search box identified by id='search-box', clicks the search button id='search-button', then waits explicitly until at least one product item with class product-item is visible.

It then collects all elements with class product-item using findElements, which returns a list of matching elements.

Assertion: The test asserts that the list size is 3 or more, ensuring multiple products are displayed.

Teardown: The @AfterClass method closes the browser.

This approach uses explicit waits to avoid flaky tests and uses proper locators and assertions for reliability.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using findElement instead of findElements to get multiple elements
Using hardcoded XPath that is brittle
Not checking the number of elements found before asserting
Bonus Challenge

Now add data-driven testing with 3 different search keywords: 'laptop', 'smartphone', 'headphones'

Show Hint