TestNG HandMade Notes
How To Install Test NG:
- Go to TestNG.org
- Click on eclipse
- Copy the link give on the page: https://dl.bintray.com/testng-team/testng-eclipse-release/
- Go to Eclipse >> Click help >> select install new software >> paste the url in "Add with" box >> click on Add >> Finish
============
# All TestNG annotations:
https://www.softwaretestingmaterial.com/testng-annotations/
============
# Normally to run a java class you need a main method.
But when u run TestNG.xml it runs without the java compiler
# Adding testNG.xml to a project
Right click on project >> Click on TestNG >> Convert to TestNG
========
#Hierarchy of TestNG.xml file:
<suite name="Regression Suite">
<test name= "UI validations">
<classes>
<class name="tests.BasicsDay1"/>
</classes>
</test>
</suite>
=========
# How to exclude a method from test execution without removing or commenting the method from your test Class?
<suite name = "Regression">
<test>
<classes>
<class name = "tests.TestClass1">
<methods>
<exclude name = "testMethod1"/>
<methods/>
</class>
</classes>
</test>
</suite>
OR
<suite name = "Regression suite">
<test name = "UI validations">
<classes>
<class name = "tests.TestClass1" >
<methods>
<include name = "testMethod2"/> // this will run only the mentioned test and other test methods in the test class will be skipped.
</methods>
</classes>
</test>
</suite>
==========
# Excluding or Including a test Method in a test Class from being executed using Regex in TestNG.xml:
*The following code excludes all methods in the test Class starting with "API"
<suite name="Regression Suite">
<test name="UI validations">
<classes>
<class name="tests.testClass2">
<methods>
<exclude name="API.*" />
</methods>
</class>
</classes>
</test>
</suite>
==========
# How to Run all tests in a package?
- Add packages tag in testNg.xml as follows:
<suite name="Regression Suite">
<test name="UI validations">
<packages>
<package name="tests" />
</packages>
</test>
</suite>
==========
# How do I group test cases in TestNG. For example there is a set of tests that I want to run as a part of Smoke Tests.
Ans:- You can use the <groups> tag in TestNG.xml to include a group.
eg.,
<suite>
<test>
<groups>
<run>
<include name = "Smoke"> // This will run only methods where groups = "Smoke"
</run>
</groups>
<classes>
<class name = "tests.testClass1">
<class name = "tests.testClass2">
</classes>
</test>
</suite>
--
TestClass1{
@Test(groups = {"Smoke"})
public void m1(){
System.out.println("Test method");
}
}
=======
# By Default the Order of Execution of TestNG methods is Alphabetical
=======
# Helper attributes in TestNG: groups, dependsOnMethods etc.
=======
# What if a test case in TestNG is dependent on other Test case i.e bebute.fore executing a test case u wan't some other test case to be executed first?
Ans: You can use the dependsOnMethods attribute which will first execute the methods mentioned by the attri
Eg.,:
Class TestClass1{
public void SQLvalidations() {
System.out.println("Test method m1");
}
@Test(dependsOnMethods= {"SQLvalidations"})
public void APItest() {
System.out.println("Test method m2");
}
@Test
public void uiValidations() {
System.out.println("Test method m3");
}
@Test(dependsOnMethods= {"APItest","uiValidations"}) // Here APItest and uiValidations methods will run before this method
public void otherValidations() {
System.out.println("Test method m4");
}
--> Here SQLvalidations method will run before the APItest method.
========
#Suppose there is a test method which you want to skip?
Ans: use enabled attribute in testNG.
Eg.,
@Test(enabled = false)
public void testMethod1(){
}
========
#Parameterized tests using testNG:
- Suppose you want to send a value as parameter to all tests.
Parameters like appn URL which is common for all tests, you can use parameter tag in testNG.xml and @Parameters annotation in the test Class.
Eg.,
<suite>
<test>
<parameter name = "URL" value = "https://www.google.com"/>
<parameter name = "username" value = "kadu"/>
<classes>
<class name = "tests.testClass1">
</classes>
</test>
</suite>
- TestClass1{
@Parameters(String url, String username, String password)
@Test
public void m1(String url, String username, String password){
System.out.println("Parameter url: "+url);
}
public void m2(){}
}
--> O/p: Parameter url: https://www.google.com
=======
# DataProvider in testNG:
public class ExcelData {
// Pls note here DataProvider method is in different Class:
@DataProvider(name = "ExcelDataProvider")
public static Object[][] getData() {
Object data[][] = new Object[3][2];
data[0][0] = "krunal";
data[0][1] = "xyz*123";
data[1][0] = "pallavi";
data[1][1] = "pal@123";
data[2][0] = "alka";
data[2][1] = "al@145";
return data;
}
}
//The following method will run 3 times as the dataProvider returns an object array of 3 rows and 2 columns
@Test(dataProvider = "ExcelDataProvider", dataProviderClass = ExcelData.class)
public void APItest1(String username, String password) {
System.out.println("APItest1 testClass2");
System.out.println("usernmame: "+username);
System.out.println("password: "+password);
}
========
#Listeners in TestNG:
- If you want to write some code to take a screenshot on test failure or anything like that you can use the ITestListener Interface in TestNG
- If you want to use Listeners you must implement the ITestListener interface.
ITestListener interface has following methods which should be overriden by the implemented class:
onTestStart(): An onTestStart() is invoked only when any test method gets started.
onTestSuccess(): An onTestSuccess() method is executed on the success of a test method.
onTestFailure(): An onTestFailure() method is invoked when test method fails.
onTestSkipped(): An onTestSkipped() run only when any test method has been skipped.
onTestFailedButWithinSuccessPercentage(): This method is invoked each time when the test method fails but within success percentage.
onStart(): An onStart() method is executed on the start of a <test> tag
onFinish(): An onFinish() method is executed on end of a <test> tag
======
Q. How do you run only selected test cases in testNG.
Ans:- There are 3 ways to do so:
1. By using enabled Helper attribute.
@Test(enabled=true)
Sample testng script to run the class.
<suite name = "Suite">
<test name = "test">
<classes>
<class name = "TestClass" />
</classes>
</test>
</suite>
Second method is to setup the group details at Test annotation.
2. By using groups in TestNG:
@Test(group = {functionalTest} )
Sample script to run the class
<suite name = "Suite">
<test name = "test">
<groups>
<run>
<include name = "functionalTest" />
</run>
</groups>
<classes>
<class name = "TestClass" />
</classes>
</test>
</suite>
3. Without using annotation , we can skip few cases in selenium testing by following the script in testng Suite
With Include Option:
<suite name = "Suite">
<test name = "test">
<classes>
<class name = "TestClass">
<methods>
<include name= "FirstCase"/>
<include name= "SecondCase"/>
<include name= "ThirdCase"/>
</methods>
</class>
</classes>
</test>
</suite>
OR With Exclude Option:
<suite name = "Suite">
<test name = "test">
<classes>
<class name = "TestClass">
<methods>
<exclude name= "FourthCase"/>
<exclude name= "FifthCase"/>
</methods>
</class>
</classes>
</test>
</suite>
Comments
Post a Comment