0
0
Selenium Javatesting~8 mins

findElement by linkText in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - findElement by linkText
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   └── HomePage.java
                ├── tests/
                │   └── HomePageTest.java
                ├── utils/
                │   └── WebDriverFactory.java
                └── config/
                    └── ConfigReader.java
    
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., WebDriverFactory).
  • Page Objects: Classes representing web pages with methods using findElement(By.linkText()) to locate links by their visible text.
  • Tests: Test classes that use page objects to perform actions and assertions.
  • Utilities: Helper classes for common tasks like waits, logging, or data handling.
  • Configuration: Reads environment settings, browser types, and credentials from files.
Configuration Patterns

Use a properties file (e.g., config.properties) to store environment URLs, browser types, and credentials.

Load these settings in ConfigReader class to provide centralized access.

Example properties:

url=https://example.com
browser=chrome
username=testuser
password=testpass
    

Use system properties or environment variables to override defaults for CI/CD pipelines.

Test Reporting and CI/CD Integration
  • Use TestNG for running tests and generating HTML reports.
  • Integrate with CI tools like Jenkins or GitHub Actions to run tests on code commits.
  • Configure reports to show pass/fail status and screenshots on failure.
  • Use logs to trace findElement(By.linkText()) calls for debugging.
Framework Design Best Practices
  1. Use Page Object Model: Encapsulate findElement(By.linkText()) inside page classes to keep tests clean.
  2. Explicit Waits: Use WebDriverWait to wait for link elements to be clickable before interacting.
  3. Readable Link Text: Use exact visible text for linkText locator to avoid brittle tests.
  4. Centralize Configuration: Manage URLs and credentials in config files, not hardcoded in tests.
  5. Clear Reporting: Capture screenshots and logs when link elements are not found to help debugging.
Self Check

Where in this folder structure would you add a new page object class for a page that contains links you want to locate by their visible text?

Key Result
Use Page Object Model with explicit waits and centralized config to locate links by visible text using findElement(By.linkText()).