public void createWorkingDirectoryIfNotExists() throws IOException { try { createDirIfNotExists(runStepConfig.getWorkingDirectory()); } catch (IOException e) { throw new IOException("Could not create or open working directory.", e); } }
public List<String> getFilenamesForStep(String path, String url, String step) throws IOException { final String matcherPattern = "glob:**" + generateScreenshotFileNamePrefix(url, path) + "*_*_" + step + ".png"; Path screenshotDirectory = getScreenshotDirectory(); return getFileNamesMatchingPattern(screenshotDirectory, matcherPattern); }
private String buildRelativePathFromReportDir(String imageFileName) { return imageFileName != null ? fileService.getRelativePathFromReportDirToScreenshotsDir() + imageFileName : null; }
public String writeScreenshot(BufferedImage image, String url, String urlSubPath, int windowWidth, int yPosition, String step) throws IOException { final String screenshotPath = getScreenshotPath(url, urlSubPath, windowWidth, yPosition, step); writeScreenshot(screenshotPath, image); return screenshotPath; }
)); when(fileService.getRelativePathFromReportDirToScreenshotsDir()).thenReturn("screenshots/"); when(fileService.getFilenamesForStep("/", "http://url", "before")).thenReturn(ImmutableList.of("http_url_root_ff3c40c_1001_02002_before.png")); when(fileService.getFilenamesForStep("/", "http://url", "after")).thenReturn(ImmutableList.of("http_url_root_ff3c40c_1001_02002_after.png", "http_url_root_ff3c40c_1001_03003_after.png")); BufferedImage beforeBuffer = ImageIO.read(new File("src/test/resources/screenshots/http_url_root_ff3c40c_1001_02002_before.png")); when(fileService.readScreenshot("http_url_root_ff3c40c_1001_02002_before.png")).thenReturn( beforeBuffer); BufferedImage afterBuffer = ImageIO.read(new File("src/test/resources/screenshots/http_url_root_ff3c40c_1001_02002_after.png")); when(fileService.readScreenshot("http_url_root_ff3c40c_1001_02002_after.png")).thenReturn( afterBuffer); when(imageService.compareImages(beforeBuffer, afterBuffer, WINDOW_HEIGHT, ImageService.DEFAULT_PIXEL_COLOR_DIFFERENCE_THRESHOLD)).thenReturn(new ImageService.ImageComparisonResult(differenceBuffer, 0.1337d, 10)); when(fileService.writeScreenshot(differenceBuffer, "http://url", "/", 1001, 2002, "DIFFERENCE")).thenReturn("http_url_root_ff3c40c_1001_02002_DIFFERENCE.png"); writeScreenshot( differenceBuffer, "http://url",
String fullUrlWithPath = BrowserUtils.buildUrl(url, path, urlConfig.envMapping); final List<String> beforeFileNamesList = fileService.getFilenamesForStep(path, url, BEFORE); final List<String> afterFileNamesList = new ArrayList<>(); beforeFileNamesList.forEach(filename -> afterFileNamesList.add(switchAfterWithBeforeInFileName(filename))); final Set<String> afterFileNamesSet = new HashSet<>(fileService.getFilenamesForStep(path, url, AFTER)); imageBefore = fileService.readScreenshot(beforeFileName); } catch (IIOException e) { System.err.println("Can't read screenshot of 'before' step. Did you run JLineup with '--step before' parameter before trying to run '--step after' or --compare?"); imageAfter = fileService.readScreenshot(afterFileName); } catch (IIOException e) { screenshotComparisonResults.add(ScreenshotComparisonResult.noAfterImageComparisonResult(fullUrlWithPath, windowWidth, yPosition, buildRelativePathFromReportDir(beforeFileName))); String differenceImageFileName = null; if (imageComparisonResult.getDifference() > 0 && imageComparisonResult.getDifferenceImage().isPresent()) { differenceImageFileName = Paths.get(fileService.writeScreenshot(imageComparisonResult.getDifferenceImage().orElse(null), url, path, windowWidth, yPosition, "DIFFERENCE")).getFileName().toString();
@Test public void shouldFindBeforeImages() throws IOException { runStepConfig = jLineupRunConfigurationBuilder() .withWorkingDirectory("src/test/resources") .withScreenshotsDirectory("screenshots") .withReportDirectory("report") .build(); FileService fileService = new FileService(runStepConfig); //when List<String> beforeFiles = fileService.getFilenamesForStep("/", "http://url", BEFORE); //then assertThat(beforeFiles, is(ImmutableList.of("http_url_root_ff3c40c_1001_02002_before.png"))); }
@Test public void shouldBuildRelativePathsForDifferentDirectories() { //given runStepConfig = jLineupRunConfigurationBuilder() .withWorkingDirectory("src/test/resources") .withScreenshotsDirectory("screenshots") .withReportDirectory("report") .build(); FileService fileService = new FileService(runStepConfig); //when String relativePathFromReportDirToScreenshotsDir = fileService.getRelativePathFromReportDirToScreenshotsDir(); //then assertThat(relativePathFromReportDirToScreenshotsDir, is("../screenshots/")); }
public boolean run() { FileService fileService = new FileService(runStepConfig); ImageService imageService = new ImageService(); fileService.createWorkingDirectoryIfNotExists(); fileService.createOrClearReportDirectory(); fileService.createOrClearScreenshotsDirectory(); } catch (IOException e) { throw new RuntimeException(e);
public void writeComparisonReportAsJson(Report report) throws FileNotFoundException { final String reportJson = gson.toJson(report); fileService.writeJsonReport(reportJson); } }
@Before public void setup() throws IOException { initMocks(this); tempDirPath = tempDir.getRoot().getPath(); writeScreenshotTestPath = tempDirPath + "/testdirforlineupwritetest"; runStepConfig = jLineupRunConfigurationBuilder() .withWorkingDirectory(writeScreenshotTestPath) .withScreenshotsDirectory("screenshots") .withReportDirectory("report") .build(); testee = new FileService(runStepConfig); testee.createDirIfNotExists(writeScreenshotTestPath); testee.createDirIfNotExists(writeScreenshotTestPath + "/screenshots"); testee.createDirIfNotExists(writeScreenshotTestPath + "/report"); }
@VisibleForTesting String getScreenshotPath(String url, String urlSubPath, int width, int yPosition, String step) { return runStepConfig.getWorkingDirectory() + (runStepConfig.getWorkingDirectory().endsWith("/") ? "" : "/") + runStepConfig.getScreenshotsDirectory() + (runStepConfig.getScreenshotsDirectory().endsWith("/") ? "" : "/") + generateScreenshotFileName(url, urlSubPath, width, yPosition, step); }
public BufferedImage readScreenshot(String fileName) throws IOException { return ImageIO.read(new File(getScreenshotPath(fileName))); }
public void writeReport(Report report) throws FileNotFoundException { fileService.writeHtmlReport(renderReport("report", report.getFlatResultList())); }
@Test public void shouldFindAfterImagesInDirectoryWithPattern() throws IOException { List<String> fileNamesMatchingPattern = testee.getFileNamesMatchingPattern(Paths.get("src/test/resources/screenshots"), "glob:**http_url_root_*_*_after.png"); assertThat(fileNamesMatchingPattern, is(Arrays.asList("http_url_root_ff3c40c_1001_02002_after.png", "http_url_root_ff3c40c_1001_03003_after.png"))); }
@Test public void shouldFindAfterImages() throws IOException { runStepConfig = jLineupRunConfigurationBuilder() .withWorkingDirectory("src/test/resources") .withScreenshotsDirectory("screenshots") .withReportDirectory("report") .build(); FileService fileService = new FileService(runStepConfig); //when List<String> afterFiles = fileService.getFilenamesForStep("/", "http://url", AFTER); //then assertThat(afterFiles, is(ImmutableList.of("http_url_root_ff3c40c_1001_02002_after.png", "http_url_root_ff3c40c_1001_03003_after.png"))); }
@Test public void shouldBuildRelativePathsForSame() { //given runStepConfig = jLineupRunConfigurationBuilder() .withWorkingDirectory("src/test/resources") .withScreenshotsDirectory("rreeppoorrtt") .withReportDirectory("rreeppoorrtt") .build(); FileService fileService = new FileService(runStepConfig); //when String relativePathFromReportDirToScreenshotsDir = fileService.getRelativePathFromReportDirToScreenshotsDir(); //then assertThat(relativePathFromReportDirToScreenshotsDir, is("")); } }
public void writeComparisonReportAsJson(Report report) throws FileNotFoundException { final String reportJson = gson.toJson(report.getFlatResultList()); fileService.writeJsonReport(reportJson); } }
@Test public void shouldConvertRoot() throws Exception { String outputString = testee.generateScreenshotFileName("https://www.otto.de/", "/", 1000, 2000, "before"); assertThat(outputString, is("https_www_otto_de_root_baa15be_1000_02000_before.png")); }
public void writeRunStepMetadata(Step step, String metadata) throws FileNotFoundException { try (PrintStream out = new PrintStream(new FileOutputStream(getScreenshotPath("metadata_" + step + ".json")))) { out.print(metadata); } } }