Last active
June 1, 2016 15:15
-
-
Save koenusz/63f8c131a1fc2592b485e005bf4418f7 to your computer and use it in GitHub Desktop.
JGiven Screenshot on testfailure
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 sym.sequence.test.functional.util; | |
import com.tngtech.jgiven.attachment.Attachment; | |
import com.tngtech.jgiven.attachment.MediaType; | |
import com.tngtech.jgiven.base.ScenarioTestBase; | |
import com.tngtech.jgiven.report.ReportGenerator; | |
import com.tngtech.jgiven.report.model.ScenarioCaseModel; | |
import com.tngtech.jgiven.report.model.StepModel; | |
import org.junit.runner.Description; | |
import org.junit.runner.Result; | |
import org.junit.runner.notification.Failure; | |
import org.junit.runner.notification.RunListener; | |
import org.openqa.selenium.OutputType; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.List; | |
import java.util.Optional; | |
public class TestExecutionListener extends RunListener { | |
Logger logger = LoggerFactory.getLogger(TestExecutionListener.class); | |
private ScenarioTestBase test; | |
public void init(ScenarioTestBase base){ | |
test = base; | |
} | |
private void addScreenshot() | |
{ | |
List<ScenarioCaseModel> models = test.getScenario().getScenarioModel().getScenarioCases(); | |
ScenarioCaseModel lastCase = models.stream().reduce((first, second) -> second).get(); | |
Optional<StepModel> lastStep = lastCase.getSteps().stream().reduce((first, second) -> second); | |
if(lastStep.isPresent()) { | |
String base64 = SeleniumUtil.getDriver().getScreenshotAs(OutputType.BASE64); | |
Attachment screenshot = Attachment.fromBase64(base64, MediaType.PNG).withTitle("Screenshot"); | |
lastStep.get().setAttachment(screenshot); | |
} | |
} | |
@Override | |
public void testRunStarted(Description description) throws Exception { | |
logger.debug("Number of tests to execute: " + description.testCount()); | |
} | |
@Override | |
public void testRunFinished(Result result) throws Exception { | |
logger.debug("Number of tests executed: " + result.getRunCount()); | |
try { | |
ReportGenerator.main("--format=html", "--sourceDir=jgiven-reports", "--targetDir=jgiven-reports"); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
} | |
@Override | |
public void testStarted(Description description) throws Exception { | |
logger.debug("Starting: " + description.getMethodName()); | |
} | |
@Override | |
public void testFinished(Description description) throws Exception { | |
logger.debug("Finished: " + description.getMethodName()); | |
} | |
@Override | |
public void testFailure(Failure failure) throws Exception { | |
logger.debug("Failed: " + failure.getDescription().getMethodName()); | |
addScreenshot(); | |
} | |
@Override | |
public void testAssumptionFailure(Failure failure) { | |
logger.debug("Failed: " + failure.getDescription().getMethodName()); | |
} | |
@Override | |
public void testIgnored(Description description) throws Exception { | |
logger.debug("Ignored: " + description.getMethodName()); | |
} | |
} |
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 sym.sequence.test.functional.util; | |
import com.tngtech.jgiven.base.ScenarioTestBase; | |
import org.junit.runner.notification.RunNotifier; | |
import org.junit.runners.model.InitializationError; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
public class TestRunner extends SpringJUnit4ClassRunner { | |
private TestExecutionListener executionListener; | |
private ScenarioTestBase test; | |
public TestRunner(Class<?> klass) throws InitializationError { | |
super(klass); | |
} | |
@Override | |
protected Object createTest() throws Exception | |
{ | |
Object test = super.createTest(); | |
if(test instanceof ScenarioTestBase && this.test == null) | |
{ | |
executionListener.init ((ScenarioTestBase) test); | |
} | |
return test; | |
} | |
@Override | |
public void run(RunNotifier notifier) { | |
TestExecutionListener listener = new TestExecutionListener(); | |
this.executionListener = listener; | |
notifier.addListener(listener); | |
try { | |
listener.testRunStarted(getDescription()); | |
} catch (Throwable t) { | |
t.printStackTrace(); | |
} | |
super.run(notifier); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment