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 org.openqa.selenium.OutputType; 018import org.openqa.selenium.TakesScreenshot; 019import org.openqa.selenium.WebDriver; 020import org.testng.ITestContext; 021 022import java.io.File; 023import java.io.FileWriter; 024import java.io.IOException; 025import java.lang.reflect.Method; 026import java.nio.file.Files; 027import java.util.ArrayList; 028import java.util.HashSet; 029import java.util.List; 030import java.util.Set; 031 032public class ErrorReporterImpl implements ErrorReporter 033{ 034 private final WebDriver webdriver; 035 036 private final ITestContext testContext; 037 038 private int uid = 0; 039 040 private final Set<String> previousNames = new HashSet<String>(); 041 042 private final List<File> outputPaths = new ArrayList<File>(); 043 044 public ErrorReporterImpl(WebDriver webdriver, ITestContext testContext) 045 { 046 this.webdriver = webdriver; 047 this.testContext = testContext; 048 } 049 050 public void writeOutputPaths() 051 { 052 if (outputPaths.isEmpty()) 053 { 054 return; 055 } 056 057 System.err.println("Page captures written to:"); 058 059 for (File file : outputPaths) 060 { 061 try 062 { 063 System.err.println(" " + file.getCanonicalPath()); 064 } catch (IOException e) 065 { 066 // Ignored. Like, what's going to happen? 067 } 068 } 069 070 } 071 072 @Override 073 public void writeErrorReport(String reportText) 074 { 075 String htmlSource = webdriver.getPageSource(); 076 077 File dir = new File(testContext.getOutputDirectory()); 078 079 dir.mkdirs(); 080 081 Method testMethod = (Method) testContext.getAttribute(TapestryTestConstants.CURRENT_TEST_METHOD_ATTRIBUTE); 082 083 String baseFileName = testMethod == null ? "Unknown-test" : testMethod.getDeclaringClass().getSimpleName() 084 + "." + testMethod.getName(); 085 086 if (previousNames.contains(baseFileName)) 087 { 088 baseFileName += "-" + uid++; 089 } else 090 { 091 previousNames.add(baseFileName); 092 } 093 094 File report = new File(dir, baseFileName + ".txt"); 095 096 System.err.println("Writing failure report to: " + report); 097 098 writeContent(report, reportText); 099 100 File capturedSource = new File(dir, baseFileName + ".html"); 101 102 System.err.println("Writing current page's HTML source to: " + capturedSource); 103 104 writeContent(capturedSource, htmlSource); 105 106 File capture = new File(dir, baseFileName + ".png"); 107 108 System.err.println("Writing current page screenshot to: " + capture); 109 110 try 111 { 112 byte[] screenshotBytes = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.BYTES); 113 Files.write(capture.toPath(), screenshotBytes); 114 outputPaths.add(capture); 115 } catch (Exception ex) 116 { 117 System.err.println(ex.getMessage()); 118 } 119 } 120 121 private void writeContent(File file, String content) 122 { 123 try 124 { 125 FileWriter fw = new FileWriter(file); 126 127 fw.write(content); 128 129 outputPaths.add(file); 130 131 fw.close(); 132 } catch (IOException ex) 133 { 134 // Ignore. 135 } 136 } 137 138}