• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / TestNG Tutorial / Best and easy way to Group test cases in selenium

Best and easy way to Group test cases in selenium

July 23, 2023 by Mukesh Otwani 15 Comments

Group Selenium Script using TestNG

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 🙂

 

Filed Under: TestNG Tutorial Tagged With: Grouping of Testcases

Reader Interactions

Comments

  1. Reddy says

    March 13, 2020 at 4:21 AM

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

    Reply
    • Mukesh Otwani says

      March 13, 2020 at 9:58 AM

      Hi Reddy,

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

      Reply
  2. Anindita says

    February 3, 2017 at 3:59 PM

    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?

    Reply
    • Mukesh Otwani says

      February 3, 2017 at 5:14 PM

      Hi Anindita,

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

      Reply
      • Anindita says

        February 6, 2017 at 4:21 PM

        Hi Mukesh,

        Thanks a lot…..

        Reply
  3. suraj says

    September 10, 2016 at 11:45 PM

    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.

    Reply
    • Mukesh Otwani says

      September 12, 2016 at 4:18 PM

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

      Reply
  4. Prashant says

    August 26, 2016 at 10:09 PM

    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?

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 10:27 PM

      Yes Prashant we can do that.

      Reply
  5. Lalit Aggarwal says

    May 1, 2015 at 2:26 PM

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

    Reply
    • Mukesh Otwani says

      May 1, 2015 at 11:00 PM

      Hi Lalit,

      Great keep Visiting, learning and Sharing

      Reply
      • Lalit Aggarwal says

        May 3, 2015 at 3:47 PM

        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?

        Reply
        • Mukesh Otwani says

          May 8, 2015 at 10:01 PM

          Hi Lalit,

          Here is the link

          https://drive.google.com/drive/folders/0B5v_nInLNoquV1p5YWtHc3lkUkU

          Reply
  6. Puneeth says

    March 31, 2015 at 10:54 AM

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

    Reply
    • Mukesh Otwani says

      March 31, 2015 at 9:57 PM

      Hey Puneeth,

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

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?