Introduction to TestNG in Selenium

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.

Today we are focusing on Test NG framework; we will touch its key components which will mentor us to use its capabilities with Selenium.
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.

Do share our learning by liking our Facebook Page,Google+,LinkedIn or Twitter.

What is TestNG?
TestNG(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. 

TestNG 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.

Creator:

Cedric Beust founder of TestNG
Cedric Beust @Cheers for TestNG


TestNG was created by Cedric Beust.

To get more enlighten about him:- listen to Cedric Beust interview.

Why to Use TestNG in Selenium?
1. It provides reporting mechanism which was missing in Selenium.
2. Annotation provides better control on the flow of the Execution. Like setting priority for test to Run.
3. Uncaught exceptions are handled, rather than stopping the execution and failures are reported in the results.
4. We can run failed test case by simply using testng.xml no need to run full test suite in case of failure

Key feature list of TestNG:-

  • 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

Annotations:-
In laymen terms it is a comment attached to a particular section of a code.
Now when compiler interprets this comment it controls the flow of execution. 

So 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.

Syntax:-
@Name 

Example:-
@BeforeTest.

Some important annotations:-
Annotation
Meaning
@BeforeSuite
This annotation method will execute before all tests in this suite, example
Creating the WebDriver Instance
@AfterSuite
This annotation method will execute after all tests in this suite, example
Destroying the WebDriver object
@BeforeTest
This annotation method will execute before any test method belonging to the classes inside the Test tag is executed
@AfterTest
This annotation method will execute after all the test methods belonging to the classes inside the Test tag have executed.
@BeforeClass
This annotation method will execute before the first test method in the current class is invoked
@AfterClass
This annotation method will execute after all the test methods in the current class have been executed.
@BeforeMethod
This annotation method will execute before each test method.
@AfterMethod
The annotated method will be executed after each test method.
@Parameters
It Describes how to pass parameters to a @Test method.
@Listeners
Used in defining listeners on a test class.
@DataProvider
This method is used in supplying data for a test method

Benefits:-
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.
2) We can pass additional parameters to annotations, like @Test (priority=1)
3) Annotations are strongly typed, making compiler to show the failures.


Data Parameterization
Running the same test with different set of Data. For example, login to the same application but with multifarious user credentials.

How to achieve it?
1.Using Parameter Annotation and TestNG xml file.
Example: – @Parameters (“UserName”) and defining the value of UserName in .xml file.
2. Using DataProvider Annotation
Example: – @DataProvider (name = “UserName”)
Listeners
TestNG 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.
TestNG defines a @Listeners annotation that is analogous to the listener’s element in the test suite configuration xml file.

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.

One thought on “Introduction to TestNG in Selenium

Leave a Reply

Your email address will not be published. Required fields are marked *