Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Tuesday, 9 December 2014

What are Listners in Testng

@Listner in Testng 

This is one very good feature in Testng. if you choose to have some default behavior for your test case when it fails, passes etc. For Eg : if you extend TestListenerAdapter class and write your implementation in a custom listner to override onTestFailure or onTestSuccess methods you will be able to do  anything which you want when a test fails or passes, like taking screenshots, writing something to database, sending emails(Report generation) etc. 

Lets say your test fails and you want to rerun the test case automatically one more time when it fails. In that case you will be writing a custom listener to achieve what you need and plug the listener to your test case via @Listener annotation. 

Lets say your test fails and you want to take snapshot of fail  test case automatically In that case you will be writing a custom listener to achieve what you need and plug the listener to your test case via @Listener annotation. 


//Above is the description but now i will explain what exactly they do.

In Testng TestListenerAdapter is a class You can override  any of the methods in which are given in TestListemerAdaptor class.
Suppose your test case is passed and you want to take snapshot at that case just you need to override a method onTestSuccess(ITestResult tr)  and just you need to write your snapshot code here.By this whenever your test case is passed this function will execute and you can see snapshot.

Suppose your test case is failed and you want to generate reports at that case just you need to override a method onTestFailure(ITestResult tr) and just you need to write (How to genrate reports)code here.By this whenever your test case is failed this function will execute and you can see snapshot.


Same activity will be done by all of the functions according to their function name



How to implement TestNG Listener



  • Create a simple class named as CustomListner and extends TestListenerAdapter



After extending this class you can override some  methods.Here i am using three methods one pass,skipped and fail.
//Here is the code 

package listner;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class Customlistner extends TestListenerAdapter{
//Override some methods 
public void onTestFailure(ITestResult tr) 
{
System.out.println("When testcase is failed generate reports ");
}
public void onTestSuccess(ITestResult tr)  {
System.out.println("When testcase is pass Take snapshot");
}
public void onTestSkipped(ITestResult tr) {
System.out.println("Testcase is skipped do some activity");
}

}

//After using some of these methods just you need to Create a simple class named as LoginTest

 Make a new class named as LoginTest in which your testcases are running and  write the @Testng annotations and create two Testcases in this class

package listner;

import org.testng.Assert;
import org.testng.annotations.Test;

public class LoginTest {
@Test
public void loginwithvalidusername(){
System.out.println("Login with valid username");
}
@Test
public void loginwithinvalidusername(){
System.out.println("Login with invalid username");
//Use assertion and delibrately failed this
Assert.assertEquals("Equal", "NotEqual");
}

}
// Run this class run this class with help of xml.
//Copy this xml into your project and named as Testng.xml
//This is the xml file for this

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Sample Suite">
  <listeners>
    <listener class-name="listner.Customlistner" />
  </listeners>
<test name="LoginTest">
    <classes>
        <class name="listner.LoginTest" ></class>  
    </classes>
</test>
</suite>
Now run this class>Go to testng.xml

Whenever Testcase is failed then failedfunction called By listner or Whenever testcase passed this passed function will called. 
  

8 comments:

  1. best one, which i have come across till now :)

    ReplyDelete
  2. Excellent Post..I am a regular contributor of your blog. I have gathered some needful information from this blog. Keep update your blog. Waiting for your next update.
    Regards..
    Software Testing Training in Chennai | Software Testing Training in Chennai

    ReplyDelete
  3. I really got an information which is useful and informative and also i like to share information about the sap abap training in Chennai with experienced staffs and provided with practical classes.


    abap training in chennai

    ReplyDelete
  4. informatic blog..

    SEO training in hyderabad by experts in digital markeing And by prosessional experts in seo.All the training by placement and also guide by the professionals.Best SEO training in hyderabad

    ReplyDelete

Popular Posts

 
subscribe
Subscribe Us
emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: