Tutorial 11:TestNG with intelliJ IDE| Listeners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome you all in this video I am talking about test ng listeners and this is also one of the important feature from test ng so let's start with water test ng listeners so basically while automating your test cases right so normally we have a test case so test case contains a number of test methods for us a test method one and it's a test method two and so on so after execution after exhibiting this particular test case so maybe these test methods maybe pass or fail or sometimes they may be skipped right so based on the status of these test methods I want to perform set enforced actions so for example this test method is passed or this method is failure and some other method is there so that is also skip so based upon this result of this test method I want to execute or I want to perform certain action so this is called post actions so if you want to perform these post actions based on the result of these just methods I need to use listeners concept from taps trenching so these actions can be performed only through test ng listeners methods so to implement this particular thing so test ng provided different type of interfaces and classes and again those interfaces and classes contains a lot of P defined methods and those methods we have to integrate with our test cases so first of all let us see what is listener so listener is a listener is a kind of a interface or set of methods and those methods will be automatically triggered based upon the result of your test methods right so there's a basically concept of test Angeles nurse so what are the different types of listeners we have so we have different types of listeners we have so before going to this this is I have just captured the screen shot and you can understand how the listeners works suppose if any test case is failure and we can perform some set in action on suppose pass or we can perform some other action something like this or skipped or started so based upon those test case status we can perform sit in action that can be done by using listeners so when you come to the types of listeners so I test the listener so there are so many types of listeners we have but I test listener is a very popularly used listener so let's go to a test ng official website and intestine G official website just go to here and go to documentation and here if we can see test in G listeners and here I test means these are the different types of business case so I annotation transformation I annotation transformer and hi you couple or I reporter I suit list now these are other different types of listener classes are available so this is a very commonly used laser at last I test listener so let's go to Java dot here and here they have given lots of things so what are the different methods are available inside this listener so an test start on test success on this failure aren't escape and so on so these are the different methods are available inside this particular I test listener and basically this is a interface a Java interface so we have to implement these methods we have to implement these methods whatever methods they have provided and basically these methods are abstract method so we have to implement them according to our requirement so let's see how we can implement this listeners for your test cases so as we understood these methods will be executed based upon your status of your test methods right so let's implement this practically first of all just go to indle AJ and Here I am going to create a new test case first and then I will create test ng methods and then we will see how to associate them so I'll create a new package under SRC and let's take a new package and I will name it as listeners then okay and inside this package I will create a new test case and I am named this as law again just name it as listener test and inside this test I am going to implement three different methods which is representing three different statuses like first method I will make it pass other method I will make this failure and the other one is capable so three different statuses I am going to show you now so let's create one method called wide just one and now inside this method I'll write a small message I can just say system dot out dot println and here this is just one I am printing and I want to make this just method pass so by default if I don't have any asset command so by default it will be passed or else you can just put one asset command I'll set the dot assert equals and here I can say something called a I want to compare with a so both are equal right so obviously my test method will pass so I will define this with the rate test annotation from testing G all right so this is one test method which will be passed according to this asset command and now I will create one more test method so let's say test 2 and this is your test 2 and here I will make it as failure so I can just say be here so both are not equals so obviously your test method will be failed and I will also create one more test method I want to skip it so here I'll say it as 3 this is your test 3 and I want to skip this so if you want to skip this particular method you have to just write some through exception so if you want to skip this method I can just write through new skip exception sorry through new skip exception I can through it escape exception and you can just specify some name so let's say so let this is this is skip exception ok some something happen type here so basically what this statement will do is this statement will skip this part alert test method right so now I have created three different test methods one is pass one will be failure and one will be skip let's execute this test case alone right click run lays not test so if you run this Butler test case so we can see the how these test measures are executed so now I can see the test one is passed test two is failure and test C not part of here in the structure because test three is skipped so if you want to see clearly status here you can see so select this suit completes your sample please not test and here you can see so this is test one this test two is printed and here test two is failure so it is giving the failure may log here and test three message is printed but that is ignore right so now these are the three different statuses we got it from these three test methods now based on the status of these just methods I want to execute certain number of post actions so that can be done by using test engine listeners so what I can do is I can just create one more listener class here let's right click here new Java class then I will name it as custom fill listener then okay so whenever I'm creating this class I need to implement a test result I test result interface right so where is your I test result interface so I test result interface is here so as I said so all these methods are come front or I test all is the interface so this interface contains all these method on start on finish on test failure on to escape on to start on to success let's see when these methods will be triggered so on start so by default these method will be taken some parameters like I test context and I test result so these messages because these objects will hold the statuses of your test methods so when these methods will be executed so on start method invoked after the test class is instantiated and before any configuration method is called and on finish invoked after all the tests have run and all their configuration methods have been called contest failure will invoke each time when it tests failed so whenever a test fails immediately this particular method will be triggered and whenever it test method is kept immediately on task method will be triggered and similarly whenever a test method is started then on test start method will be triggered then if any method is passed then aren't as successful be trigger so these methods automatically trigger so let's implement these methods now so we have to implement these methods so that we need to implement I test tool is not these are all abstract methods so here I'm using a concept of linear it is less implements I test listener so as soon as we have done this we have to implement all the methods which are coming from this particular I test regional so the first method is we have to use exactly the same definition we should not get our own definition so let us say a public void on start so this is the first method so this will take ieave test context as a parameter and for that we need to just create one argument just create an arc means let us say our G some argument is required right inside this method I'll just write a simple message let's say system dot out dot println starts testing you so this part of our method will start only one time before exhibiting all the S so here I'm using this R because I want to just if you want to display the suitum you can just use it on start method or else we do not require so just I am printing the name all right and similarly I will also implement other methods so this particular method will execute here I'm putting a comment this belongs to I test listener and will execute before starting off test set or batch all right similarly I will also implement other method that is public void on finish this will also take the same parameter and ultimate and I can say argument or get name so this particular method will give you the name of the test method so whichever test method right so on finish so unfinished will execute after starting off test or test test set or batch so this will execute before and this will execute after completion of all the test matters right so on start and on finish now come to the method level so every method starting or sometimes you will start and maybe pass failure skip so how we can handle these cases so here we have another method called on test start so this will also take a test result as parameter a test result and argument 0 and here I'll say system not order quintal and starts test some message I'm brightening here this is on test start and we have another method called on test skipped so this is another method called on test skipped so this will also take a parameter I test result argument and here internal printing skipping test so but actually we have to write some action steps here whenever a test method is failure or skip or passed so normally we will write the code here to send the logs to your reports and send the status messages to the report right so this is on test skip so the last method we have on test success this is another method so this particular method will execute as soon as your test method is passed and finally we have another method called on test to failure so this method will be executed as soon as your test is failure so these are most important contest start on test skip on test success on test failure so these methods will be executed whenever your test case status is pass or whenever it test method is started on test our Twi'leks so whenever your test is skip then skip will execute then whenever your test method is passed or successful then on text success method will execute then if any test method is failure then aren't as failure will be executed so there is also an one more method is available so that is not much important so I can just leave this message as it is I am NOT going to implement this but we have to define this because even though if you are not implementing this body we have to define that method because I test Lesnar is an interface whenever you are trying to implement this interface we must address all the methods within the class okay so we must address all the within that we must address all the methods within the class even though we haven't implemented those methods so there's a reason I kept this Butler method as it is so now this is your listener class so these methods will be triggered automatically but how it will happen so how we can integrate this listener test clap test case and this customer is not your Java so to associate these two classes we need to add one parameter at the test case level so listener test so here I'll add one parameter at the class level so here I need to add at the rate listeners and this is annotation which we need to add at the class level so at the rate listeners and here we can specify the in which package our test list customer is not class is available specify that I guess name so my package name is listeners dot custom listeners right so this is my listener class I created so dot custom listeners dot it will show you here so custom business dot this is a class so we need to add this particular annotation at that class level so then whenever these methods are executing starting or passing or failure or skipping immediately the method which are defined in the custom listener class will be executed so let's execute this test case now run listener test right so now he started all right so now it is executed now we can see so one test is failure one test pass one is ignored right but what about the messages from act from listener class so see this these are the messages we got it from listener class key so starting test so test one is started and passed and again starting touched two and printed that message and it is failure so here it is giving the failure message and if we come down again test three started so this is these messages coming from test Angeles and this is the message of test Creek method and finally this is also skipped so these messages are coming from listener class case so as soon as my test is started or pass or fail or skipped so immediately the listener class methods have been triggered and they have executed and the first two methods like on start on finish so these two methods exhibit only one time so an start is exhibited only one time here so before exhibiting all that s and then finally on finish is also executed only one time after completion of all the tests right so this is how we can just use this listener class inside your test case and by adding this as at the rate listener sanitation we can use we can associate the listener class with your test case so this is one way of integrating your listener class with your test case class now using there is another way we can also integrate using test ng XML file so let me also show you that particular approach let me just close this and I will create an XML file so what I can do is take this class listener test and take create a new test range XML okay now sing this project then it will create a new test and XML file so I'm just reformatting this so this is your test ang XML file which is generated which contains the three different methods right and for now I am just removing these methods so that's fine so now this is your class I am going to execute so if I run this Butler XML file normal test case will X good but listener class will not export so if you want to integrate test case along with the listener class using XML file rights you don't have to specify this Butler annotation in your test so this is not required so if you want to integrate both using XML file right so I am just commenting this for now so now listener test is different and custom regional it's not Java is different so now I need to integrate them using XML file so in XML file anyway listener tests will execute so now I need to add one more tag here to integrate the listener class so before test is started so here I will add a new tag called listeners ok so inside this listener start out toward one more tag called listener and the class name attribute you have to specify so where is my listener class is present under listeners package dot custom listener so this is my package right so this is a new tag which we need to add case so this is only new tag which we need to add into the XML file so now you can run this XML file and it will also trigger all the methods from sorry this is custom listener right so please nurse a dot custom listeners right so as soon as I have executed this XML file along with this listener test class parallely this listener class methods also will be triggered so this is another approach so using this particular annotation at the rail is another annotation we can integrate listener class or we can also integrate within XML file so let's execute this XML file now we got all we will also get the same results all right so now again we got the same thing so can start a test execution this on start message this is on test start message and this output of that method test method and this is our on the success message output and again started message from test2 and test two is failure so we got the log message here and test 3 test to fail and test3 started and just three is printed and it is ignored because this is skipped so these messages are coming from listener class right so based upon the test results we can trigger different methods and in those methods we need to write the post actions so post actions means after execution of your test methods what should be done so should be part of your test ng listeners matters okay so this is another very useful interface we have in testing G so by which we can create this listener we can implement these listener class methods according to our requirement all right so these are there are two methods I have told you so first one is we talk without having XML file we can just add this listener annotation at the class level is specify the listener class and the other one is through XML file we can integrate and in XML file we need to add a new listener stack and insert this original tag here we have to add one attribute called class - name and here we specify the listener class alright so that's okay so there's all for this video so thanks for watching
Info
Channel: SDET- Automation Techie
Views: 8,284
Rating: 4.9436622 out of 5
Keywords: Python, Selenium with Python, Selenium webdriver, Automation, Testing tools, Selenium tutorials, Python tutorials, Manual testing, SDET, Java, selenium with java, big data, web services, postman, rest assured, big data testing, ETL testing, SQL, Programming, gcreddy, Software Testing material, selenium videos, python videos, automation vidios, jira, jmeter, performance testing, functional testing, QA, QC, Agile testing, SDLC, Listeners
Id: a16fQs69H3k
Channel Id: undefined
Length: 20min 50sec (1250 seconds)
Published: Sun Apr 07 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.