TestNG – 13 || Use of @BeforeSuite & @AfterSuite.

Methods defined under @BeforeSuite & @AfterSuite get execute before and after all tests defined in a TestSuite.xml file.

The better way to understand this by comparing the process of @BeforeTest and @BeforeSuite. Look at below images.

Comparison between @BeforeTest and @BeforeSuite annotations.
@BeforeTest V/S @BeforeSuite

In both programs, we have used two tests(Test-1 and Test-2) in the TestSuite.xml file. The only difference what we can see in Class_Base file. As in the left side program we have used @BeforeTest annotation. And in the right side program, we have used @BeforeSuite annotation.

The actual difference would be observed in the Console Result section. @BeforeTest & @AfterTest method will be invoked only for the Test-1 as it was defined in Class_Base.

On the other hand, @BeforeSuite & AfterSuite will be invoked for both Tests(Test-1 and Test-2) even it was defined only in Class_Base.

To learn more about @BeforeTest and @AfterTest annotations, please visit below link:

@BeforeSuite & @AfterSuite

For better understanding let’s move into the coding structure for these annotations. For this we have created 3 class files with few methods as mentioned below:

  • BrowserHandling.java
    • @BeforeSuite
      • LaunchBrowser()
      • login()
    • @AfterSuite
      • logout()
      • closeBrowser()
  • MyOrders.java
    • @Test
      • viewOrders()
      • placeOrder()
  • MyProfile.java
    • @Test
      • VerifyProfile()
      • editProfile()
      • closeProfile()

Note: In file BrowserHandling.java, we have also written code for @BeforeTest and @AfterTest with comments. To see the working of these annotations, just remove the comment from these statements. Execute the program and observe the output in the console.

Also, attribute “dependsOnMethods” have been only used to maintain the sequence of the test methods.

BrowserHandling.java

package beforeaftersuite;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

/**
 * @author ashok.kumar
 *
 */
public class BrowserHandling {
	
	//@BeforeTest
	@BeforeSuite
	public void launchBrowser()
	{
		System.out.println("Browser Launched Successfully.");
	}
	
	//@BeforeTest
	@BeforeSuite(dependsOnMethods="launchBrowser")
	public void login()
	{
		System.out.println("User logged-in.");
	}
	
	//@AfterTest
	@AfterSuite
	public void logout()
	{
		System.out.println("User logged out.");
	}
	
	//@AfterTest
	@AfterSuite(dependsOnMethods = "logout")
	public void closeBrowser()
	{
		System.out.println("Browser Closed Successfully.");
	}
}

MyOrders.java

package beforeaftersuite;

import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 *
 */
public class MyOrders {

	@Test
	public void viewOrders()
	{
		System.out.println("Orders page opened successfully.");
	}
	
	@Test
	public void placeOrder()
	{
		System.out.println("Order placed.");
	}
}

MyProfile.java

package beforeaftersuite;

import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 *
 */
public class MyProfile {
	
	@Test
	public void VerifyProfile()
	{
		System.out.println("My Profile link is working.");
	}
	
	@Test
	public void editProfile()
	{
		System.out.println("Profile edited and updated successfully.");
	}
	
	@Test(dependsOnMethods = "editProfile")
	public void closeProfile()
	{
		System.out.println("Profile page closed.");
		
	}
}

BeforeAfterSuite.xml

In the xml file we have used two <test> section. The name attribute defines the name of each Test in this suite.

Test1 contains two classes in which “BrowserHandling.java” contains methods having @BeforeTest or @BeforeSuite annotations. Where in the Test2 we have used only one class using only @Test annotations.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="BeforeAfterSuite">

<test name="Test1">
<classes>
	<class name="beforeaftersuite.BrowserHandling"/>
	<class name="beforeaftersuite.MyProfile"/>
</classes>
</test>

<test name="Test2">
<classes>
	<class name="beforeaftersuite.MyOrders"></class>
</classes>
</test>

</suite>

Result Analysis

Console output would be like:

Snapshot of Chronological View in index.html file.

Snapshot of Chronological View in index.html file.

Snapshot of emailable-report.html file

This shows only the methods defined under @Test annotations only.

Snapshot of emailable-report.html file

Related Links:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


599total visits,4visits today

Leave a Reply

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