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.text.SimpleDateFormat;
24 import java.util.Date;
25 import java.util.regex.Pattern;
27 import org.apache.archiva.web.test.parent.AbstractSeleniumTest;
28 import org.testng.ITestResult;
29 import org.testng.TestListenerAdapter;
31 public class CaptureScreenShotsListener
32 extends TestListenerAdapter
35 public void onTestFailure( ITestResult tr )
38 System.out.println( "Test " + tr.getName() + " -> Failed" );
39 super.onTestFailure( tr );
43 public void onTestSuccess( ITestResult tr )
45 System.out.println( "Test " + tr.getName() + " -> Success" );
46 super.onTestFailure( tr );
49 private void captureError( ITestResult tr )
53 captureScreenshot( tr );
55 catch ( RuntimeException e )
57 System.out.println( "Error when take screenshot for test " + tr.getName() );
62 // captureAssertionError() creates a 'target/screenshots' directory and saves '.png' page screenshot of the
64 private void captureScreenshot( ITestResult tr )
66 File f = new File( "" );
67 String filePath = f.getAbsolutePath();
69 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy.MM.dd-HH_mm_ss" );
70 String time = sdf.format( d );
71 String fs = File.separator;
72 File targetPath = new File( filePath + fs + "target" + fs + "screenshots" );
74 String cName = tr.getTestClass().getName();
75 StackTraceElement stackTrace[] = tr.getThrowable().getStackTrace();
76 int index = getStackTraceIndexOfCallingClass( cName, stackTrace );
77 String methodName = stackTrace[index].getMethodName();
78 int lNumber = stackTrace[index].getLineNumber();
79 String lineNumber = Integer.toString( lNumber );
80 String className = cName.substring( cName.lastIndexOf( '.' ) + 1 );
82 targetPath.toString() + fs + methodName + "(" + className + ".java_" + lineNumber + ")-" + time + ".png";
83 AbstractSeleniumTest.getSelenium().windowMaximize();
84 AbstractSeleniumTest.getSelenium().captureEntirePageScreenshot( fileName, "" );
87 private int getStackTraceIndexOfCallingClass( String nameOfClass, StackTraceElement stackTrace[] )
89 boolean match = false;
93 String className = stackTrace[i].getClassName();
94 match = Pattern.matches( nameOfClass, className );
97 while ( match == false );