TestNG is a Testing framework that covers different types of test designs like unit, functional, end to end, UI and integration test.

You can run a single or multiple packages (package here means to encapsulate a group of classes in a proper director format) by creating XML and run it through maven.

TestNG Groups with Example

We use groups in Testng when,

  • We don't want to define test methods separately in different classes (depending upon functionality) and
  • At the same time want to ignore (not to execute) some test cases as if they does not exist in the code.
  • So to carry out this we have to Group them. This is done by using "include" and "exclude" mechanism supported in testNG.

In below example, we have shown the syntax of how to use groups in the XML file.

@Test (groups = { "mobile TC", "browser TC" })              

Here we are using 2 group names i.e. "bonding" and "strong_ties" (these are logical name that can be altered as per your wish).

<groups> tag defines the starting of groups in XML.

Customize your XML to pick the mentioned group from the test classes. Below mentioned is the syntax of how to declare groups in XML file e.g.

<groups>
<run>
<include name=" mobile TC " />
</run>
</groups>

So, let us assume that there are 10 test methods in a class.

Out of them,

  • 6 methods are tagged in " mobile TC " group and
  • 4 are in " browser TC " group.

Introduction to XML and how to make an XML files

  • XML (Extensible Markup Language) file in Maven framework contains the information of one or more tests and is defined by the tag <suite>.
  • Test information in XML is represented by tag <test> and can contain one or more TestNG classes.
  • A Java class which contains @Test annotation above test methods is defined as TestNG methods.

Multiple tags are used in a sequence to build a working testNG xml like <suite>, <test> and <class>

  • First is <suite> tag, which holds a logical name which defines full information to testNG reported to generate execution report.
  • Second is <test name="TestNG Demo">, note it is logical name which holds the information of test execution report like pass, fail, skip test cases and other information like total time for execution and group info
  • Third is <class name="com.group.rahulshetty.TC_Class1" />, com.group.guru99 is the package used, and Test Class name is TC_Class1.

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

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

            <test name=" TestNG Demo">

                        <groups>

                                    <run>

                                                <include name="Mobile TC "/>

                                    </run>

                        </groups>

                        <classes>

                                    <class name="com.group. rahulshetty.TC_Class1"/>

                        </classes>

            </test>

</suite>

Another mechanism instead of Grouping is "exclude" or "include" in test XML

Suppose you are finding the usage of group mechanism complex then testNG XML facilitate the functionality to exclude/include a test.

Exclude Tag:  Syntax for exclude tag <exclude name="${TEST_CASE_NAME}" />
Include Tag:  Syntax for include tag <include name="${TEST_CASE_NAME}" />

Note: We can include/exclude multiple test cases once at a time, and it works with Groups as well.

Test script code

package com.group.rahulshetty;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.Test;

public class TestNGDemo {

       public static final WebDriver driver = new ChromeDriver();;

       String browserHeading = "Practice Page";

       /**

        * This test case will initialize the webDriver

        *

        */

       @Test(groups = { "mobile TC", "browser TC" })

       public void launchURL() {

              driver.manage().window().maximize();

              driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

              driver.get("https://rahulshettyacademy.com/AutomationPractice/");

       }

       /**

        * Will check the presence of Heading on home Page

        */

       @Test(groups = { "mobile TC" })

       public void verifyLaunchPage() {

              Assert.assertTrue(driver.findElement(By.xpath(browserHeading)).isDisplayed(),

                           "Home Page heading is displayed");

              System.out.println("Home Page heading is displayed");

       }

       /**

        * To click on radio button

        */

       @Test(groups = { "browser TC" })

       public void clickRadioButton() {

              driver.findElement(By.xpath("//input[@value='radio1']")).click();

              System.out.println("Radio Button clicked");

       }

}

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

       <modelVersion>4.0.0</modelVersion>

       <groupId>GroupTestNG</groupId>

       <artifactId>GroupTestNG</artifactId>

       <version>0.0.1-SNAPSHOT</version>

       <build>

              <sourceDirectory>src</sourceDirectory>

              <plugins>

                     <plugin>

                           <artifactId>maven-compiler-plugin</artifactId>

                           <version>3.3</version>

                           <configuration>

                                  <source>1.7</source>

                                  <target>1.7</target>

                           </configuration>

                     </plugin>

              </plugins>

       </build>

       <dependencies>

              <dependency>

                     <groupId>org.seleniumhq.selenium</groupId>

                     <artifactId>selenium-java</artifactId>

                     <version>2.48.2</version>

              </dependency>

       </dependencies>

</project>

TestNG_exclude.xml

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

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

       <test name="Rahul Shetty Smoke test">

              <classes>

                     <class name="com.group.rahulshetty.TestNGDemo.java">

                           <methods>

                                  <exclude name="verifyLaunchPage"></exclude>

                           </methods>

                     </class>

              </classes>

       </test>

</suite>

TestNG_group.xml

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

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

       <test name="Rahul Shetty Smoke Test Demo">

              <!-- <groups>

                     <run>

                           <include name="mobile TC" />

                     </run>

              </groups> -->

             

              <!-- currently group is commented -->

              <classes>

                     <class name="com.group.rahulshetty.TestNGDemo.java" />

              </classes>

       </test>

</suite>

TestNG_include.xml

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

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

       <test name="Rahul Shetty Regression Test Demo">

              <classes>

                     <class name="com.group.rahulshetty.TestNGDemo.java">

                           <methods>

                                  <include name="launchURL"></include>

                                  <include name="verifyLaunchPage"></include>

                                  <include name="clickRadioButton"></include>

                           </methods>

                     </class>

              </classes>

       </test>

</suite>

If we consider above code we have learned that we can include and exclude or group test cases based on our requirement, using TestNG XML we can created test groups for sanity testing, smoke testing and regression testing.

The above code can also be found below.

GroupTestNG.zip


Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}