TestNG is a next-generation testing framework. designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages, and even several external frameworks, such as application servers).
priority: The priority for this test method. Lower priorities will be scheduled first.
@Test(priority=1)
public void t1(){}
@Test(priority=2)
public void l1(){}
Advantages:
- TestNg provides rich annotation support
- TestNg generates better html/xslt reports
- we can prioritize the testcases(@Test methods)ex: @Test(priority=1)
- we can skip the testcases ex:@Test(enabled=false)
- we can set dependancy among the test methods ex:@Test(dependsOnMethods={"test1"})
- we can group the test cases ex:@Test(groups={"smoke/sanity/regression"})
- we can run the testcases parallally on multiple browsers
- we can do datadriven testing using @DataProvider annotation
- we can supply parameter values from xml file using @Parameters annotation
Installing TestNG in Eclipse:
Install via Eclipse Marketplace
Go to the TestNG page on the Eclipse Market Place and drag the icon called "Install" onto your workspace.Install from update site:
- Go to Help --> Install New Software...
- Enter the update site URL in "Work with:" field:
- Update site for release: https://testng.org/testng-eclipse-update-site
- Make sure the check box next to URL is checked and click Next.
- Eclipse will then guide you through the process.
Writing a test is typically a three-step process:
- Write the business logic of your test and insert TestNG annotations in your code.
- Add the information about your test (e.g. the class name, the groups you wish to run, etc...) in a testng.xml file or in build.xml.
- Run TestNG.
2. Annotations:
- @BeforeSuite: The annotated method will be run before all tests in this suite have run.
@BeforeSuite
public void beforeSuite(){
//write the code
}
- @AfterSuite: The annotated method will be run after all tests in this suite have run.
@AfterSuite
public void afterSuite(){
//code here
}
- @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@BeforeTest
public void beforeTest(){
//code here;
}
- @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@AfterTest
public void afterTest(){
//code here;
}
- @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@BeforeGroups
public void beforeGroups(){
//code here;
}
- @AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@AfterGroups
public void afterGroups(){
//code here;
}
- @BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@BeforeClass
public void beforeClass(){
//precondition code for test
}
- @AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@AfterClass
public void afterClass(){
destroycode;
}
- @BeforeMethod: The annotated method will be run before each test method.
@BeforeMethod
public void setup(){
precondition code for every testcode
}
- @AfterMethod: The annotated method will be run after each test method.
@AfterMethod
public void tearDown(){
destroycode;
}
- @DataProvider: Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
Example:
public class DataproviderDemo{
@DataProvider(name="name of the dataprovider")
public Object[][] methodName(){
Object[][] data=new Object[rows][cols];
//rows tells you number of times test has to be executed
//columns will tell you number of parameters in the test
mehod
Object[][] data=new object[3][2];
//first row
data[0][0]="uname1";
data[0][1]="pwd1";
//2nd row
data[1][0]="uname2";
data[1][1]="pwd2";
//3rd row
data[2][0]="uname3";
data[2][1]="pwd3";
return data;
}
@Test(dataProvider="dataprovidername/dataprovidermethodname")
public void loginTest(String uname,String pwd){
Sop(uname+" "+pwd);
}
}
}
------------------------------------------------------------
@Parameters: Describes how to pass parameters to a @Test method.
To do this, we need to declare parameter tag in xml file
using 'name' and 'value' attribute.
Where the name attribute of the tag defines name of the
parameter and the value attribute defines the value of the
parameter.
parameter names are declared inside the class and above the
method
syntax:
public class ParameterDemo{
@Parameters({"parameter1","parameter2",..})
@Test
public void test(String parameter1,String parameter2){
//type the username
driver.findElement(By.name("")).sendKeys(parameter1);
//type the password
driver.findElement(By.name("")).sendKeys(parameter2);
logic inside the method
}
}
How To Create an testng.xml file for particular class
1)right click on the class
2)select TESTNG option-->convert to testNG
3)give the suite name,test name and xml file location
4)click on finish button
//parameter values will be supplied from testng.xml file
testng.xml format
----------------------------------------------------------------------------
<suite name="suitename" parallel="none">
<test name="testname">
<parameter name="parameter1" value="valueofparameter1"/>
<parameter name="parameter2" value="value of parameter2"/>
<classes>
<class name="packagename.classname1"/>
<class name="packagename.classname2"/>
</classes>
</test>
</suite>
============================================
- @Test: Marks a class or a method as part of the test.
@Test
public void methodTest(){
//only test steps;
}
@Test Keywords/Parameters:
dataProvider : The name of the data provider for this test method.
this keyword is used to map between @DataProvider and @Test
Syntax:
@Test(dataProvider="dataprovidername/dataprovidermethodname")
this keyword is used to map between @DataProvider and @Test
Syntax:
@Test(dataProvider="dataprovidername/dataprovidermethodname")
dependsOnGroups: The list of groups this method depends on.
Syntax:
@Test(dependsOnGroups={"groupname"})
public void dgroup(){}
Syntax:
@Test(dependsOnGroups={"groupname"})
public void dgroup(){}
dependsOnMethods: The list of methods this method depends on.
Syntax:
@Test(dependsOnMethods={"methodname1","method2"})
public void dmethod(){}
alwaysRun: If set to true, this test method will always be run even
if it depends on a method that failed.
@Test(alwaysRun=true,dependsOnMethods={"t"})
public void t1(){}
description: The description for this method.
Syntax:
@Test(dependsOnMethods={"methodname1","method2"})
public void dmethod(){}
alwaysRun: If set to true, this test method will always be run even
if it depends on a method that failed.
@Test(alwaysRun=true,dependsOnMethods={"t"})
public void t1(){}
description: The description for this method.
Syntax:
@Test(description="write small description")
enabled: Whether methods on this class/method are enabled.
Syntax:
@Test(enabled=false)--this test method will skip
@Test(description="write small description")
enabled: Whether methods on this class/method are enabled.
Syntax:
@Test(enabled=false)--this test method will skip
expectedExceptions: The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
Syntax:
@Test(expectedExceptions={"e1","e2"..})
Syntax:
@Test(expectedExceptions={"e1","e2"..})
groups: The list of groups this class/method belongs to.
Grouping in Automation is a 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 the TestNG feature through which we can create groups and maintain them easily.
Syntax
@Test(groups={"groupname"})
public void methodname(){}
Example:
@Test(groups={"Smoke"},priority=1,description="")
public void m1(){}
We can create multiple groups as well
Syntax
@Test(groups={"group1","group2"})
public void methodname(){}
We can include and exclude groups through testng.xml file
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
------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SuiteGroups" parallel="none">
<test name="Testgroups">
<groups>
<run>
<include name="Smoke" />
<exclude name="Regression"/>
</run>
</groups>
<classes>
<class name="testngDemo.TestGroupDemo"/>
<class name="testngDemo.TestGroupDemo122"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
I created a new tag called group, an inside group we have mentioned includes and excludes tags, 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 the Smoke group category.
Grouping in Automation is a 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 the TestNG feature through which we can create groups and maintain them easily.
Syntax
@Test(groups={"groupname"})
public void methodname(){}
Example:
@Test(groups={"Smoke"},priority=1,description="")
public void m1(){}
We can create multiple groups as well
Syntax
@Test(groups={"group1","group2"})
public void methodname(){}
We can include and exclude groups through testng.xml file
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
------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SuiteGroups" parallel="none">
<test name="Testgroups">
<groups>
<run>
<include name="Smoke" />
<exclude name="Regression"/>
</run>
</groups>
<classes>
<class name="testngDemo.TestGroupDemo"/>
<class name="testngDemo.TestGroupDemo122"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
I created a new tag called group, an inside group we have mentioned includes and excludes tags, 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 the Smoke group category.
invocationCount: The number of times this method should be invoked.
Syntax:
@Test(invocationCount=3)
Syntax:
@Test(invocationCount=3)
successpercentage: The percentage of success expected from this method
Timeout: The maximum number of milliseconds this test should take.
@Test(timeOut=3000)
@Test(timeOut=3000)
threadPoolSize: The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount.
@Test(priority=1)
public void t1(){}
@Test(priority=2)
public void l1(){}
How to Specify the particular class methods using include & exclude tags?
You can also specify groups and methods to be included and excluded:
<suite name="nameofthesuite">
<test name="Regression1">
<groups>
<run>
<exclude name="brokenTests" />
<include name="checkinTests" />
</run>
</groups>
<classes>
<class name="test.IndividualMethodsTest">
<methods>
<include name="testMethod" />
<exclude name="test2"/>
</methods>
</class>
<class name="test.IndependentTest"/>
</classes>
</test>
</suite>
TestNg Reports:
Importance of Reporting in Selenium?
Ans-1- Reports helps you to identify the status of the test case (Pass/Fail/Skip).
2- Using reports we calculate the time taken by each test case that helps to calculate ROI(Return of Investment).
3- You can share automation reports with your team and clients as well to share the status of testing progress etc.
TestNG will provide three types of reports?
1)First open Default test.html
2)Second open emailable-report.html
3)Third open Index.html
if you expand test-output folder under the same project you can find the above three reports.
--------------------------------------------------------------------------------------
ReportNg Reports:
we need two dependencies
1) ReportNG --[org.uncommons » reportng]
2)Google Guice Core Library
include below listeners tag in testNg.xml after suite tag
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>
==================================================================
How to generate a report in link format:
Reporter.log("<a href=\"D:\\screenshot\\error.jpg\">screenshot</a>");
wherever you are adding the above line of anchor tag just add the below line of code inside the method in first
System.getProperty("org.uncommons.reportng.escape-output","false");
if you want to open the screenshot in another/separate tab then change the 1544 line like below
Reporter.log("<a target=\"blank\" href=\"D:\\screenshot\\error.jpg\">screenshot</a>");
if you want to add a small thumbnail in the reports.
Reporter.log("<a target=\"blank\" href=\"D:\\screenshot\\error.jpg\">screenshot</a>");
Reporter.log("<br>");
Reporter.log("<a target=\"blank\" href=\"D:\\screenshot\\error.jpg\"><img src=\"D:\\screenshot\\error.jpg\" height=200 width=200></img></a>");
======================================================================
How to Run Parallel tests, classes, and methods
=======================================================================
The parallel attribute on the <suite> tag can take one of the following values:
<suite name="My suite" parallel="methods" thread-count="5">
view source print?
<suite name="My suite" parallel="tests" thread-count="5">
<suite name="My suite" parallel="classes" thread-count="5">
<suite name="My suite" parallel="instances" thread-count="5">
parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread-safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
