Allure. How to get screenshot on testFailure event. Use Java+Junit4+Maven -
if want screenshot when tests failed, best practice is? try next way:
1)overridre allurerunlistener:
public class simplescreenshottestlistener extends allurerunlistener{ @override public void testfailure(failure failure) { if (failure.getdescription().istest()) { firetestcasefailure(failure.getexception()); } else { startfaketestcase(failure.getdescription()); firetestcasefailure(failure.getexception()); finishfaketestcase(); } makescreenshot("failure screenshot"); } }
the method makescreenshot("failure screenshot") static method in util class:
public final class util { private util() {} @attachment(value = "{0}", type = "image/png") public static byte[] makescreenshot(string name) { return ((takesscreenshot) <thread local driver>).getscreenshotas(outputtype.bytes); } }
3) in pom file use created listener simplescreenshottestlistener:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-surefire-plugin</artifactid> <version>2.18.1</version> <configuration> <testfailureignore>false</testfailureignore> <argline> -javaagent:${settings.localrepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar </argline> <properties> <property> <name>listener</name> <value>cms.firefox.tps.simplescreenshottestlistener</value> </property> </properties> </configuration> <dependencies> <dependency> <groupid>org.aspectj</groupid> <artifactid>aspectjweaver</artifactid> <version>${aspectj.version}</version> </dependency> </dependencies> </plugin>
and question : way best way or should easier.
just use junit rules following:
public class screenshotonfailurerule implements testrule { public statement apply(final statement statement, final description description) { return new statement() { @override public void evaluate() throws throwable { try { statement.evaluate(); } catch (throwable t) { capturescreenshot(); throw t; } } @attachment private byte[] capturescreenshot() { try { return ((takesscreenshot)driver).getscreenshotas(outputtype.bytes); } catch (exception e) { // no need crash tests if screenshot fails } } }; } }
so far capturescreenshot() method run on failure allure attach resulting png byte stream test case. further reading rules.
Comments
Post a Comment