Created
October 20, 2015 11:30
-
-
Save juherr/600edc68c32ba6a3f419 to your computer and use it in GitHub Desktop.
TestNG: No need of listener by class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.testng.Assert; | |
import org.testng.annotations.BeforeMethod; | |
import org.testng.annotations.Listeners; | |
import org.testng.annotations.Test; | |
@Listeners(ScreenshotMaker.class) | |
public class BeforeScreenshotTest implements Screen { | |
public String getDriver() { | |
return "driver of BeforeScreenshotTest"; | |
} | |
@BeforeMethod | |
public void beforeMethod() { | |
Assert.fail("BOUM"); | |
} | |
@Test | |
public void test() { } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public interface Screen { | |
String getDriver(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.testng.IInvokedMethod; | |
import org.testng.IInvokedMethodListener; | |
import org.testng.ITestResult; | |
public class ScreenshotMaker implements IInvokedMethodListener { | |
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { | |
} | |
public void afterInvocation(IInvokedMethod method, ITestResult testResult) { | |
if (testResult.getStatus() == ITestResult.FAILURE) { | |
Object testInstance = testResult.getInstance(); | |
if (testInstance instanceof Screen) { | |
Screen screen = (Screen) testInstance; | |
System.out.println("after invocation of " +testResult.getName()); | |
System.out.println("\t" + screen.getDriver()); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.testng.Assert; | |
import org.testng.annotations.Listeners; | |
import org.testng.annotations.Test; | |
@Listeners(ScreenshotMaker.class) | |
public class ScreenshotTest implements Screen { | |
public String getDriver() { | |
return "driver of ScreenshotTest"; | |
} | |
@Test | |
public void test() { | |
Assert.fail("BOUM"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting. I didn't know applying listeners with annotation applies the listener once for whole suite. This is actually the cleanest way to add a screenshot listener I've seen so far! GJ and Thanks