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:


112total visits,5visits today

Leave a Reply

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