TestNG:
Unit testing framework which supports java and .net
TestNG = Junit + Nunit
TestNG for developers:
-> Developers will make use of TESTNG for Unit testing of the source code
TestNG for Automation Testers:
-> Automation Testers will make use of TESTNG for developing the test scripts
in a more optimised way.
Annotations in TestNG:
-> @Test
-> @BeforeMethod
-> @AfterMethod
-> @BeforeClass
-> @AfterClass
-> @BeforeTest
-> @AfterTest
-> @BeforeSuite
-> @AFterSuite
-> @DataProvider
-> @Listeners
-> @Parameters
Annotations in TestNG
=> Basic Configuration Annotations - ReturnType is void
@Test:--- act like a main method, which is identified by JVM to start the execution
@BeforeSuite: It is executed before the <suite> tag in the suite xml file
It is executed only once per execution as there will be only one
<suite>
It is used for establishing database connection.
@AfterSuite: It is executed after the closing of suite tag </suite> in suite xml
file
It is executed only once per execution as there will be only one
</suite>
It is used for closing database connection.
@BeforeTest: It is executed before the opening of <Test> in suite xml file
The number of times it will execute depends on number <Test> tags
This is mostly used for parallel executions as it create multiple
threads
@AfterTest: It is executed after the closing of test tag </Test> in suite xml file
The number of times it will execute depends on number </Test> tags
This is mostly used for parallel executions as it create multiple
threads
@BeforeClass: It will execute before opening of every class <class> in suite xml
file
or simply we can tell before evry test class
The number of times it will execute depends on the number of <Class>
or Test class
It is used for launching browser
@AfterClass: It will execute after closing of every class </Class> in suite xml
file
or simply we can tell after evry test class
The number of times it will execute depends on the number of </Class>
or Test class
It is used for closing browser
@BeforeMethod: It will execute before every @Test annotation
The number of times it will execute depends on number of @Test
It is used for login to Application
@AfterMethod: It will execute after every @Test annotation
The number od times it will execute depends on number of @Test
It is used for logout of Application
=> To change the execution order of @Test annotations inside a test class: priority
in testNg
@Test(priority = int)
-> Lowest priority will execute first
-> Default priority will be 0
-> Negetive priorities are allowed
=> To run the same test script/ @Test mutiple Times:invocation count in TestNG
@Test(invocationCount = int)
-> Default invocation count is 1
-> If we want to run the same test script more than once then provide invocation
count
-> If invocation count is given 0 or negetive values, Then that @Test will not be
executed
we can give both invocation count and priority for the same test script
@Test(invocationCount = 4, priority = 2) - priority is given first preference
=> To make the execution of one test script depend on the status(pass/fail) of
other test sript
- DependsOnMethods in TestNG
@Test(dependsOnMethods = "method name")
- if one test script should depend on execution status of multiple test scripts
like
@Test(dependsOnMethods = {"method name 1","method name 2"})
=> To disable a prticular test script/@Test : enabled in testNG
@Test(enabled = false)
-> Enabled is feature which accepts boolean(true/false)
-> Default value for enabled is true,
-> If you want to disable the test script execution give enabled = false.
Priority:
When ever we execute testNg class , by default all the test methods will be
executed based on Alphabetical order, in order to change the order of Execution, we
go for priority.
public class Sample {
@Test(priority = 0)
public void createContact()
{
[Link]("contact created");
}
@Test(priority = -1)
public void modifyContact()
{
[Link]("modify contact");
}
@Test(priority = 1)
public void deleteContact()
{
[Link]("delete contact");
}
}
modify contact
contact created
delete contact
PASSED: createContact
PASSED: modifyContact
PASSED: deleteContact
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
-----------------------------------------------------------------------------------
---------------------------------------
Depends on method:
Its help us to check the dependent test script is pass or fail.
If dependent test script get pass, execution will continue
if dependent test-script get fail, skip all other test script which is dependent on
first test.
public class Sample {
@Test
public void createContact()
{
[Link]("contact created");
@Test(dependsOnMethods = "createContact")
public void modifyContact()
{
[Link]("modify contact");
}
@Test(dependsOnMethods = "createContact")
public void deleteContact()
{
[Link]("delete contact");
}
}
[RemoteTestNG] detected TestNG version 7.4.0
contact created
delete contact
modify contact
PASSED: modifyContact
PASSED: createContact
PASSED: deleteContact
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
-----------------------------------------------------------------------------------
--------------------------------------
public class Sample {
@Test
public void createContact()
{
.
[Link](arr[5]);
[Link]("contact created");
@Test(dependsOnMethods = "createContact")
public void modifyContact()
{
[Link]("modify contact");
}
@Test(dependsOnMethods = "createContact")
public void deleteContact()
{
[Link]("delete contact");
}
}
===============================================
Default test
Tests run: 3, Failures: 1, Skips: 2
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 0, Failures: 1, Skips: 2
===============================================
-----------------------------------------------------------------------------------
--------------------------------------
Invocation count:
Same test-script executed with multiple times with same test data.
public class Sample {
@Test
public void createContact()
{
[Link]("contact created");
}
@Test
public void modifyContact()
{
[Link]("modify contact");
}
@Test(invocationCount = 2)
public void deleteContact()
{
[Link]("delete contact");
}
}
contact created
delete contact
delete contact
modify contact
PASSED: deleteContact
PASSED: createContact
PASSED: deleteContact
PASSED: modifyContact
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
-----------------------------------------------------------------------------------
---------------------------------------
Batch Execution:
Executing all the test script one after the another is called Batch Exceution
To achive any kind of suite execution we requried [Link] file.
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="FrameWork.Properties_file"/>
<class name="[Link]"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
-----------------------------------------------------------------------------------
---------------------------------------
Parallel Execution:Executing all the TestScript in a same browser at same time with
different instance is called parallel
Execution.
<!DOCTYPE suite SYSTEM "[Link]
<suite parallel="tests" name="Suite">
<test thread-count="5" parallel="tests" name="Test">
<classes>
<class name="[Link]"/>
<class name="FrameWork.Properties_file"/>
</classes>
</test>
<test name="Test1">
<classes>
<class name="[Link]"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
-----------------------------------------------------------------------------------
-------------------------------------
Assertion:-
Assertion is a feature available in TestNg used to validate the TestScripts
expected Results.
Types of Assertion
1)Hard Assert
2)Soft Assert
-HardAssert:-When ever hard Assert method fails. testNG generate AssertError
Execption and stop the current test Execution and continue execution with
remaining test.
public class HardAssert {
@Test
public void createCustomer()
{
[Link]("step1");
[Link]("step2");
[Link](false, true);
[Link]("step3");
[Link]("step4");
}
@Test
public void m1()
{
String expName="shobha";
String actuName="shobha";
[Link](expName, actuName);
}
}
[RemoteTestNG] detected TestNG version 7.4.0
step1
step2
PASSED: m1
FAILED: createCustomer
[Link]: expected [true] but found [false]
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
===============================================
Example 2
public class HardAssert {
@Test
public void createCustomer()
{
[Link]("step1");
[Link]("step2");
[Link](true, true);
[Link]("step3");
[Link]("step4");
}
@Test
public void m1()
{
String expName="shobha";
String actuName="shobhaRani";
[Link](expName, actuName);
}
}
[RemoteTestNG] detected TestNG version 7.4.0
step1
step2
step3
step4
PASSED: createCustomer
FAILED: m1
[Link]: expected [shobhaRani] but found [shobha]
===============================================
Default test
Tests run: 2, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 2, Passes: 1, Failures: 1, Skips: 0
===============================================
-----------------------------------------------------------------------------------
--------------------------------------
2)SoftAssert:-When ever SoftAssert fails, TestNg Generates Assert Error Exception
and continue Execution with remaining STeps of same TestScripts.
package FrameWork;
import [Link];
import [Link];
public class SoftAssertEx {
@Test
public void m1()
{
[Link]("Step1");
[Link]("Step2");
SoftAssert soft = new SoftAssert();
[Link](false, true);
[Link]("Step3");
[Link]("Step4");
[Link]();
}
@Test
public void m2()
{
String exp = "Shobha";
String act = "Shobha ";
SoftAssert soft = new SoftAssert();
[Link](act, exp);
[Link]();
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ScreenShotEx {
@Test
public void m1() throws Throwable
{
WebDriver driver=new ChromeDriver();
[Link]("[Link]
TakesScreenshot screen = (TakesScreenshot)driver;
File src = [Link]([Link]);
File dest = new File("./[Link]");
[Link](src, dest);
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------
HardAssert :-ALl the methods are static in nature
-When ever the Assertion Fails stops the Execution in same line and
throw the Exception
-Used for all the madatory field
-AssertAll() is not madantory
SoftAssert:- All the methods are non-static in nature
-When ever the Assertion Fails Continues the Execution at the end
Throws the Exception
-Used for all the non-madatory field
-AssertAll() is mandatory
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------
package FrameWork;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ScreenShotEx {
@Test
public void method() throws Throwable
{
WebDriver driver=new ChromeDriver();
[Link]("[Link]
[Link]().window().maximize();
TakesScreenshot ts = (TakesScreenshot)driver;
File src = [Link]([Link]);
File dest = new File("./[Link]");
[Link](src, dest);