Search in sources :

Example 1 with UrlConfig

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;
}
Also used : UrlConfig(de.otto.jlineup.config.UrlConfig) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 2 with UrlConfig

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));
}
Also used : Cookie(de.otto.jlineup.config.Cookie) UrlConfig(de.otto.jlineup.config.UrlConfig) UrlConfig(de.otto.jlineup.config.UrlConfig) Config(de.otto.jlineup.config.Config) Dimension(org.openqa.selenium.Dimension) File(java.io.File) Browser(de.otto.jlineup.browser.Browser) Test(org.junit.Test)

Example 3 with UrlConfig

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;
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) BufferedImage(java.awt.image.BufferedImage) ImageService(de.otto.jlineup.image.ImageService) LoggerFactory(org.slf4j.LoggerFactory) UrlConfig(de.otto.jlineup.config.UrlConfig) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Config(de.otto.jlineup.config.Config) FileService(de.otto.jlineup.file.FileService) BrowserUtils(de.otto.jlineup.browser.BrowserUtils) Matcher(java.util.regex.Matcher) Parameters(de.otto.jlineup.config.Parameters) Paths(java.nio.file.Paths) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Pattern(java.util.regex.Pattern) IIOException(javax.imageio.IIOException) ImageService(de.otto.jlineup.image.ImageService) BufferedImage(java.awt.image.BufferedImage) UrlConfig(de.otto.jlineup.config.UrlConfig)

Example 4 with UrlConfig

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);
}
Also used : UrlConfig(de.otto.jlineup.config.UrlConfig) UrlConfig(de.otto.jlineup.config.UrlConfig) Config(de.otto.jlineup.config.Config) File(java.io.File) Browser(de.otto.jlineup.browser.Browser) Test(org.junit.Test)

Example 5 with UrlConfig

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()));
}
Also used : Parameters(de.otto.jlineup.config.Parameters) UrlConfig(de.otto.jlineup.config.UrlConfig) UrlConfig(de.otto.jlineup.config.UrlConfig) Config(de.otto.jlineup.config.Config) Test(org.junit.Test)

Aggregations

UrlConfig (de.otto.jlineup.config.UrlConfig)6 Config (de.otto.jlineup.config.Config)4 Parameters (de.otto.jlineup.config.Parameters)3 Test (org.junit.Test)3 Browser (de.otto.jlineup.browser.Browser)2 File (java.io.File)2 JCommander (com.beust.jcommander.JCommander)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 BrowserUtils (de.otto.jlineup.browser.BrowserUtils)1 Cookie (de.otto.jlineup.config.Cookie)1 FileService (de.otto.jlineup.file.FileService)1 ImageService (de.otto.jlineup.image.ImageService)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 Paths (java.nio.file.Paths)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1