TestNG – 12 || Use of @BeforeGroups & @AfterGroups.

Executing specific methods before and after of a test group is possible using @BeforeGroups and @AfterGroups annotations.

We have learned that using groups attribute we can create groups of test methods from multiple classes. I would recommend please go through with the previous chapter if want to learn implementation of groups attributes.

Use of @BeforeGroup and @AfterGroup Annotations.

Above example is the modified version of the program we have written in the previous chapter. Here we can see that we have defined two groups “Alpha” and “Regression” with specific methods from different classes(myOrders.java & myProfile.java).

Now, we have added one more class “BrowserHandling.java” with @BeforeGroups and @AfterGroups annotations and associated it with the existing groups. The XML suite file will remain same, we have to just add a new class in the <class> tag.

@BeforeGroups and @AfterGroups

For a better understanding of the whole concept, I am rewriting the code for all the files again here.

BrowserHandling.JAVA

package groupExample;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
/**
 * @author ashok.kumar
 *
 */
public class BrowserHandling {
	
	@BeforeGroups(groups= {"Alpha","Regression"})
	public void launchLogin()
	{
		System.out.println("Browser is launched.");
	}
	@AfterGroups(groups="Alpha")
	public void closeBrowser()
	{
		System.out.println("Browser is closed.");
	}
}

MyOrders.JAVA

package groupExample;
import org.testng.annotations.Test;
/**
 * @author ashok.kumar
 *
 */
public class MyOrders {
	
	@Test(groups= {"Regression","Alpha"})
	public void VerifyOrders()
	{
	   System.out.println("Orders buttons is working.");	
	}
	
	@Test(groups="Alpha")
	public void viewOrders()
	{
		System.out.println("View you all orders.");
	}
}

MyProfile.JAVA

package groupExample;
import org.testng.annotations.Test;
/**
 * @author ashok.kumar
 *
 */
public class MyProfile {
	
	@Test(groups="Alpha")
	public void verifyProfile()
	{
		System.out.println("MyProfile button is active.");
	}
	
	@Test(groups= {"Alpha","Regression"})
	public void viewProfile()
	{
		System.out.println("View MyProfile Page.");
	}
	
	@Test(groups="Alpha")
	public void updateProfile()
	{
		System.out.println("Profile updated successfully.");
	}
}

beforeAfterGoups_Suite.XML

<?xml version="1.0" encoding="UTF-8"?>
<suite name="beforeAfterGoups_Suite">
	<test name="Test1">
		<groups>
			<run>
				<include name="Regression"/>
			</run>
		</groups>
	
	<classes>
		<class name="groupExample.BrowserHandling"/>
		<class name="groupExample.MyOrders"/>
		<class name="groupExample.MyProfile"/>
	</classes>s
	</test>
</suite>

Result Analysis

Result in index.html format for Groups=”Regression”.

Result in index.html format for Groups="Regression".

Result in index.html format for Groups=”Alpha”.

Result in index.html format for Groups="Alpha".

Related Links:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


368total visits,3visits today

Leave a Reply

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