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    public void writeErrorReport(String reportText)
070    {
071        String htmlSource = commandProcessor.getString("getHtmlSource", new String[]
072                {});
073
074        File dir = new File(testContext.getOutputDirectory());
075
076        dir.mkdirs();
077
078        Method testMethod = (Method) testContext.getAttribute(TapestryTestConstants.CURRENT_TEST_METHOD_ATTRIBUTE);
079
080        String baseFileName = testMethod == null ? "Unknown-test" : testMethod.getDeclaringClass().getSimpleName()
081                + "." + testMethod.getName();
082
083        if (previousNames.contains(baseFileName))
084        {
085            baseFileName += "-" + uid++;
086        } else
087        {
088            previousNames.add(baseFileName);
089        }
090
091        File report = new File(dir, baseFileName + ".txt");
092
093        System.err.println("Writing failure report to: " + report);
094
095        writeContent(report, reportText);
096
097        File capturedSource = new File(dir, baseFileName + ".html");
098
099        System.err.println("Writing current page's HTML source to: " + capturedSource);
100
101        writeContent(capturedSource, htmlSource);
102
103        File capture = new File(dir, baseFileName + ".png");
104
105        System.err.println("Writing current page screenshot to: " + capture);
106
107        try
108        {
109            commandProcessor.doCommand("captureEntirePageScreenshot", new String[]
110                    {capture.getAbsolutePath(), "background=white"});
111
112            outputPaths.add(capture);
113        } catch (Exception ex)
114        {
115            System.err.println(ex.getMessage());
116        }
117    }
118
119    private void writeContent(File file, String content)
120    {
121        try
122        {
123            FileWriter fw = new FileWriter(file);
124
125            fw.write(content);
126
127            outputPaths.add(file);
128
129            fw.close();
130        } catch (IOException ex)
131        {
132            // Ignore.
133        }
134    }
135
136}