Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

TestNG is a popular testing framework in the Java ecosystem, especially for Selenium test automation. Two important annotations in TestNG are ‘@BeforeMethod‘ and ‘@AfterMethod‘, which help in setting up preconditions and postconditions for test methods.

@BeforeMethod

The ‘@BeforeMethod‘ annotation is used to specify a method that should be executed before each test method in the current class. This is useful for setting up common preconditions for tests, such as initializing the WebDriver, opening a browser, and navigating to a specific URL.

@AfterMethod

The ‘@AfterMethod‘ annotation is used to specify a method that should be executed after each test method in the current class. This is typically used for clean-up activities, such as closing the browser, logging out, or resetting test data.

Example

Below is an example demonstrating the usage of ‘@BeforeMethod‘ and ‘@AfterMethod‘ annotations in a Selenium test using TestNG.

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ExampleTest {
    WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Set up the WebDriver and initialize the browser
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        
        // Navigate to the desired URL
        driver.get("https://www.example.com");
    }

    @Test
    public void testExample() {
        // Your test code here
        System.out.println("Executing testExample");
    }

    @Test
    public void anotherTest() {
        // Another test code here
        System.out.println("Executing anotherTest");
    }

    @AfterMethod
    public void tearDown() {
        // Clean up the WebDriver and close the browser
        if (driver != null) {
            driver.quit();
        }
    }
}

				
			

Explanation

  1. WebDriver Setup (‘@BeforeMethod‘):

    • The ‘setUp‘ method is annotated with ‘@BeforeMethod‘, which means it will run before each test method in the class.
    • It sets up the WebDriver, initializes a Chrome browser instance, and navigates to “https://www.example.com“.
  2. Test Methods:

    • testExample‘ and ‘anotherTest‘ are test methods annotated with ‘@Test‘.
    • Each test method contains the code for the specific test scenario.
  3. WebDriver Cleanup (‘@AfterMethod‘):

    • The ‘tearDown‘ method is annotated with ‘@AfterMethod‘, which means it will run after each test method in the class.
    • It checks if the WebDriver instance is not null and then closes the browser using ‘driver.quit()‘.

Benefits

  • Isolation: Ensures each test method starts with a clean slate by setting up the browser environment before each test and cleaning it up afterward.
  • Reusability: Common setup and teardown code is centralized, reducing duplication and making maintenance easier.
  • Consistency: Provides a consistent environment for each test run, reducing the likelihood of test flakiness due to shared state between tests.

Using ‘@BeforeMethod‘ and ‘@AfterMethod‘ annotations in TestNG helps in writing clean, maintainable, and reliable test automation scripts.

Scroll to Top