[Selenium+Java] Introduction to TestNG Groups
Original URL: https://www.guru99.com/introduction-testng-groups.html
Introduction to TestNG Groups
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.
In this tutorial, you will learn-
-
TestNG groups with Example
-
Set maven and Java path in environment variable (for windows user)
-
Introduction to XML and how to make an XML files
-
Another mechanism instead of Grouping is “exclude” or “include” in test XML
-
How to run code using XML file (video demo)
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 = { "bonding", "strong_ties" })
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="bonding" /> </run> </groups>
So, let us assume that there are 10 test methods in a class.
Out of them,
- 6 methods are tagged in "bonding" group and
- 4 are in "strong_ties" group
Moving forward, we are going to set maven/Java path and use the Eclipse IDE to demonstrate the usage of groups using XML files in Java based maven project.
Set maven and Java path in environment variable (for windows user)
Please refer https://www.guru99.com/maven-jenkins-with-selenium-complete-tutorial.html
https://www.guru99.com/install-java.html
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="Guru 99 Smoke Test 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.guru99.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="Guru 99 Smoke Test Demo"> <groups> <run> <include name="strong_ties" /> </run> </groups> <classes> <class name="com.group.guru99.TC_Class1" /> </classes> </test> </suite>
We will be using this XML for upcoming video downside.
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.
How to run code using XML file (video demo)
Explanation of the Java Code and XML with the group, exclude and include the tag in XML.
- Scenario: Launch Guru99 demo Banking site, verify few thing‘s on login page after that enter credentials and re-verify few new thing on the application when logged in.
Note: Each step which you code should be declared in separate methods, but when executed, it will execute test methods depending upon the entries in the XML file.
Method 1: Initialize Browser and launch URL (tc01LaunchURL())
Method 2: Verify Login Page Heading (tc02VerifyLaunchPage())
Method 3: Enter userName and Password on login form (tc03EnterCredentials())
Method 4: Verify the presence of Manager ID on User Dashboard (tc04VerifyLoggedInPage())
Method 5: Verify few more links on User DashBoard (tc05VerifyHyperlinks())
Code for our scenario:
package com.group.guru99; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; public class TC_Class1 { public static final WebDriver webDriver = new FirefoxDriver();; String launchPageHeading = "//h3[text()=‘Guru99 Bank‘]"; final String userName_element = "//input[@name=‘uid‘]", password_element = "//input[@name=‘password‘]", signIn_element = "//input[@name=‘btnLogin‘]"; final String userName_value = "mngr28642", password_value = "ydAnate"; final String managerID = "//td[contains(text(),‘Manger Id‘)]"; final String newCustomer = "//a[@href=‘addcustomerpage.php‘]", fundTransfer = "//a[@href=‘FundTransInput.php‘]"; /** * This test case will initialize the webDriver */ @Test(groups = { "bonding", "strong_ties" }) public void tc01LaunchURL() { webDriver.manage().window().maximize(); webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); webDriver.get("http://www.demo.guru99.com/V4/"); } /** * Will check the presence of Heading on Login Page */ @Test(groups = { "bonding" }) public void tc02VerifyLaunchPage() { Assert.assertTrue(webDriver.findElement(By.xpath(launchPageHeading)).isDisplayed(), "Home Page heading is not displayed"); System.out.println("Home Page heading is displayed"); } /** * This test case will enter User name, password and will then click on * signIn button */ @Test(groups = { "bonding", "strong_ties" }) public void tc03EnterCredentials() { webDriver.findElement(By.xpath(userName_element)).sendKeys(userName_value); webDriver.findElement(By.xpath(password_element)).sendKeys(password_value); webDriver.findElement(By.xpath(signIn_element)).click(); } /** * This test case will verify manger‘s ID presence on DashBoard */ @Test(groups = { "strong_ties" }) public void tc04VerifyLoggedInPage() { Assert.assertTrue(webDriver.findElement(By.xpath(managerID)).isDisplayed(), "Manager ID label is not displayed"); System.out.println("Manger Id label is displayed"); } /** * This test case will check the presence of presence of New customer link * And FundTransfer link in Left pannel */ @Test(groups = { "bonding" }) public void tc05VerifyHyperlinks() { Assert.assertTrue(webDriver.findElement(By.xpath(newCustomer)).isEnabled(), "New customer hyperlink is not displayed"); System.out.println("New customer hyperlink is displayed"); Assert.assertTrue(webDriver.findElement(By.xpath(fundTransfer)).isEnabled(), "Fund Transfer hyperlink is not displayed"); System.out.println("Fund Transfer hyperlink is displayed"); } }
Please Note: The credentials are only valid for 20 days, so if you are trying to run code on your local machine, so you might face invalid credentials error. Please find below steps to generate your login credentials:
- Launch http://www.demo.guru99.com
- Enter your email id in the box.
- Click enter and see your login details on screen.
Explanation of Code:
As mentioned above, we have created 5 test cases for performing each action in independent methods.
You can observe that to every method, we have associated a group parameter holding some value in it.
Basically, these are the name of the differentiating groups i.e. "strong_ties" & "bonding".
- First and Third methods are tagged to "bonding", "strong_ties" which means if XML is updated in any of the group, this Test Case will run.
- The second method is only tagged to "bonding" group it means that if XML is updated with bonding group. Only in that case this test case will run.
- Fourth Test case is tagged to strong_ties group, which means this test case will only run if XML is updated with strong_ties group name.
- Last but not the least fifth test case is attached to bonding group, which means this test case will only run if XML is updated with bonding group name.
So overall, we have 4 scenarios;
- We want to run all test cases irrespective of the group name. In this case, we will remove Group tag from running XML.
- We want to run test case few test that are related only to either of groups i.e. strong_ties or bonding
- Please refer:
- In this video, Group parameter is commented from running XML. Hence, you will see all test cases were executed.
- In continuation to video, now we have included group name in XML, you can see only test cases specific to that group is only running.
- We are using Exclude mechanism to exclude the test case:
- Please refer
- You see that we have used exclude few test case (tc02) by writing their name in running XML. In final result mentioned test cases did not run.
4.Last, we are using include test mechanism to include the test cases (tc01LaunchURL, tc03EnterCredentials and tc05VerifyHyperlinks)
- Please refer
- In this video, you will see that test cases which are mentioned in XML are only running during the test execution.
Please download code from the mentioned URL, it will contain all type of testXML:
Download the above Code
Conclusion
We have learned here relatively a new way for running test cases using XML in Maven project.
We started by providing a brief introduction on testNG and continued with the full technical specification of Groups, exclude and include.
[Selenium+Java] Introduction to TestNG Groups