TestNG manages everything through suite, tests methods and the listeners gives us the ability to act before and after of every suite, test, methods.
Types of listener
There are many types of listeners available in TestNg for example IAnnotation Transformer,IAnnotation Transformer2, IConfigurable, IConfigurationListener,IconfigurationListener2,IExecutionListener,IHoookable,IInovokedMethodLisner,IInovokedMethodLisner2,IMethodInterceptor, IReporter,IsuiteListener,ITestListener.
ISuiteLister: It has two method in it onStart() and onFinish(). Whenever a class implements this listener,tesng guaranteesthe end-user that it will invokethe methods on start() and onFinish()before and after running a testNg suite.
ITestListener: The working of this listener is also exactly the same as ISuiteListener but the only difference is that it makes the call before and after the test. Is has seven methods in it.
IInvokedMethodListener: The working of this listener is also exactly the same as ISuiteListener & ITestListener and the only difference is that it makes the call before and after every Method. It has only two methods in it.
For example:
Implement the interface methods: ISuiteLister ,ITestListener,IInvokedMethodListener.
Create a test case, a xml file and a class to implement the ISuiteLister ,ITestListener,IInvokedMethodListener.
Test case "ExampleListener"
The testng.xml file
Execute the testng.xml file
Another example:
We take a screenshot when the test fails.
In our listener class we will add a method that takes a screenshot and we must call within "onTestFailure" method because when an error occurs,onTestFailure method will execute, so we take the screenshot.
We also create a new class by our webDriver
This is our test case.
our test was successfully executed, but we change the button name to fail the test.
Refresh the project and will appear the image the screenshot
















No hay comentarios:
Publicar un comentario