TestNG – 7 || Use of @BeforeClass and @AfterClass.

BeforeClass and AfterClass annotations are like defining pre-condition and post-conditions in a Program.

As in the last chapter, we have read about the BeforeMethod and AfterMethod annotations which runs before and after for each test method.

But there are few scenarios where we have to execute any specific method at the starting or end of the programs. Suppose, there are some pre-conditions (like initializing global variables, launch browser, log in, etc.) which needs to be executed at the beginning of the program. And some post-conditions (like closing browser, log out, etc.) needs to be executed at the end of the program.

BeforeClass and AfterClass Annotation

BeforeClass and AfterClass

All the pre-conditions can be defined under BeforeClass annotation. And all the post-conditions can be defined under AfterClass annotation.

  • If we took the example from the last chapter using BeforeMethod and AfterMethod, program execution flow was like:
    • Test Case 1:- Launch the browser and URL > Check Profile of the author > Close browser
    • Test Case 2:- Launch the browser and URL > Get blogs count on the page > Close browser
    • Test Case 3:- Launch the browser and URL > Validate Search Functionality > Close browser
  • Now if we change these annotations with BeforeClass and AfterClass, program execution flow would be like:
    • Launch the browser and URL
    • Test Case 1:- Check Profile of the author
    • Test Case 2:- Get blogs count on the page
    • Test Case 3:- Validate Search Functionality
    • Close Browser

Sample Code

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
 * @author ashok.kumar
public class BeforeClass_AfterClass 
	WebDriver driver;
	public void launchAndLogin() throws InterruptedException
		driver=new ChromeDriver();
		//Please comment below code if there is no Subscribe pop-up box appears.
		System.out.println("Browser and URL Launched.");
	public void checkProfile()
		Actions action= new Actions(driver);
		WebElement profileElement=driver.findElement(By.xpath("//*[@id=\"text-5\"]/div/p[2]/a[1]"));
		System.out.println("Profile Page opened.");
	public void getBlogsCount()
		List<WebElement> blogElements=driver.findElements(By.xpath("//*[@id=\"lps-c64a26c4197944c0a82ce287589aa330\"]/article[1]/a/h3"));
		System.out.println("Blogs Count: "+blogElements.size());
	public void searchBlog() throws InterruptedException
		String searchKey="TestNG";
		System.out.println("Search Result Displayed.");

	public void closeBrowser() throws InterruptedException
		// Paused execution for a second, to observe the progress.
		System.out.println("Browser Closed.\n");

Related Links:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:


2218total visits,6visits today

Leave a Reply

Your email address will not be published. Required fields are marked *