001// Copyright 2009, 2010, 2012 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.test; 016 017import com.thoughtworks.selenium.CommandProcessor; 018import org.testng.ITestContext; 019 020import java.io.File; 021import java.io.FileWriter; 022import java.io.IOException; 023import java.lang.reflect.Method; 024import java.util.ArrayList; 025import java.util.HashSet; 026import java.util.List; 027import java.util.Set; 028 029public class ErrorReporterImpl implements ErrorReporter 030{ 031 private final CommandProcessor commandProcessor; 032 033 private final ITestContext testContext; 034 035 private int uid = 0; 036 037 private final Set<String> previousNames = new HashSet<String>(); 038 039 private final List<File> outputPaths = new ArrayList<File>(); 040 041 public ErrorReporterImpl(CommandProcessor commandProcessor, ITestContext testContext) 042 { 043 this.commandProcessor = commandProcessor; 044 this.testContext = testContext; 045 } 046 047 public void writeOutputPaths() 048 { 049 if (outputPaths.isEmpty()) 050 { 051 return; 052 } 053 054 System.err.println("Page captures written to:"); 055 056 for (File file : outputPaths) 057 { 058 try 059 { 060 System.err.println(" " + file.getCanonicalPath()); 061 } catch (IOException e) 062 { 063 // Ignored. Like, what's going to happen? 064 } 065 } 066 067 } 068 069 @Override 070 public void writeErrorReport(String reportText) 071 { 072 String htmlSource = commandProcessor.getString("getHtmlSource", new String[] 073 {}); 074 075 File dir = new File(testContext.getOutputDirectory()); 076 077 dir.mkdirs(); 078 079 Method testMethod = (Method) testContext.getAttribute(TapestryTestConstants.CURRENT_TEST_METHOD_ATTRIBUTE); 080 081 String baseFileName = testMethod == null ? "Unknown-test" : testMethod.getDeclaringClass().getSimpleName() 082 + "." + testMethod.getName(); 083 084 if (previousNames.contains(baseFileName)) 085 { 086 baseFileName += "-" + uid++; 087 } else 088 { 089 previousNames.add(baseFileName); 090 } 091 092 File report = new File(dir, baseFileName + ".txt"); 093 094 System.err.println("Writing failure report to: " + report); 095 096 writeContent(report, reportText); 097 098 File capturedSource = new File(dir, baseFileName + ".html"); 099 100 System.err.println("Writing current page's HTML source to: " + capturedSource); 101 102 writeContent(capturedSource, htmlSource); 103 104 File capture = new File(dir, baseFileName + ".png"); 105 106 System.err.println("Writing current page screenshot to: " + capture); 107 108 try 109 { 110 commandProcessor.doCommand("captureEntirePageScreenshot", new String[] 111 {capture.getAbsolutePath(), "background=white"}); 112 113 outputPaths.add(capture); 114 } catch (Exception ex) 115 { 116 System.err.println(ex.getMessage()); 117 } 118 } 119 120 private void writeContent(File file, String content) 121 { 122 try 123 { 124 FileWriter fw = new FileWriter(file); 125 126 fw.write(content); 127 128 outputPaths.add(file); 129 130 fw.close(); 131 } catch (IOException ex) 132 { 133 // Ignore. 134 } 135 } 136 137}