セレンでテストが失敗したときにスクリーンショットを撮る必要があります。私のすべてのテスト クラスは AbstractTestNGSpringContextTests を拡張し、TestWithSeleniumDriver を実装し、注釈 @Listeners(ScreenshotForFailedTestListener.class) を持っています。
public interface TestWithSeleniumDriver {
public RemoteWebDriver getDriver();
}
public class ScreenshotForFailedTestListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
// nothing
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
if (method.isTestMethod() && ITestResult.FAILURE == testResult.getStatus()) {
if (method.getTestResult().getInstance() instanceof TestWithSeleniumDriver) {
TestWithSeleniumDriver instance = (TestWithSeleniumDriver) method.getTestResult().getInstance();
RemoteWebDriver driver = instance.getDriver();
if (driver != null) {
TestingUtils.captureScreen(driver, method);
}
}
}
}
}
public class TestingUtils {
private static final Logger logger = LogManager.getLogger(TestingUtils.class);
public static String captureScreen(RemoteWebDriver driver, IInvokedMethod method) {
String path;
try {
Throwable throwable = method.getTestResult().getThrowable();
String testClass = method.getTestMethod().getRealClass().getName();
String packageName = method.getTestMethod().getRealClass().getPackage().getName();
String shortClassName = method.getTestMethod().getRealClass().getSimpleName();
String testMethod = method.getTestMethod().getMethodName();
StackTraceElement stackTraceElement = null;
for (StackTraceElement traceElement : throwable.getStackTrace()) {
if (traceElement.getClassName().equals(testClass) && traceElement.getMethodName().equals(testMethod)) {
stackTraceElement = traceElement;
break;
}
}
WebDriver augmentedDriver = new Augmenter().augment(driver);
File source = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
String drvName = "unknown";
if (driver instanceof FirefoxDriver)
drvName = "firefox";
else if (driver instanceof ChromeDriver)
drvName = "chrome";
else if (driver instanceof OperaDriver)
drvName = "opera";
// else if ( driver instanceof AndroidDriver )
// drvName = "android";
else if (driver instanceof RemoteWebDriver)
if (driver.getCapabilities().getBrowserName() != null)
drvName = driver.getCapabilities().getBrowserName();
String day = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String hour = new SimpleDateFormat("HH-mm-ss").format(new Date());
if (stackTraceElement == null) {
path = ("./target/screenshots/" + drvName + "/" + "failed" + "/" + //
day + "/" + //
packageName + "/" + shortClassName + "/" + testMethod + "_noLineInfo_" + //
driver.getCurrentUrl().replaceAll("https://", "").replaceAll("/", "_") + "_" + //
hour + ".png").replaceAll("__", "_");//
} else {
path = ("./target/screenshots/" + drvName + "/" + "failed" + "/" + //
day + "/" + //
packageName + "/" + shortClassName + "/" + testMethod + "_line-" + stackTraceElement.getLineNumber() + "_" + //
driver.getCurrentUrl().replaceAll("https://", "").replaceAll("/", "_") + "_" + //
hour + ".png").replaceAll("__", "_");//
}
FileUtils.copyFile(source, new File(path));
String stackTrace = ExceptionUtils.getStackTrace(throwable);
logger.error("===========================");
logger.error("screnshot captured to : " + path);
logger.error("===========================");
} catch (IOException e) {
path = "Failed to capture screenshot: " + e.getMessage();
}
return path;
}
}
コードはニーズに合わせて少し調整する必要があります