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

In TestNG, @BeforeTest and @AfterTest are annotations used to define methods that should run before and after the execution of any test methods in the specified test tag in the TestNG XML file. These annotations help set up and clean up resources needed for tests.

@BeforeTest

The ‘@BeforeTest‘ annotation is used to identify methods that should run before any test method in the current ‘<test>‘ tag is executed. It is typically used for setting up configurations or initializing resources required for the tests.

Example of @BeforeTest

				
					import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;

public class SampleTest {
    
    @BeforeTest
    public void setUp() {
        System.out.println("Setting up the test environment");
        // Code to set up the test environment, e.g., initialize WebDriver
    }

    @Test
    public void testMethod1() {
        System.out.println("Executing test method 1");
        // Test logic here
    }

    @Test
    public void testMethod2() {
        System.out.println("Executing test method 2");
        // Test logic here
    }

    @AfterTest
    public void tearDown() {
        System.out.println("Cleaning up the test environment");
        // Code to clean up after tests, e.g., close WebDriver
    }
}

				
			

Explanation

  1. @BeforeTest setUp():

    • This method will run before any test methods in the <test> tag of the XML file.
    • It’s used to set up the environment or initialize resources needed for the tests, such as WebDriver initialization in Selenium.
  2. Test Methods:

    • testMethod1 and testMethod2 are the actual test methods.
    • These methods contain the test logic and will run after the @BeforeTest method.
  3. @AfterTest tearDown():

    • This method will run after all the test methods in the <test> tag have been executed.
    • It’s used to clean up the environment or release resources used during the tests, such as closing the WebDriver.

XML Configuration

To see the full effect of ‘@BeforeTest‘ and ‘@AfterTest‘, you should configure the TestNG XML file to define your tests.

				
					<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1">
    <test name="Test1">
        <classes>
            <class name="SampleTest"/>
        </classes>
    </test>
</suite>

				
			

Usage in Selenium

To see the full effect of ‘@BeforeTest‘ and ‘@AfterTest‘, you should configure the TestNG XML file to define your tests.

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;

public class SeleniumTest {
    WebDriver driver;

    @BeforeTest
    public void setUp() {
        System.out.println("Initializing WebDriver");
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://example.com");
    }

    @Test
    public void testMethod1() {
        System.out.println("Executing test method 1");
        // Perform test actions on the web page
    }

    @Test
    public void testMethod2() {
        System.out.println("Executing test method 2");
        // Perform test actions on the web page
    }

    @AfterTest
    public void tearDown() {
        System.out.println("Closing WebDriver");
        if (driver != null) {
            driver.quit();
        }
    }
}

				
			

Explanation for Selenium

  1. @BeforeTest setUp():

    • Initializes the WebDriver before any test method is executed.
    • Maximizes the browser window and navigates to a specific URL.
  2. Test Methods:

    • Contains the logic to interact with the web page.
  3. @AfterTest tearDown():

    • Closes the WebDriver after all the test methods have been executed to ensure proper cleanup.

Using @BeforeTest and @AfterTest helps to ensure that your test environment is properly set up before the tests run and cleaned up afterwards, which is crucial for maintaining a stable and consistent testing process.

Scroll to Top