0
0
Selenium Javatesting~8 mins

Test suites (testng.xml) in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Test suites (testng.xml)
Folder Structure
src/
 └── test/
      └── java/
           └── com/
                └── example/
                     ├── pages/
                     │    └── LoginPage.java
                     ├── tests/
                     │    ├── LoginTest.java
                     │    └── DashboardTest.java
                     ├── utils/
                     │    └── WebDriverFactory.java
                     └── config/
                          └── TestConfig.java

resources/
 └── testng.xml
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., WebDriverFactory.java).
  • Page Objects: Classes representing web pages with locators and actions (e.g., LoginPage.java).
  • Test Layer: Test classes containing test methods using TestNG annotations (e.g., LoginTest.java).
  • Utilities: Helper classes for common functions like waits, logging.
  • Configuration: Holds environment settings and test parameters (e.g., TestConfig.java).
  • Test Suite Definition: testng.xml file defines which tests run and their order.
Configuration Patterns

The testng.xml file controls test execution. It allows:

  • Grouping tests into suites and test tags.
  • Specifying test classes and methods to run.
  • Passing parameters like browser type or environment.
  • Running tests in parallel or sequentially.

Example snippet from testng.xml:

<suite name="RegressionSuite" parallel="tests" thread-count="2">
  <test name="LoginTests">
    <parameter name="browser" value="chrome" />
    <classes>
      <class name="com.example.tests.LoginTest" />
    </classes>
  </test>
  <test name="DashboardTests">
    <parameter name="browser" value="firefox" />
    <classes>
      <class name="com.example.tests.DashboardTest" />
    </classes>
  </test>
</suite>

Parameters can be accessed in test classes using @Parameters annotation.

Test Reporting and CI/CD Integration
  • TestNG generates HTML and XML reports automatically after test runs.
  • Reports show passed, failed, and skipped tests with details.
  • CI/CD tools (Jenkins, GitHub Actions) can run testng.xml suites as part of build pipelines.
  • Reports can be archived or published for team visibility.
  • Failures can trigger alerts or rollback steps in CI/CD.
Best Practices
  • Use descriptive suite and test names in testng.xml for clarity.
  • Group related tests logically to run targeted suites (e.g., smoke, regression).
  • Use parameters to avoid hardcoding browser or environment in tests.
  • Keep testng.xml clean and readable by avoiding duplication.
  • Run tests in parallel carefully to speed up but avoid flaky tests.
Self Check

Where in this folder structure would you add a new test class for user registration tests to be included in the test suite?

Key Result
Use testng.xml to organize and configure test suites, enabling flexible and maintainable test execution.