0
0
Selenium Javatesting~8 mins

TestNG annotations (@Test, @BeforeMethod, @AfterMethod) in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - TestNG annotations (@Test, @BeforeMethod, @AfterMethod)
Folder Structure
selenium-testng-project/
├── src/
│   └── test/
│       └── java/
│           ├── tests/
│           │   └── LoginTest.java
│           ├── pages/
│           │   └── LoginPage.java
│           ├── utils/
│           │   └── WebDriverFactory.java
│           └── config/
│               └── TestConfig.java
├── testng.xml
└── pom.xml
  
Test Framework Layers
  • Test Layer: Contains test classes with @Test methods. Example: LoginTest.java where tests run and assertions happen.
  • Setup/Teardown Layer: Uses @BeforeMethod and @AfterMethod annotations to prepare and clean up before and after each test method. For example, opening and closing browser sessions.
  • Page Object Layer: Classes representing web pages with methods to interact with UI elements. Example: LoginPage.java encapsulates login form actions.
  • Utility Layer: Helper classes like WebDriverFactory.java to create and manage WebDriver instances.
  • Configuration Layer: Holds environment settings, browser types, and credentials in classes like TestConfig.java or external files.
Configuration Patterns

Use testng.xml to define test suites and parameters like browser type or environment.

Example snippet in testng.xml:

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

In test classes, use @Parameters to receive these values and configure WebDriver accordingly.

Store sensitive data like credentials securely, e.g., environment variables or encrypted files, and load them in TestConfig.java.

Test Reporting and CI/CD Integration
  • TestNG generates HTML and XML reports automatically after test runs.
  • Use listeners like ITestListener to customize reports or capture screenshots on failures.
  • Integrate with CI/CD tools (Jenkins, GitHub Actions) to run tests on code commits and publish reports.
  • Configure build tools (Maven/Gradle) to run TestNG tests and fail builds on test failures.
Best Practices
  1. Use @BeforeMethod and @AfterMethod for clean setup and teardown: This ensures each test runs independently with a fresh browser state.
  2. Keep tests atomic: Each @Test method should test one thing to make failures easy to diagnose.
  3. Use Page Object Model: Encapsulate UI interactions to keep tests readable and maintainable.
  4. Parameterize tests: Use @Parameters and testng.xml to run tests on different browsers or environments without code changes.
  5. Handle waits explicitly: Avoid flaky tests by using explicit waits instead of fixed sleeps.
Self Check

Where in this folder structure would you add a new method to open the browser before each test?

Answer: In the test class under src/test/java/tests/, inside a method annotated with @BeforeMethod.

Key Result
Use TestNG annotations @Test, @BeforeMethod, and @AfterMethod to organize test execution with setup and cleanup steps.