[Selenium+java] Introduction to TestNG Groups

Source: Internet
Author: User
Tags testng guru99

Original URL: https://www.guru99.com/introduction-testng-groups.html

Introduction to TestNG Groups

TestNG is a testing framework this covers different types of test designs like unit, functional, end to end, UI and Integr ation test.

Can run a single or multiple packages (package here means to encapsulate a group of classes in a proper director forma T) by creating XML and run it through MAVEN.






In this tutorial, you'll 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 of this we had to Group them. This was done by using the "include" and "exclude" mechanism supported in TestNG.

In below example, we had 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 is logical name that can be altered as per your W ISH).

<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 what to declare groups in XML file e.g.

<groups>   <run>    <include name= "bonding"/>   </run>  </groups>

So, let us assume that there is ten test methods in a class.

Out of them,

    • 6 methods is tagged in "bonding" group and
    • 4 is in "Strong_ties" group

Moving forward, we is going to set Maven/java path and use the Eclipse IDE to demonstrate the usage of the groups using XML F Iles 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 are defined by T He tag <suite>.
    • Test information in XML are represented by tags <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 is used in a sequence to build a working TestNG XML like <suite>, <test> and <class>

    • First is <suite> tags, which holds a logical name which defines full information to TestNG reported to generate EXECU tion report.
    • Second is <test name= "Guru Smoke test Demo", note it is logical name which holds the information of test Executi On 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 are 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 Smoke Test Demo"><groups><run>    <include name=" strong_ties "/>        </run>       </groups><classes><class name= "com.group.guru99.TC_Class1"/>           </classes ></test>  </suite>

We'll be using the this XML for upcoming video downside.

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

Suppose you is finding the usage of group mechanism complex then TestNG XML facilitate the functionality to EXCLUDE/INCLU De 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-v Erify few new thing on the application when logged in.

Note: Each step which you code should is declared in separate methods, and when executed, it'll execute test me Thods 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 would initialize the Webdriver */@Test (groups = {"Bonding", "strong_ties"}) public Vo ID Tc01launchurl () {webdriver.manage (). window (). Maximize ();        Webdriver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);    Webdriver.get ("http://www.demo.guru99.com/V4/"); /** * would check the presence of Heading on Login Page */@Test (groups = {"Bonding"}) public void tc0                2VerifyLaunchPage () {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 would enter the User name, password and would then click on * signIn button */@Test (gr Oups = {"Bonding", "strong_ties"}) public void Tc03entercredentials () {webdriver.findelement (By.xpath (Usernam        e_element). SendKeys (Username_value);        Webdriver.findelement (By.xpath (password_element)). SendKeys (Password_value);    Webdriver.findelement (By.xpath (signin_element)). Click ();   }/** * This test case would verify manger ' s ID presence on DashBoard  */@Test (groups = {"Strong_ties"}) public void Tc04verifyloggedinpage () {assert.asserttrue (Webdriver.fin        Delement (By.xpath (ManagerID)). isdisplayed (), "Manager ID label is not displayed");    System.out.println ("Manger Id label is displayed");  }/** * This test case would 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 no        T displayed ");    System.out.println ("Fund Transfer hyperlink is displayed"); }}

Please note:the credentials is only valid for the days, so if you is trying to run code on your local machine, so you mi Ght face Invalid credentials error. Please find below steps to generate your login credentials:

    1. Launch http://www.demo.guru99.com
    2. Enter your email id in the box.
    3. 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 this to every method, we had associated a group parameter holding some value in it.

Basically, these is the name of the differentiating groups i.e. "strong_ties" & "bonding".

    • First and third methods was tagged to "bonding", "strong_ties" which means if XML was updated in all of the group, this Tes T case would run.
    • The second method is a tagged to "bonding" group it means, if XML is updated with bonding group. Only in the case of this test case would run.
    • Fourth Test case was tagged to strong_ties Group, which means this Test case would only run if XML was updated with Strong_ti ES group name.
    • Last but not the least fifth Test case was attached to bonding group, which means this test case would only run if XML was up Dated with bonding group name.

So overall, we have 4 scenarios;

    1. We want to run all test cases irrespective of the group name. In this case, we'll remove Group tag from running XML.
    1. We want to run test case few test that is related only to either of groups i.e. strong_ties or bonding
    • Please refer:

    • In this video, the Group parameter is commented from running XML. Hence, you'll see all test cases were executed.
    • In continuation to video, now we had included group name in XML, and you can see only test cases specific Ly running.
    1. We is using Exclude mechanism to Exclude the test case:
    • Please refer

    • You see, we have the used exclude few test case (TC02) by writing their The name in running XML. In final result mentioned test cases do not run.

4.Last, we is using include test mechanism to include the test cases (Tc01launchurl, tc03entercredentials and Tc05verifyh Yperlinks)

    • Please refer

    • In this video, you'll see that test cases which is mentioned in XML is only running during the test execution.

Download code from the mentioned URL, it'll contain all type of testxml:

Download the above Code

Conclusion

We have learned here relatively a new to 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, excl Ude and include.

[Selenium+java] Introduction to TestNG Groups

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.