continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r899077 - /continuum/branches/continuum-1.3.x/continuum-webapp-test/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java
Date Thu, 14 Jan 2010 06:43:48 GMT
Author: brett
Date: Thu Jan 14 06:43:47 2010
New Revision: 899077

URL: http://svn.apache.org/viewvc?rev=899077&view=rev
Log:
capture HTML if screenshot can't be taken

Modified:
    continuum/branches/continuum-1.3.x/continuum-webapp-test/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java

Modified: continuum/branches/continuum-1.3.x/continuum-webapp-test/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java
URL: http://svn.apache.org/viewvc/continuum/branches/continuum-1.3.x/continuum-webapp-test/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java?rev=899077&r1=899076&r2=899077&view=diff
==============================================================================
--- continuum/branches/continuum-1.3.x/continuum-webapp-test/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java
(original)
+++ continuum/branches/continuum-1.3.x/continuum-webapp-test/src/test/testng/org/apache/continuum/web/test/listener/CaptureScreenShotsListener.java
Thu Jan 14 06:43:47 2010
@@ -20,18 +20,28 @@
  */
 
 import java.io.File;
+import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.regex.Pattern;
 
 import org.apache.continuum.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
 {
     @Override
+    public void onTestSkipped( ITestResult tr )
+    {
+        System.out.println( "Test " + tr.getName() + " -> Skipped" );
+        super.onTestSkipped( tr );
+    }
+
+    @Override
     public void onTestFailure( ITestResult tr )
     {
         captureError( tr );
@@ -48,40 +58,38 @@
 
     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[]
)



Mime
View raw message