Commits

Chris Darroch committed a261be1

SELENIUM-197 - Output new screenshots in report if no baseline images exist.

This will make it simpler to start a visual regression test.

  • Participants
  • Parent commits 29cd6c5

Comments (0)

Files changed (2)

File atlassian-visual-comparison/src/main/java/com/atlassian/selenium/visualcomparison/VisualComparer.java

 import com.atlassian.selenium.visualcomparison.utils.BoundingBox;
 import com.atlassian.selenium.visualcomparison.utils.ScreenResolution;
 import com.atlassian.selenium.visualcomparison.utils.Screenshot;
-import com.atlassian.selenium.visualcomparison.utils.ScreenshotDiff;
-import junit.framework.Assert;
-
-import java.io.File;
-import java.io.FileFilter;
-import java.io.IOException;
+import com.atlassian.selenium.visualcomparison.utils.ScreenshotDiff;
+import junit.framework.Assert;
+
+import javax.imageio.ImageIO;
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
     public boolean compareScreenshots(ArrayList<Screenshot> oldScreenshots, ArrayList<Screenshot> newScreenshots)
             throws Exception
-    {
-        if (oldScreenshots.size() != newScreenshots.size())
-        {
-            throw new IllegalArgumentException("Did not find correct number of baseline images");
-        }
-
-        boolean matches = true;
+    {
+        if (oldScreenshots.size() != newScreenshots.size())
+        {
+            if (oldScreenshots.size() == 0)
+            {
+                if (reportingEnabled)
+                {
+                    String imageOutputDir = ScreenshotDiff.getImageOutputDir(reportOutputPath, imageSubDirName);
+                    for (Screenshot newScreenshot : newScreenshots)
+                    {
+                        // Copy the new image to the output directory.
+                        ImageIO.write(newScreenshot.getImage(), "png", new File(imageOutputDir + newScreenshot.getFileName()));
+                    }
+                }
+                throw new IllegalArgumentException("There were new screenshots, but no baseline images. Is this a new test?"
+                    + " If reporting is enabled, the new screenshots will be output in the report.");
+            }
+            else
+            {
+                throw new IllegalArgumentException("Incorrect number of images."
+                        + " There were " + oldScreenshots.size() + " baseline images,"
+                        + " but only " + newScreenshots.size() + " new images.");
+            }
+        }
+
+        boolean matches = true;
         for (int i = 0; i < oldScreenshots.size(); i++)
         {
             ScreenshotDiff diff = getScreenshotDiff(oldScreenshots.get(i), newScreenshots.get(i));

File atlassian-visual-comparison/src/main/java/com/atlassian/selenium/visualcomparison/utils/ScreenshotDiff.java

 
     public boolean hasDifferences()
     {
-        return boxes.size() > 0;
-    }
-
-    public void writeDiffReport(String outputDir, String imageSubDir) throws Exception
-    {
-        if (!hasDifferences())
-        {
-            return;
-        }
-
-        String imageOutputDir = outputDir + "/";
-        if (imageSubDir != null && !imageSubDir.equals(""))
+        return boxes.size() > 0;
+    }
+
+    public static String getImageOutputDir(String outputDir, String imageSubDir)
+    {
+        String imageOutputDir = outputDir + "/";
+        if (imageSubDir != null && !imageSubDir.equals(""))
+        {
+            imageOutputDir = imageOutputDir + imageSubDir + "/";
+        }
+        return imageOutputDir;
+    }
+
+    public void writeDiffReport(String outputDir, String imageSubDir) throws Exception
+    {
+        if (!hasDifferences())
         {
-            imageOutputDir = imageOutputDir + imageSubDir + "/";
-        }
-        
-        ArrayList<ReportDiffInfo> reportDiffs = new ArrayList<ReportDiffInfo>();
-        int i = 0;
+            return;
+        }
+
+        String imageOutputDir = getImageOutputDir(outputDir, imageSubDir);
+
+        ArrayList<ReportDiffInfo> reportDiffs = new ArrayList<ReportDiffInfo>();
+        int i = 0;
         for (BoundingBox box : boxes)
         {
             Graphics2D graphics = diffImage.createGraphics();
 
         // Copy the full baseline image to the output directory.
         String oldImageFile = "old-" + oldScreenshot.getFileName();
-        ImageIO.write(oldScreenshot.getImage(), "png", new File(imageOutputDir + oldImageFile));
-
-        // Copy the new image to the output directory.
-        ImageIO.write(newScreenshot.getImage(), "png", new File(imageOutputDir + newScreenshot.getFileName()));
-
-        VelocityContext context = ReportRenderer.createContext();
-        context.put("id", id);
-        context.put("resolution", resolution);
-        context.put("diffs", reportDiffs);
-        context.put("oldImageFile", imageSubDir + "/" + oldImageFile);
-        context.put("newImageFile", imageSubDir + "/" + newScreenshot.getFileName());
-        context.put("diffImageFile", imageSubDir + "/" + diffImageFile);
-        String report = ReportRenderer.render(context, "visual-regression-report-single.vm");
-
+        ImageIO.write(oldScreenshot.getImage(), "png", new File(imageOutputDir + oldImageFile));
+
+        // Copy the new image to the output directory.
+        final String newImageFile = newScreenshot.getFileName();
+        ImageIO.write(newScreenshot.getImage(), "png", new File(imageOutputDir + newImageFile));
+
+        VelocityContext context = ReportRenderer.createContext();
+        context.put("id", id);
+        context.put("resolution", resolution);
+        context.put("diffs", reportDiffs);
+        context.put("oldImageFile", imageSubDir + "/" + oldImageFile);
+        context.put("newImageFile", imageSubDir + "/" + newImageFile);
+        context.put("diffImageFile", imageSubDir + "/" + diffImageFile);
+        String report = ReportRenderer.render(context, "visual-regression-report-single.vm");
+
         OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File(outputDir + "/report-" + id + "-" + resolution + ".html")));
         writer.append(report);
         writer.close();