0
0
Selenium Javatesting~8 mins

Dependency between tests in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Dependency between tests
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   └── LoginPage.java
                ├── tests/
                │   ├── LoginTests.java
                │   └── DashboardTests.java
                ├── utils/
                │   ├── WebDriverFactory.java
                │   └── TestDataLoader.java
                └── config/
                    └── TestConfig.java
Test Framework Layers
  • Driver Layer: Manages WebDriver instances (e.g., WebDriverFactory.java).
  • Page Objects: Encapsulate page elements and actions (e.g., LoginPage.java).
  • Test Layer: Contains test classes with test methods (e.g., LoginTests.java).
  • Utilities: Helper classes for data loading, waits, logging (e.g., TestDataLoader.java).
  • Configuration: Holds environment and test settings (e.g., TestConfig.java).

Dependency between tests: Use TestNG's dependsOnMethods or dependsOnGroups annotations to specify test order and dependencies.

Configuration Patterns
  • Environment Settings: Use TestConfig.java or property files to define URLs, credentials, and browser types.
  • Browser Selection: Pass browser type as a parameter or system property to WebDriverFactory.
  • Credentials: Store securely in environment variables or encrypted files, accessed via config classes.
  • Test Dependencies: Define dependencies in TestNG XML or via annotations in test classes.
Test Reporting and CI/CD Integration
  • Use TestNG built-in reports for pass/fail status and detailed logs.
  • Integrate with Allure or ExtentReports for enhanced visual reports.
  • Configure CI tools (Jenkins, GitHub Actions) to run tests and publish reports automatically.
  • Fail dependent tests if their prerequisite tests fail, ensuring clear failure cause.
Best Practices for Dependency Between Tests
  1. Minimize dependencies: Keep tests independent when possible to avoid cascading failures.
  2. Use TestNG annotations: Use @Test(dependsOnMethods = {"methodName"}) to declare dependencies clearly.
  3. Clear naming: Name tests to reflect their purpose and dependency order.
  4. Fail fast: If a prerequisite test fails, skip dependent tests to save time and avoid misleading results.
  5. Separate setup: Use @BeforeClass or @BeforeMethod for common setup instead of test dependencies when possible.
Self Check

Where in this folder structure would you add a new test method that must run only after a successful login test?

Key Result
Use TestNG's dependsOnMethods annotation to manage test dependencies clearly and reliably.