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 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.
Console output.
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 🙂
Reddy says
How tagging can be done in keyword driven framework where we don’t have TestNG
Mukesh Otwani says
Hi Reddy,
Grouping is from TestNg only. If you don’t use TestNG then you cannot use tagging.
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?
Mukesh Otwani says
Hi Anindita,
Either you can create separate xml file such one for regression another one for smoke test or edit the existing xml file.
Anindita says
Hi Mukesh,
Thanks a lot…..
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.
Mukesh Otwani says
Right click on testcase > TestNG> Convert to testng and run again.
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?
Mukesh Otwani says
Yes Prashant we can do that.
Lalit Aggarwal says
Thanks dear for posting, it clears my Groups concept in TestNG.
Mukesh Otwani says
Hi Lalit,
Great keep Visiting, learning and Sharing
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?
Mukesh Otwani says
Hi Lalit,
Here is the link
https://drive.google.com/drive/folders/0B5v_nInLNoquV1p5YWtHc3lkUkU
Puneeth says
It’s really good!!! Can you create a post for Webtable concepts faced across realtime?
Mukesh Otwani says
Hey Puneeth,
Great you like this post. Soon I will post for Webtable also 🙂