0
0
Selenium Javatesting~8 mins

findElements for multiple matches in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - findElements for multiple matches
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   └── SearchResultsPage.java
                ├── tests/
                │   └── SearchTests.java
                ├── utils/
                │   └── WebDriverFactory.java
                └── config/
                    └── TestConfig.java
  
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., WebDriverFactory.java).
  • Page Objects: Classes representing web pages, encapsulating element locators and actions. For example, SearchResultsPage.java uses findElements to get multiple matching elements.
  • Tests: Test classes that use page objects to perform test scenarios, e.g., SearchTests.java.
  • Utilities: Helper classes for common functions like waits, logging, or data handling.
  • Configuration: Holds environment settings, browser types, URLs, and credentials.
Configuration Patterns

Use a TestConfig.java class or property files to manage:

  • Environment URLs (dev, staging, production)
  • Browser types (Chrome, Firefox, Edge)
  • Timeouts and waits
  • Credentials for login if needed

Load these settings before tests run, so tests and page objects use the right environment and browser.

Test Reporting and CI/CD Integration
  • Use TestNG or JUnit reports to show pass/fail results clearly.
  • Integrate with CI tools like Jenkins or GitHub Actions to run tests automatically on code changes.
  • Generate HTML or XML reports for easy review.
  • Include screenshots on failure for debugging.
Best Practices
  • Use findElements to get all matching elements: It returns a list, even if empty, so avoid exceptions when no matches.
  • Encapsulate element lists in page objects: Keep locators and element retrieval in one place for easy maintenance.
  • Use explicit waits before calling findElements: Ensure elements are present to avoid flaky tests.
  • Check list size before accessing elements: Prevent IndexOutOfBoundsException by verifying the list is not empty.
  • Write clear assertions on list contents: For example, assert the number of elements or their text values.
Self Check

Where in this folder structure would you add a new page object class to handle a page that shows multiple search results?

Key Result
Use page objects to encapsulate multiple element matches with findElements, supported by explicit waits and clear assertions.