1 package org.apache.archiva.web.test.listener;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.io.IOException;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.regex.Pattern;
28 import org.apache.archiva.web.test.parent.AbstractSeleniumTest;
29 import org.apache.commons.io.FileUtils;
30 import org.testng.ITestResult;
31 import org.testng.TestListenerAdapter;
32 import com.thoughtworks.selenium.Selenium;
34 public class CaptureScreenShotsListener
35 extends TestListenerAdapter
38 public void onTestSkipped( ITestResult tr )
40 System.out.println( "Test " + tr.getName() + " -> Skipped" );
41 super.onTestSkipped( tr );
45 public void onTestFailure( ITestResult tr )
48 System.out.println( "Test " + tr.getName() + " -> Failed" );
49 super.onTestFailure( tr );
53 public void onTestSuccess( ITestResult tr )
55 System.out.println( "Test " + tr.getName() + " -> Success" );
56 super.onTestFailure( tr );
59 private void captureError( ITestResult tr )
61 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
62 String time = sdf.format( new Date() );
63 File targetPath = new File( "target", "screenshots" );
64 StackTraceElement stackTrace[] = tr.getThrowable().getStackTrace();
65 String cName = tr.getTestClass().getName();
66 int index = getStackTraceIndexOfCallingClass( cName, stackTrace );
67 String methodName = stackTrace[index].getMethodName();
68 int lNumber = stackTrace[index].getLineNumber();
69 String lineNumber = Integer.toString( lNumber );
70 String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
72 Selenium selenium = AbstractSeleniumTest.getSelenium();
73 String fileBaseName = methodName + "_" + className + ".java_" + lineNumber + "-" + time;
76 selenium.windowMaximize();
77 File fileName = new File( targetPath, fileBaseName + ".png" );
78 selenium.captureEntirePageScreenshot( fileName.getAbsolutePath(), "" );
80 catch ( RuntimeException e )
82 System.out.println( "Error when take screenshot for test " + tr.getName() + ": " + e.getMessage() );
85 File fileName = new File( targetPath, fileBaseName + ".html" );
86 FileUtils.writeStringToFile( fileName, selenium.getHtmlSource() );
88 catch ( IOException ioe )
90 System.out.println( ioe.getMessage() );
95 private int getStackTraceIndexOfCallingClass( String nameOfClass, StackTraceElement stackTrace[] )
97 boolean match = false;
101 String className = stackTrace[i].getClassName();
102 match = Pattern.matches( nameOfClass, className );
105 while ( match == false );