From 560d3e0ebca9170c4756a6f8a02c926369f8f93a Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Thu, 14 Jan 2010 03:42:01 +0000 Subject: [PATCH] if capturing a screenshot fails, save the HTML git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@899049 13f79535-47bb-0310-9956-ffa450edef68 --- .../listener/CaptureScreenShotsListener.java | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-webapp-test/src/test/testng/org/apache/archiva/web/test/listener/CaptureScreenShotsListener.java b/archiva-modules/archiva-web/archiva-webapp-test/src/test/testng/org/apache/archiva/web/test/listener/CaptureScreenShotsListener.java index 4e6ed3182..46ebbfeaa 100644 --- a/archiva-modules/archiva-web/archiva-webapp-test/src/test/testng/org/apache/archiva/web/test/listener/CaptureScreenShotsListener.java +++ b/archiva-modules/archiva-web/archiva-webapp-test/src/test/testng/org/apache/archiva/web/test/listener/CaptureScreenShotsListener.java @@ -20,13 +20,16 @@ package org.apache.archiva.web.test.listener; */ import java.io.File; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; import org.apache.archiva.web.test.parent.AbstractSeleniumTest; +import org.apache.commons.io.FileUtils; import org.testng.ITestResult; import org.testng.TestListenerAdapter; +import com.thoughtworks.selenium.Selenium; public class CaptureScreenShotsListener extends TestListenerAdapter @@ -48,40 +51,38 @@ public class CaptureScreenShotsListener private void captureError( ITestResult tr ) { - try - { - captureScreenshot( tr ); - } - catch ( RuntimeException e ) - { - System.out.println( "Error when take screenshot for test " + tr.getName() ); - e.printStackTrace(); - } - } - - // captureAssertionError() creates a 'target/screenshots' directory and saves '.png' page screenshot of the - // encountered error - private void captureScreenshot( ITestResult tr ) - { - File f = new File( "" ); - String filePath = f.getAbsolutePath(); - Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" ); - String time = sdf.format( d ); - String fs = File.separator; - File targetPath = new File( filePath + fs + "target" + fs + "screenshots" ); - targetPath.mkdir(); - String cName = tr.getTestClass().getName(); + String time = sdf.format( new Date() ); + File targetPath = new File( "target", "screenshots" ); StackTraceElement stackTrace[] = tr.getThrowable().getStackTrace(); + String cName = tr.getTestClass().getName(); int index = getStackTraceIndexOfCallingClass( cName, stackTrace ); String methodName = stackTrace[index].getMethodName(); int lNumber = stackTrace[index].getLineNumber(); String lineNumber = Integer.toString( lNumber ); String className = cName.substring( cName.lastIndexOf( '.' ) + 1 ); - String fileName = - targetPath.toString() + fs + methodName + "(" + className + ".java_" + lineNumber + ")-" + time + ".png"; - AbstractSeleniumTest.getSelenium().windowMaximize(); - AbstractSeleniumTest.getSelenium().captureEntirePageScreenshot( fileName, "" ); + targetPath.mkdirs(); + Selenium selenium = AbstractSeleniumTest.getSelenium(); + String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time; + try + { + selenium.windowMaximize(); + File fileName = new File( targetPath, fileBaseName + ".png" ); + selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "" ); + } + catch ( RuntimeException e ) + { + System.out.println( "Error when take screenshot for test " + tr.getName() + ": " + e.getMessage() ); + try + { + File fileName = new File( targetPath, fileBaseName + ".html" ); + FileUtils.writeStringToFile( fileName, selenium.getHtmlSource() ); + } + catch ( IOException ioe ) + { + System.out.println( ioe.getMessage() ); + } + } } private int getStackTraceIndexOfCallingClass( String nameOfClass, StackTraceElement stackTrace[] ) -- 2.39.5