Skip to toolbar

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:


1205total visits,2visits today

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 2
  • Total visitors : 48,855
  • Total page view: 67,273

   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.