ITestListener

16 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.60
List Price:  €92.30
You save:  €27.69
£55.40
List Price:  £79.14
You save:  £23.74
CA$95.14
List Price:  CA$135.92
You save:  CA$40.78
A$107.21
List Price:  A$153.17
You save:  A$45.95
S$94.29
List Price:  S$134.71
You save:  S$40.41
HK$547.55
List Price:  HK$782.26
You save:  HK$234.70
CHF 63.39
List Price:  CHF 90.56
You save:  CHF 27.17
NOK kr753.41
List Price:  NOK kr1,076.34
You save:  NOK kr322.93
DKK kr481.90
List Price:  DKK kr688.47
You save:  DKK kr206.56
NZ$116.54
List Price:  NZ$166.49
You save:  NZ$49.95
د.إ257.02
List Price:  د.إ367.19
You save:  د.إ110.16
৳7,699.83
List Price:  ৳11,000.23
You save:  ৳3,300.40
₹5,834.28
List Price:  ₹8,335.05
You save:  ₹2,500.76
RM331.26
List Price:  RM473.25
You save:  RM141.99
₦97,755.03
List Price:  ₦139,656.03
You save:  ₦41,901
₨19,509.87
List Price:  ₨27,872.43
You save:  ₨8,362.56
฿2,546.30
List Price:  ฿3,637.73
You save:  ฿1,091.43
₺2,255.86
List Price:  ₺3,222.80
You save:  ₺966.93
B$348.35
List Price:  B$497.67
You save:  B$149.31
R1,321.80
List Price:  R1,888.37
You save:  R566.56
Лв126.46
List Price:  Лв180.67
You save:  Лв54.20
₩94,455.35
List Price:  ₩134,942
You save:  ₩40,486.65
₪256.23
List Price:  ₪366.06
You save:  ₪109.83
₱3,937.46
List Price:  ₱5,625.18
You save:  ₱1,687.72
¥10,582.69
List Price:  ¥15,118.78
You save:  ¥4,536.09
MX$1,164.09
List Price:  MX$1,663.06
You save:  MX$498.96
QR255.79
List Price:  QR365.44
You save:  QR109.64
P961.07
List Price:  P1,373.02
You save:  P411.94
KSh9,203.68
List Price:  KSh13,148.68
You save:  KSh3,945
E£3,306.33
List Price:  E£4,723.53
You save:  E£1,417.20
ብር3,986
List Price:  ብር5,694.53
You save:  ብር1,708.53
Kz58,526.40
List Price:  Kz83,612.73
You save:  Kz25,086.33
CLP$68,613.99
List Price:  CLP$98,024.19
You save:  CLP$29,410.20
CN¥505.72
List Price:  CN¥722.49
You save:  CN¥216.77
RD$4,149.44
List Price:  RD$5,928.03
You save:  RD$1,778.58
DA9,428.56
List Price:  DA13,469.95
You save:  DA4,041.39
FJ$157.67
List Price:  FJ$225.25
You save:  FJ$67.58
Q547.27
List Price:  Q781.84
You save:  Q234.57
GY$14,690.75
List Price:  GY$20,987.69
You save:  GY$6,296.93
ISK kr9,671.91
List Price:  ISK kr13,817.61
You save:  ISK kr4,145.70
DH707.37
List Price:  DH1,010.57
You save:  DH303.20
L1,234.61
List Price:  L1,763.81
You save:  L529.19
ден3,973.74
List Price:  ден5,677.01
You save:  ден1,703.27
MOP$565.34
List Price:  MOP$807.66
You save:  MOP$242.32
N$1,327.13
List Price:  N$1,895.98
You save:  N$568.85
C$2,582.33
List Price:  C$3,689.21
You save:  C$1,106.87
रु9,350.11
List Price:  रु13,357.88
You save:  रु4,007.76
S/259.70
List Price:  S/371.02
You save:  S/111.31
K268.50
List Price:  K383.60
You save:  K115.09
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,783.85
List Price:  ZK2,548.47
You save:  ZK764.61
L321.17
List Price:  L458.83
You save:  L137.66
Kč1,633.99
List Price:  Kč2,334.38
You save:  Kč700.38
Ft25,496.77
List Price:  Ft36,425.52
You save:  Ft10,928.74
SEK kr741.62
List Price:  SEK kr1,059.50
You save:  SEK kr317.88
ARS$59,963.93
List Price:  ARS$85,666.43
You save:  ARS$25,702.49
Bs486.54
List Price:  Bs695.09
You save:  Bs208.54
COP$272,760.10
List Price:  COP$389,673.99
You save:  COP$116,913.89
₡35,355.24
List Price:  ₡50,509.65
You save:  ₡15,154.41
L1,731.96
List Price:  L2,474.33
You save:  L742.37
₲515,624.88
List Price:  ₲736,638.54
You save:  ₲221,013.66
$U2,646.58
List Price:  $U3,781
You save:  $U1,134.41
zł278.71
List Price:  zł398.17
You save:  zł119.46
Already have an account? Log In

Transcript

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.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.