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, annotations like ‘@BeforeSuite‘ and ‘@AfterSuite‘ are used to define methods that should run before and after the entire suite of tests, respectively. These annotations are useful for setting up and tearing down global resources required by all tests within the suite.

@'BeforeSuite'

The ‘@BeforeSuite‘ annotation is used to specify a method that should run before all the tests in the suite. This method typically contains code for setting up global configurations, initializing resources, or any other setup tasks that need to be completed once before all tests run.

Example

				
					import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class BeforeSuiteExample {

    @BeforeSuite
    public void beforeSuiteSetup() {
        System.out.println("Setting up resources before the test suite execution");
        // Setup code, e.g., initializing WebDriver, loading configurations, etc.
    }

    @Test
    public void test1() {
        System.out.println("Test 1 executed");
    }

    @Test
    public void test2() {
        System.out.println("Test 2 executed");
    }
}

				
			

@'AfterSuite'

The ‘@AfterSuite‘ annotation is used to specify a method that should run after all the tests in the suite have finished. This method typically contains code for tearing down global resources, closing connections, or any other cleanup tasks that need to be performed once after all tests run.

Example

				
					import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

public class AfterSuiteExample {

    @AfterSuite
    public void afterSuiteTeardown() {
        System.out.println("Cleaning up resources after the test suite execution");
        // Cleanup code, e.g., quitting WebDriver, releasing resources, etc.
    }

    @Test
    public void test1() {
        System.out.println("Test 1 executed");
    }

    @Test
    public void test2() {
        System.out.println("Test 2 executed");
    }
}

				
			

Combining '@BeforeSuite' and '@AfterSuite'

In a real-world scenario, you would often use both annotations in the same test class to manage resources before and after the test suite runs. Here’s an example:

				
					import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

public class TestSuiteExample {

    @BeforeSuite
    public void beforeSuiteSetup() {
        System.out.println("Setting up resources before the test suite execution");
        // Initialize WebDriver, set up database connections, load configuration files, etc.
    }

    @AfterSuite
    public void afterSuiteTeardown() {
        System.out.println("Cleaning up resources after the test suite execution");
        // Quit WebDriver, close database connections, clear temporary files, etc.
    }

    @Test
    public void test1() {
        System.out.println("Test 1 executed");
        // Test code
    }

    @Test
    public void test2() {
        System.out.println("Test 2 executed");
        // Test code
    }
}

				
			

Practical Example with Selenium WebDriver

Here’s a practical example where ‘@BeforeSuite‘ initializes a WebDriver instance and ‘@AfterSuite‘ closes the browser after all tests are done:

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

public class SeleniumTestSuiteExample {

    WebDriver driver;

    @BeforeSuite
    public void setup() {
        System.out.println("Initializing WebDriver");
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

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

    @Test
    public void test1() {
        System.out.println("Executing Test 1");
        driver.get("https://example.com");
        // Perform test steps
    }

    @Test
    public void test2() {
        System.out.println("Executing Test 2");
        driver.get("https://example.com/page");
        // Perform test steps
    }
}

				
			

In this example, ‘@BeforeSuite‘ sets up the WebDriver once before any tests run, and ‘@AfterSuite‘ ensures the WebDriver is closed after all tests have completed, ensuring efficient resource management.

Scroll to Top