Skip to toolbar

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;
	
	@BeforeClass
	public void launchAndLogin() throws InterruptedException
	{
		System.out.println("Pre-condition:");
		System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
		driver=new ChromeDriver();
		driver.get("http://www.allinoneblogs.com");
		driver.manage().window().maximize();
		
		//Please comment below code if there is no Subscribe pop-up box appears.
		driver.findElement(By.xpath("//*[@id=\"popup_box_close_1484\"]")).click();
		System.out.println("Browser and URL Launched.");
		System.out.println("---------------------------------------------------------------");
	}
	
	@Test(priority=1)
	public void checkProfile()
	{
		Actions action= new Actions(driver);
		WebElement profileElement=driver.findElement(By.xpath("//*[@id=\"text-5\"]/div/p[2]/a[1]"));
		action.moveToElement(profileElement);
		action.click();
		action.perform();
		System.out.println("Profile Page opened.");
	}
	
	@Test(priority=2)
	public void getBlogsCount()
	{
		List<WebElement> blogElements=driver.findElements(By.xpath("//*[@id=\"lps-c64a26c4197944c0a82ce287589aa330\"]/article[1]/a/h3"));
		System.out.println("Blogs Count: "+blogElements.size());
	}
	
	@Test(priority=3)
	public void searchBlog() throws InterruptedException
	{
		Thread.sleep(1000);
		String searchKey="TestNG";
		driver.findElement(By.xpath("//*[@id=\"search-3\"]/form/label/input")).sendKeys(searchKey);
		driver.findElement(By.xpath("//*[@id=\"search-3\"]/form/label/input")).sendKeys(Keys.ENTER);
		System.out.println("Search Result Displayed.");
		
	}

	@AfterClass
	public void closeBrowser() throws InterruptedException
	{
		System.out.println("---------------------------------------------------------------");
		System.out.println("Post-condition:");
		// Paused execution for a second, to observe the progress.
		Thread.sleep(1000);
		driver.quit();
		System.out.println("Browser Closed.\n");
	}
}

Related Links:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Excel:


Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 3
  • Page views today : 4
  • Total visitors : 49,367
  • Total page view: 67,963

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.