import geb.testng.GebReportingTest;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import ru.yandex.qatools.allure.annotations.Attachment;
public class ScreenshotListener extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult failingTest) {
try {
GebReportingTest test = (GebReportingTest) failingTest.getInstance();
createAttachment(test.getBrowser().getDriver());
} catch (Exception ex) {
System.err.println("Unable to capture screenshot...");
ex.printStackTrace();
}
}
@Attachment(type = "image/png")
private byte[] createAttachment(WebDriver driver) throws Exception {
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
}
Now it seems it runs only of I annotate a test class with @Listeners(ScreenshotListener.class)
To make it use globally I should probably annotate GebReportingTest class but I do not have access to it and extending it now seems really troubling. This is the only way I managed onTestFailure method to run correctly. Is there any other?
Regards
Mateusz