Introduction to TestNG in Selenium – User friendly Tech help
In our last tutorials we covered the basic aspects of Selenium. Now we are moving towards intermediate phase so that we can inculcate framework level things in Selenium execution.
n
n
Today we are focusing on Test NG framework; we will touch its key components which will mentor us to use its capabilities with Selenium.
n
Our Philosophy “The best way to increase happiness is to share it with Others, And the best way to increase learning is to share with others“.
n
nDo share our learning by liking our Facebook Page,Google+,LinkedIn or Twitter.
n
n
What is TestNG?
nTestNG(Next Generation) is an open source testing framework written in Java and inspired from JUnit and NUnit, it is not only inherited existing functionality from Junit as well as introducing some new innovative functionality that make it powerful, easy to use, reliable, maintainable and testable codes.
n
nTestNG is emerging as a Testing platform, which is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc. we can take full advantage of TestNG from engineering to quality insurance.
n
nCreator: –n
Cedric Beust @Cheers for TestNG |
n
nTestNG was created by Cedric Beust.
nTo get more enlighten about him:- listen to Cedric Beust interview.
n
n
Why to Use TestNG in Selenium?
n1. It provides reporting mechanism which was missing in Selenium.
n2. Annotation provides better control on the flow of the Execution. Like setting priority for test to Run.
n3. Uncaught exceptions are handled, rather than stopping the execution and failures are reported in the results.
n4. We can run failed test case by simply using testng.xml no need to run full test suite in case of failure
n
Key feature list of TestNG:-
n
- n
- Annotations.
- HTML reports of Execution.
- Logs
- Support for parameters
- Support for multi-threaded testing
- Data Provider (Data driven Testing)
- Supported by a variety of tools and plug-ins (Eclipse, Maven, etc…).
- Listeners
n
n
n
n
n
n
n
n
n
Annotations:-
nIn laymen terms it is a comment attached to a particular section of a code.
nNow when compiler interprets this comment it controls the flow of execution.
n
nSo we can say Annotations in TestNG are lines of code that can control the flow of execution of an attached method, class, field and other program elements.
n
n
Syntax:-
n@Name
n
n
Example:-
n@BeforeTest.
n
Some important annotations:-
n
n
Annotation n |
n
Meaning n |
n
@BeforeSuite n |
n
This annotation method will execute before all tests in this suite, example n Creating the WebDriver Instance n |
n
@AfterSuite n |
n
This annotation method will execute after all tests in this suite, example n Destroying the WebDriver object n |
n
@BeforeTest n |
n
This annotation method will execute before any test method belonging to the classes inside the Test tag is executed n |
n
@AfterTest n |
n
This annotation method will execute after all the test methods belonging to the classes inside the Test tag have executed. n |
n
@BeforeClass n |
n
This annotation method will execute before the first test method in the current class is invoked n |
n
@AfterClass n |
n
This annotation method will execute after all the test methods in the current class have been executed. n |
n
@BeforeMethod n |
n
This annotation method will execute before each test method. n |
n
@AfterMethod n |
n
The annotated method will be executed after each test method. n |
n
@Parameters n |
n
It Describes how to pass parameters to a @Test method. n |
n
@Listeners n |
n
Used in defining listeners on a test class. n |
n
@DataProvider n |
n
This method is used in supplying data for a test method n |
n
Benefits:-
n
1) TestNG identifies the methods it is interested in by looking up annotations,we don’t need to add Static or Main method in our code. Hence method names are not restricted to any pattern or format.
n
2) We can pass additional parameters to annotations, like @Test (priority=1)
n
3) Annotations are strongly typed, making compiler to show the failures.
n
Data Parameterization
nRunning the same test with different set of Data. For example, login to the same application but with multifarious user credentials.
n
n
How to achieve it?
n
1.Using Parameter Annotation and TestNG xml file.
n
Example: – @Parameters (“UserName”) and defining the value of UserName in .xml file.
n
2. Using DataProvider Annotation
n
Example: – @DataProvider (name = “UserName”)
n
nHow to achieve cross browser testing in Selenium?
n
Listeners
nTestNG listeners is like an interfaces that allows extending of TestNG behaviour. It provides a way to call event handlers inside custom listener classes, and makes it possible to do certain operations in the TestNG execution cycle.
n
TestNG defines a @Listeners annotation that is analogous to the listener’s element in the test suite configuration xml file.
n
n
When running TestNG tests, one could want to perform some common actions – after each test has finished successfully, after each failed test, after each skipped test, or after all the tests have finished running, no matter their result. To apply such a common behaviour to a group of tests, a custom listener can be created, that implements TestNG’s ITestListener interface.
n
Read More:-
nDownload and install TestNG
nFirst Test Case in TestNG
n
n