TestNG SeleniumFramework
TestNG SeleniumFramework
Introduction to TestNG
Installation Steps of TestNG
Import of TestNG
Annotations in TestNG
Reports generation in TestNG
Assertion
How to set priority
Advantages of TestNG
2
Introduction to TestNG Framework
TestNG is a testing framework tool which supports both Java and .net languages.
TestNG has all features of Junit (Java based) and Nunit(.Net based).
All testing tool implemented as plug-in for Eclipse , but Junit is a default plug-in for
Eclipse,no need to install Junit in Eclipse.
In case of development ,TestNG will be used to implement unit test case.
Each Unit test case tests the business logic of the source code.
MAVEN /ANT is a build testing tool , whcih is used to check the compilation issues in
the entire source code.
3
Installation steps of TestNG Framework
Go to Eclipse window
Click on Help --> Eclipse Market place
You will get Eclipse market place (Internet connection should be there)
In Eclipse market place window,write TestNG in find box and go
It will connect to internet and search TEST NG
Click on Install button for TEST NG for Eclipse.
Click on the radio button "I accept the terms of license agreement" and
click on Finish.
Restart your Eclipse.
Note:-If your Eclipse has not inbuilt TestNG plug-in; then please get
the latest version using the update site.
In your Eclipse IDE, select Help / Software updates / Find and Install
4
Installation steps of TestNG Framework
5
Installation steps of TestNG
6
Installation steps of TestNG
7
Installation steps of TestNG
8
Installation steps of TestNG
9
Installation steps of TestNG
10
Installation steps of TestNG
11
Installation steps of TestNG
• Inorder to cross verify TESTNG installation
• Go to Window -->Show View -->Other -->New window will pop-up -->Expand Java Folder
and find TESTNG symbol.
12
How to Import TESTNG
• Inorder to use TESTNG features in Eclipse ,make sure TESTNG available for the project.
• Go to Configure build path from window-->Click on add library -->Click on Next -->Click
• on Finish
13
How to Import TESTNG
14
How to Import TESTNG
15
How to Import TESTNG
16
Annotation
• Annotation is a meta-data which provides instruction to TestNG compiler at the time of
execution .
• List of Annotation
@Test
@BeforeMethod
@AfterMethod
@BeforeClass
@AfterClass
@BeforeGroup
@AfterGroup
@BeforeSuit
@AfterSuit
17
Tips for writing test cases in TestNG
• For writing TestNG test cases follow below 4 steps .
Step 1:-Create TestNG class
Select package -> Right click ->Come down to TestNg option ->Create TestNG class
Step 2:-Implement TestNg test cases,within a class
Example:- public class NewTest{
@Test
public void createUserTest()
{
System.out.println(“Execute create user Test”);
}
}
Step 3:-Execute TestNG class
Select TestNG class --> Right Click -->Run as -->TestNG Test
Step 4:-Verify TestNG ,html report
18
@Test
• When we execute TestNG class, TestNG compiler always look for @Test annotation
• to start the execution.
• Return type of TestNG method should be void and access specifier should be public.
• One TestNG class can have multiple test cases or test methods,but each test method
should have @annotation,before the method signature .
• @Test annotation method is used to implement test case.
Step 1:-Create TestNG class
Select package -> Right click ->Come down to TestNg option ->Create TestNG class
19
@Test
Example:- public class NewTest{
@Test
public void createUserTest()
{
System.out.println(“Execute create user Test”);
}
}
20
@BeforeMethod and @AfterMethod
@AfterMethod
Public void configAfterMethod()
{
System.out.println(“Execute after test”);
}
}
22
@BeforeClass and @AfterClass
• BeforeClass annotation is executed before executing first test casein the class.
• AfterClass annotation is executed after executing all the test cases in the class.
• Before and after class annotation are used to implement global configuration
• which is common to all test cases.
• As per the automation tool, test cases should be independ from each other and
• each and every test case should be unique.
• Example:-
package selenium;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class UserTest{
23
Program using all annotations
@BeforeClass
public void configBeforeClass()
{
System.out.println("---Launch Browser and intiate variables----");
}
@BeforeMethod
public void configBeforeMethod()
{
System.out.println("Login");
}
@Test
public void createUserTest(){
System.out.println("Create and verify User");
}
24
Program using all annotations
@Test
public void createUserTest(){
System.out.println("Create and verify User");
}
@Test
public void modifyUserTest(){
System.out.println("Create and modify and verify user");
}
@AfterMethod
public void configAfterMethod(){
System.out.println("Log out");
}
@AfterClass
public void configAfterClass(){
System.out.println("Close the browser , disconnect it");
} 25
}
Report Generation in TestNG
26
Report Generation in Eclipse console
[TestNG] Running:
---Launch Browser and intiate variables----
Login
Create and verify User
Log out
Login
Create and modify and verify user
Log out
Close the browser , disconnect it
PASSED: createUserTest
PASSED: modifyUserTest
27
HTML Report Generation
28
Assertion
3)assertFalse():-If the status of the variable is false , pass the test case or else fail it.
Syntax:-Assert.assertFalse(“status”,error message);
30
How to set priority in TestNG
• Whenever we execute TestNG class by default TestNG always execute the test based on
alphabetical orders of test method name.
• Inorder to execute the test in user define order, either go for priority or depends on
method.
• By setting priority
Package pac1;
Import org.testng.anntotations.Test;
Public class FlipkartOrderTest{
@Test (priority =1)
Public void orderTest(){
System.out.println(“Execute order test “);
}
@Test(priority=2)
Public void billingTest(){
System.out.println(“Execute billing test”);
}
} 31
How to set priority in TestNG
• Output:-
Execute order test
Execute billing test
• Dependson Method
Package pac1;
Import org.testng.anntotations.Test;
Public class FlipkartOrderTest{
@Test
Public void orderTest(){
System.out.println(“Execute order Test”);
}
@Test(dependsOnMethods=”modifyTest”)
Public void billingTest(){
System.out.println(“Execute billing Test”);
}
32
How to set priority in TestNG
@Test(dependsOnMethods=”orderTest”);
Public void modifyTest()
{
System.out.println(“Execute modify test”);
}
}
• Output
Exeucte order Test
Execute modify Test
Execute billing test
33
Advantages of TestNG
• It generate logs
• We can do parallel testing
• Annotations helps to set program/function priority easy
• Allow to generate HTML report of execution
• We can group test cases.
Let’s take a scenario where we have created two set of groups “Regression” & “Sanity”.
If we want to execute the test cases under Sanity group then it is possible in TestNG
framework.
• We can set test cases priorities.
• We can do data Parameterization
34
Thank You
35