Hello all welcome back to the next one. In this one, we'll take a look at the next listener, which is I test listener. So let me just bring up Eclipse and let's get started. So I still have my test and G dash listeners dot XML file. And in the listeners packet, let's create another class. So or maybe you know what, I'll just copy this.
So I've selected the file Ctrl C, or Command C, and then Ctrl C or Command V to copy it. And then we can name it as custom listener to and click OK. And then we will change this file definitely. So I want to delete these two methods before invocation and after invocation. And I want to delete this also because we will not implement the same interface in this one in this lecture. So we will implement eye test and then control space. And then let's filter listener.
So I test listener, do not use IE test nj listener do not get confused, we will use eye test listener from our dot test engine. And then we will implement the unimplemented methods. So now we have all these methods on test start on test, success on test failure, and then on test escaped and on test failed, but within success percentage, and on start and on finish. So, there are a lot of methods inside this package. So, inside this interface, so let me just delete the unused import packages. So, let's understand these methods and let's use them let's try to use them so we get a feel of it.
The first one we hear See is on test start, right. So this method is executed when any test method starts. So I'm gonna write it. So we learn it when test method starts. And by test method, we mean the actual test method with the ad the rate test annotation. So you know what, let's side by side create a test class also.
So test in G underscore listeners test one, I'm gonna copy this and name it as listeners test to click OK. So we have a test class also right now, it is using a previous listener. So we will change it to custom listener to dot class. And from here also we will change the input statement so it stops complaining right now. Now take a look at it. We have a lot of have different kinds of methods inside the test class, we have the setter method with before class annotation cleanup method bill after class annotation and then actual test methods with ADD delete test annotation.
So on test start method, this one the one we are overriding here, this only executes when test method starts. So, when the actual after a test method starts, it will not execute before the other eight before class or at the rate after class or the other at the read before and after method. So, it will only run on the test methods. So, basically this is the test start one let's print a line as well. So we understand it So, see So, I'm going to print the name of this method so we know from where the print statement is coming and maybe we will say so, best Name. And now, we will just use the result dot getname method.
So this will give us the name of the test method, right. So after on test start we have on test success. So the name is very obvious for this right? This will run when the test method is successful. That's very, very easy to understand. So, and we're printing the name of the test, so we will know from where it's coming.
And let's add a comment. So when test method is, or maybe we can say if this method is successful, that's it. This is pretty easy to understand. Same thing as this on test. Failure method, this will execute if the test method is fail. So I'm just copying these lines.
So we can save some typing time here. And it's, it's almost the same thing that we want to print here. So the only thing we will change here is if this is failed, right? After this, we have on test skip. It's again, the same thing. If the test method is skip, currently, I'm not printing anything, it's almost gonna be the same thing because we're not going to skip any test method right now.
But if you have some test cape and you want to do something, then put your logic inside this method on test skip method. There's one more method which is kind of, actually I don't use it, this is not, I don't find it very useful. Also, only in some places you might find it useful depending on your applicant. But this one says on test fail, but within success percentage. So this means that the test is fail. But you might have some some kind of scenario where you may say that I have a criteria where I have six assertion points inside one test method maybe.
And if five of them are successful, then I still want the test case to be successful. So that kind of thing, which we usually do not do in testing in automation. If something is failed, then we just want to say it's fail, because we want it to be properly debug. And if something is failing, we want to isolate it and put it in a separate test case. So we don't mess up with everything else, right? So I don't really find it very useful.
That's why I usually say that just ignore this one because I don't want to use it. And I don't want people to get confused with this. So I would just say ignore this. Now, we have on Start. So we can see that this starts before ad before the test tag of XML files. So there is a difference in this and on test start the on test start method will execute when the test method start, the test method is add the rate test annotation.
But the on start method this will execute before the test tag of XML file. So in our XML file, we can have multiple test tags right. Now let's just copy this and have one more. So I'm gonna say this is application test one. And this is application test two. And in the second test, I'll call the test listeners to class.
So in the first test tag, I'm calling test listeners one class and the second one. I'm calling the test listeners. listeners test to class right? So basically now I have two test tags. And in the custom listener we have on start and on finish. So let's just copy this.
Okay, I think I should copy this System dot out dot print ln to this as well. And also, I should copy the comment, so we know what's happening. So this one, we will say, it happens before. Test tag. So I'm gonna write it so you understand the difference test tag of XML file. This XML file, it does not work on test method on test tag.
Now we'll just say this is from OnStar. And this is the Just dag name and beacon use. So the name of the variable is context, we can use context, dot get name. So now since this is my context, I test context. And this is a test result, this will give us the name of the test method. And context dot get name will give us the name of the test stack.
That's, that's really easy. It's, it's really smart. Now I'm gonna actually what we should do here is, I'm just taking an example what we can do inside these methods. So maybe inside on start, I may want to print all the names of methods that exist in this class, or maybe all the classes because we might have more than one class here. So maybe we want to just say that on test tag start, I want to print the name of all the methods inside that stick test tag. So what we can do here is we can actually get the name of all the test methods.
So I'm going to use that Test and D method. This is the type. And we will say let's say methods and this is an array. So we define an array equals context dot get all test methods. So there is a method provided by test ng D, which is get all test methods, it will get us the name of all the test methods. In here we want to import it test entry method from our dot test and now, I'll put in maybe something which is meaningful.
So we'll say these methods will be executed in this test tag. Right, something like that. And then we can do a for loop. And the for loop I'll state I test and D method. And method inside all the methods because this is the array we defined. And then we can just simply print the method name.
So I'm gonna say method. So this variable I'm using, this is just a for each loop, very simple. dot get method name. And this will print all the method names. So this is kind of one use of onstart method. And basically, this is kind of one example that I'm showing.
But it may depend on your application, what you want to do. The whole purpose of understanding is the concept that what exactly we want to do on test start, what exactly we want to do on test is when test is successful, what exactly we want to do when test is failed. Maybe you have a web application and you want to take screenshots when something has failed. So maybe this was a one of the credit example for that, right. So there are several implementations that you may want to use these things well So on finish is again same thing it executes when actually after the test tag of the XML file. So instead of before we can just say after, and we can simply print this one.
So this is coming from on finish, and the name of the test tag will be printed. That's about it. So we have kind of implemented all the methods, it's all done. And this test engine listeners too, we are listening to custom listener to class and test engine listeners here. so here also I want to actually use the same class test listener class because listeners test one class is listening to some other listener and we don't want to change the existing code. The same methods will be run anyway, the the methods are same in both the classes.
The whole thing we want to understand is how the listeners who are called so That's about it here. So now, let's just try to run the listeners XML file. So right click and run as test in G Suite. So many things happen right and one thing very important because we are using the custom listener and we are using these methods on start on finish and they are from the test tag and XML file, we have to use the XML file, we cannot just right click and run run the test file because those methods will not be executed. So the code was run let's go over everything in detail. First thing what happened was on start was run write the test take tag name is application test one.
And then in on start being implemented the name it threats, the names of the methods right. So these methods will be executed in these tests tag and we wrote this. It got the name Mr. Bowden methods test method one and method two. And then Gordon before class executed, and then on test start executed because then test method one was about to execute, then core inside test method one was executed. And this was successful also on test success executed. And the name of the test method is test method one, the non test star executed for test method two, then the code inside this executed test method two and take a look here on test failure got executed and test method two why that happened.
Because in test method two we are asserting true and we are providing false here. So, it's obviously gonna fail and we did it intentionally, because we wanted to show that when the test method is successful on test success will be called. And if the test method is fail on test failure will be called So, not everything will run every time. It depends on the event that is happening and that's By these things our listeners, right, they are very clever, very intelligent. Then coding after class executed, then on finish executed for the test tag one, which is application test one. Here we can see that.
And then everything in similar way will start for application tests to the test acto. So let's take a look at the output. So OnStar executed for application pesto, the same things will be executed and on finish executed here at the end. So that's the benefit of these listeners. And these might be one good replacement of things that we put in the base test class. I mean, not everything can be replaced.
But definitely these are very smart things and we can use these instead of having the before suite or before test in our base test class. But it again, depends on the practice that you want to follow, and kind of application kind of functionality that you have but it's a very Good feature. Test engine listeners are really, really smart. And we can get the name of the class name of the method from where it's getting called. So we can use everything and that's about it. That's what I wanted to talk in this one.
Please download the code files. Practice as much as possible. And let me know if you have any questions I'll see in the next lectures. Thanks for attending this one.