001    // Copyright 2009, 2010 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    
015    package org.apache.tapestry5.test;
016    
017    import com.thoughtworks.selenium.CommandProcessor;
018    import org.testng.ITestContext;
019    
020    import java.io.File;
021    import java.io.FileWriter;
022    import java.io.IOException;
023    import java.lang.reflect.Method;
024    import java.util.ArrayList;
025    import java.util.HashSet;
026    import java.util.List;
027    import java.util.Set;
028    
029    public 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()
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 + ".html");
092    
093            System.err.println("Writing current page's HTML source to: " + report);
094    
095            try
096            {
097                FileWriter fw = new FileWriter(report);
098    
099                fw.write(htmlSource);
100    
101                outputPaths.add(report);
102    
103                fw.close();
104            } catch (IOException ex)
105            {
106                // Ignore.
107            }
108    
109            File capture = new File(dir, baseFileName + ".png");
110    
111            System.err.println("Writing current page screenshot to: " + capture);
112    
113            try
114            {
115                commandProcessor.doCommand("captureEntirePageScreenshot", new String[]
116                        {capture.getAbsolutePath(), "background=white"});
117    
118                outputPaths.add(capture);
119            } catch (Exception ex)
120            {
121                System.err.println(ex.getMessage());
122            }
123        }
124    
125    }