TestNG Tutorial

Best and easy way to Group test cases in selenium

How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
How To Group test cases in selenium. I am sure you must have got this question in mind is there any way to group a set of test cases in Selenium, yes we have the TestNG group feature which will allow you to do the same. Grouping in Automation is quite an interesting feature through which you can easily categorize your test cases based on requirements. For Example- If we have 200 test cases out of these some are end-to-end test cases, some are functional test cases and some are regression or smoke test cases so if you don’t categorize these test cases these all test cases will come under one common category.
Group test cases in selenium

Group test cases in selenium

   

Group Selenium Script using TestNG

For grouping in Selenium, we have to use TestNG feature through which we can create groups and maintain them easily. We can include and exclude groups through testng.xml file  

 How to create group in TestNG

We have predefined syntax for this  
    Syntax

   @Test(groups={"groupname"})
   
    Example

    @Test(groups={"Smoke"})
We can create multiple groups as well
  Syntax

    @Test(groups={"group1","group2"})



  After grouping in Selenium we can specify the include and exclude in testng.xml. <include> – It tells testng.xml that which group we need to execute. <exclude>- It tells testng.xml which group we have to Skip   Therefore, if you execute your test cases with grouping then we have to modify our testng.xml file and we have to explicitly specify the group.

Complete program to group test cases in selenium

Sample Program

package testngDemo;

import org.testng.annotations.Test;

public class TestGroupDemo {

    
    @Test(groups={"Smoke"})
    public void login(){
        
        System.out.println("Login done");
        System.out.println("Smoke Scenario passed");
    }
    
    @Test(groups={"Regression"})
    public void register(){
        System.out.println("Registration done");
    }
    
    
    
}
 

We need to modify the  testng.xml to Group test cases in selenium

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">
    <groups>
     
       <run>
            
            <include name="Smoke" />
            <exclude name="Regression"/>
             
       </run>
     
    </groups>
    <classes>
      <class name="testngDemo.TestGroupDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

In above xml we have crea
  I created a new tag called group, inside the group we have mentioned the include and exclude tag, and we have mentioned the group name, which we have to include and exclude.   Once you run the above XML it will run all the test cases, which belong to Smoke group category. Let us run this XML and see the output.   Group test cases in selenium    Console output.   Group Selenium Script using TestNG Check out HTML Reports as well after executing scripts. You can verify included and excluded the group in report itself. Visting XSLT reports also for attractive reports.
  Thanks for visiting my blog. Share this post with others as well. Comment below if finding any issue. Have a nice day 🙂
 
author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

15 thoughts on “Best and easy way to Group test cases in selenium

  1. Reddy says:

    How tagging can be done in keyword driven framework where we don’t have TestNG

    1. Hi Reddy,

      Grouping is from TestNg only. If you don’t use TestNG then you cannot use tagging.

  2. Anindita says:

    Hi Mukesh,

    Your posts are really wonderful.
    I have a question: Suppose I am using Selenium with TestNG. Now for a particular release I have some changes/enhancement over the existing one. Now how come I build and run those changed and related parts only rather than running the whole suite?

    1. Hi Anindita,

      Either you can create separate xml file such one for regression another one for smoke test or edit the existing xml file.

      1. Anindita says:

        Hi Mukesh,

        Thanks a lot…..

  3. suraj says:

    Hi Mukesh,

    After running tests by testng.xml the browser not get closed .
    I used
    @AfterTest
    public void teardown()
    {
    driver.quit();
    }
    but it is not get executed. How can I execute this via testng.xml file to close all browsers opened after executing test.

    1. Right click on testcase > TestNG> Convert to testng and run again.

  4. Prashant says:

    Hi Mukesh,

    Superb explaination!!!. Mukesh I have a question: Can we group the different methods from different classes and run them together.
    Let suppose: We have 3 classes and in each class we have 3 methods. So can we group as: 2 methods from class 1, 2 method from class 2 and 1 methods from class 3?

    1. Yes Prashant we can do that.

  5. Lalit Aggarwal says:

    Thanks dear for posting, it clears my Groups concept in TestNG.

    1. Mukesh Otwani says:

      Hi Lalit,

      Great keep Visiting, learning and Sharing

      1. Lalit Aggarwal says:

        Hi Mukesh,
        I was reading the XSLT report topic there you mentioned that build.xml file we need to download from your Google documents, could you please share me that build.xml along other folder required to generate the xslt report?

  6. Puneeth says:

    It’s really good!!! Can you create a post for Webtable concepts faced across realtime?

    1. Mukesh Otwani says:

      Hey Puneeth,

      Great you like this post. Soon I will post for Webtable also 🙂

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.