use of de.otto.jlineup.config.UrlConfig in project jlineup by otto-de.
the class BrowserUtils method buildScreenshotContextListFromConfigAndState.
static List<ScreenshotContext> buildScreenshotContextListFromConfigAndState(Parameters parameters, Config config, boolean before) {
List<ScreenshotContext> screenshotContextList = new ArrayList<>();
Map<String, UrlConfig> urls = config.urls;
if (urls == null) {
System.err.println("No urls are configured in the config.");
System.exit(1);
}
for (final Map.Entry<String, UrlConfig> urlConfigEntry : urls.entrySet()) {
final UrlConfig urlConfig = urlConfigEntry.getValue();
final List<Integer> resolutions = urlConfig.windowWidths;
final List<String> paths = urlConfig.paths;
for (final String path : paths) {
screenshotContextList.addAll(resolutions.stream().map(windowWidth -> new ScreenshotContext(prepareDomain(parameters, urlConfigEntry.getKey()), path, windowWidth, before, urlConfigEntry.getValue())).collect(Collectors.toList()));
}
}
return screenshotContextList;
}
use of de.otto.jlineup.config.UrlConfig in project jlineup by otto-de.
the class BrowserTest method shouldDoAllTheScreenshotWebdriverCalls.
@Test
public void shouldDoAllTheScreenshotWebdriverCalls() throws Exception {
// given
final Long viewportHeight = 500L;
final Long pageHeight = 2000L;
UrlConfig urlConfig = new UrlConfig(ImmutableList.of("/"), 0f, ImmutableList.of(new Cookie("testcookiename", "testcookievalue")), ImmutableMap.of(), ImmutableMap.of("key", "value"), ImmutableMap.of("key", "value"), ImmutableList.of(600, 800), 5000, 0, 0, 0, 3, "testJS();", 5);
Config config = configBuilder().withBrowser(FIREFOX).withUrls(ImmutableMap.of("testurl", urlConfig)).withWindowHeight(100).build();
testee.close();
testee = new Browser(parameters, config, fileService, browserUtilsMock);
ScreenshotContext screenshotContext = ScreenshotContext.of("testurl", "/", 600, true, urlConfig);
ScreenshotContext screenshotContext2 = ScreenshotContext.of("testurl", "/", 800, true, urlConfig);
when(webDriverMock.executeScript(JS_DOCUMENT_HEIGHT_CALL)).thenReturn(pageHeight);
when(webDriverMock.executeScript(JS_CLIENT_VIEWPORT_HEIGHT_CALL)).thenReturn(viewportHeight);
when(webDriverMock.getScreenshotAs(OutputType.FILE)).thenReturn(new File(getFilePath("screenshots/http_url_root_ff3c40c_1001_02002_before.png")));
when(webDriverMock.executeScript(JS_RETURN_DOCUMENT_FONTS_SIZE_CALL)).thenReturn(3L);
when(webDriverMock.executeScript(JS_RETURN_DOCUMENT_FONTS_STATUS_LOADED_CALL)).thenReturn(false).thenReturn(true);
// when
testee.takeScreenshots(ImmutableList.of(screenshotContext, screenshotContext2));
// then
verify(webDriverWindowMock, times(2)).setSize(new Dimension(600, 100));
verify(webDriverWindowMock, times(2)).setSize(new Dimension(800, 100));
verify(webDriverMock, times(2)).executeScript(JS_SCROLL_TO_TOP_CALL);
verify(webDriverMock, times(2)).executeScript("testJS();");
verify(webDriverMock, times(10)).executeScript(JS_DOCUMENT_HEIGHT_CALL);
verify(webDriverMock, times(5)).get("testurl/");
verify(webDriverMock, times(2)).executeScript(JS_CLIENT_VIEWPORT_HEIGHT_CALL);
verify(webDriverOptionsMock, times(2)).addCookie(new org.openqa.selenium.Cookie("testcookiename", "testcookievalue"));
verify(webDriverMock, times(2)).executeScript(String.format(JS_SET_LOCAL_STORAGE_CALL, "key", "value"));
verify(webDriverMock, times(2)).executeScript(String.format(JS_SET_SESSION_STORAGE_CALL, "key", "value"));
verify(webDriverMock, times(8)).executeScript(String.format(JS_SCROLL_CALL, 500));
}
use of de.otto.jlineup.config.UrlConfig in project jlineup by otto-de.
the class ScreenshotsComparator method compare.
public Map<String, List<ScreenshotComparisonResult>> compare() throws IOException {
LOG.debug("Comparing images...");
if (config.urls == null) {
LOG.debug("No urls configured, so no comparison.");
return null;
}
Map<String, List<ScreenshotComparisonResult>> results = new HashMap<>();
for (Map.Entry<String, UrlConfig> urlConfigEntry : config.urls.entrySet()) {
List<ScreenshotComparisonResult> screenshotComparisonResults = new ArrayList<>();
String url = BrowserUtils.prepareDomain(parameters, urlConfigEntry.getKey());
UrlConfig urlConfig = urlConfigEntry.getValue();
LOG.debug("Url: {}", url);
for (String path : urlConfig.paths) {
LOG.debug("Path: {}", path);
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> beforeFileNamesSet = new HashSet<>(beforeFileNamesList);
final Set<String> afterFileNamesSet = new HashSet<>(fileService.getFilenamesForStep(path, url, AFTER));
// we need after files that have no before file in the final report
final List<String> afterFileNamesWithNoBeforeFile = new ArrayList<>();
afterFileNamesSet.stream().filter(name -> !beforeFileNamesSet.contains(switchAfterWithBeforeInFileName(name))).forEach(afterFileNamesWithNoBeforeFile::add);
for (int i = 0; i < beforeFileNamesList.size(); i++) {
String beforeFileName = beforeFileNamesList.get(i);
String afterFileName = afterFileNamesList.get(i);
LOG.debug("Comparing '{}' with '{}'", beforeFileName, afterFileName);
int yPosition = extractVerticalScrollPositionFromFileName(beforeFileName);
int windowWidth = extractWindowWidthFromFileName(beforeFileName);
BufferedImage imageBefore;
try {
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?");
throw e;
}
BufferedImage imageAfter;
try {
imageAfter = fileService.readScreenshot(afterFileName);
} catch (IIOException e) {
screenshotComparisonResults.add(ScreenshotComparisonResult.noAfterImageComparisonResult(fullUrlWithPath, windowWidth, yPosition, buildRelativePathFromReportDir(beforeFileName)));
continue;
}
ImageService.ImageComparisonResult imageComparisonResult = imageService.compareImages(imageBefore, imageAfter, config.windowHeight);
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();
}
screenshotComparisonResults.add(new ScreenshotComparisonResult(fullUrlWithPath, windowWidth, yPosition, imageComparisonResult.getDifference(), buildRelativePathFromReportDir(beforeFileName), buildRelativePathFromReportDir(afterFileName), buildRelativePathFromReportDir(differenceImageFileName)));
}
addMissingBeforeFilesToResults(screenshotComparisonResults, fullUrlWithPath, afterFileNamesWithNoBeforeFile);
}
screenshotComparisonResults.sort(Comparator.<ScreenshotComparisonResult, String>comparing(r -> r.url).thenComparing(r -> r.width).thenComparing(r -> r.verticalScrollPosition));
results.put(urlConfigEntry.getKey(), screenshotComparisonResults);
}
return results;
}
use of de.otto.jlineup.config.UrlConfig in project jlineup by otto-de.
the class BrowserTest method shouldNotResizeWindowWhenDoingHeadless.
@Test
public void shouldNotResizeWindowWhenDoingHeadless() throws Exception {
// given
final Long viewportHeight = 500L;
final Long pageHeight = 2000L;
UrlConfig urlConfig = new UrlConfig(ImmutableList.of("/"), 0f, ImmutableList.of(), ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of(), ImmutableList.of(600, 800), 5000, 0, 0, 0, 3, null, 5);
Config config = configBuilder().withBrowser(CHROME_HEADLESS).withUrls(ImmutableMap.of("testurl", urlConfig)).withWindowHeight(100).build();
testee.close();
testee = new Browser(parameters, config, fileService, browserUtilsMock);
ScreenshotContext screenshotContext = ScreenshotContext.of("testurl", "/", 600, true, urlConfig);
ScreenshotContext screenshotContext2 = ScreenshotContext.of("testurl", "/", 800, true, urlConfig);
when(webDriverMock.executeScript(JS_DOCUMENT_HEIGHT_CALL)).thenReturn(pageHeight);
when(webDriverMock.executeScript(JS_CLIENT_VIEWPORT_HEIGHT_CALL)).thenReturn(viewportHeight);
when(webDriverMock.getScreenshotAs(OutputType.FILE)).thenReturn(new File("src/test/resources/screenshots/http_url_root_ff3c40c_1001_02002_before.png"));
when(webDriverMock.executeScript(JS_RETURN_DOCUMENT_FONTS_SIZE_CALL)).thenReturn(3L);
when(webDriverMock.executeScript(JS_RETURN_DOCUMENT_FONTS_STATUS_LOADED_CALL)).thenReturn(false).thenReturn(true);
// when
testee.takeScreenshots(ImmutableList.of(screenshotContext, screenshotContext2));
verifyNoMoreInteractions(webDriverWindowMock);
}
use of de.otto.jlineup.config.UrlConfig in project jlineup by otto-de.
the class BrowserUtilsTest method shouldGenerateScreenshotsParameters.
@Test
public void shouldGenerateScreenshotsParameters() throws FileNotFoundException {
// given
Parameters parameters = mock(Parameters.class);
Config config = Config.readConfig(".", "src/test/resources/lineup_test.json");
when(parameters.getWorkingDirectory()).thenReturn("some/working/dir");
when(parameters.getScreenshotDirectory()).thenReturn("screenshots");
when(parameters.getUrlReplacements()).thenReturn(ImmutableMap.of("google", "doodle"));
UrlConfig expectedUrlConfigForOttoDe = getExpectedUrlConfigForOttoDe();
UrlConfig expectedUrlConfigForGoogleDe = getExpectedUrlConfigForGoogleDe();
final List<ScreenshotContext> expectedScreenshotContextList = ImmutableList.of(ScreenshotContext.of("https://www.otto.de", "/", 600, true, expectedUrlConfigForOttoDe), ScreenshotContext.of("https://www.otto.de", "/", 800, true, expectedUrlConfigForOttoDe), ScreenshotContext.of("https://www.otto.de", "/", 1200, true, expectedUrlConfigForOttoDe), ScreenshotContext.of("https://www.otto.de", "multimedia", 600, true, expectedUrlConfigForOttoDe), ScreenshotContext.of("https://www.otto.de", "multimedia", 800, true, expectedUrlConfigForOttoDe), ScreenshotContext.of("https://www.otto.de", "multimedia", 1200, true, expectedUrlConfigForOttoDe), ScreenshotContext.of("http://www.doodle.de", "/", 1200, true, expectedUrlConfigForGoogleDe));
// when
final List<ScreenshotContext> screenshotContextList = BrowserUtils.buildScreenshotContextListFromConfigAndState(parameters, config, true);
// then
assertThat(screenshotContextList, containsInAnyOrder(expectedScreenshotContextList.toArray()));
}
Aggregations