Search in sources :

Example 1 with Browser

use of de.otto.jlineup.browser.Browser in project jlineup by otto-de.

the class JLineup method run.

public int run() throws IOException {
    FileService fileService = new FileService(jLineupOptions.getParameters());
    ImageService imageService = new ImageService();
    // Make sure the working dir exists
    if (jLineupOptions.getParameters().isBefore()) {
        fileService.createWorkingDirectoryIfNotExists();
    }
    if (config.debug) {
        Util.setLogLevelToDebug();
    }
    if (config.logToFile || jLineupOptions.getParameters().isLogToFile()) {
        Util.logToFile(jLineupOptions.getParameters().getWorkingDirectory());
    }
    // Only create screenshots and report dirs if config was found
    if (jLineupOptions.getParameters().isBefore()) {
        fileService.createOrClearReportDirectory();
        fileService.createOrClearScreenshotsDirectory();
    }
    System.out.printf("Running JLineup [%s] with step '%s'.%n%n", Util.getVersion(), jLineupOptions.getParameters().getStep());
    if (!jLineupOptions.getParameters().isJustCompare()) {
        BrowserUtils browserUtils = new BrowserUtils();
        try (Browser browser = new Browser(jLineupOptions.getParameters(), config, fileService, browserUtils)) {
            browser.takeScreenshots();
        } catch (Exception e) {
            System.err.println("JLineup Exception: " + e);
            return 1;
        }
    }
    if (jLineupOptions.getParameters().isAfter() || jLineupOptions.getParameters().isJustCompare()) {
        ScreenshotsComparator screenshotsComparator = new ScreenshotsComparator(jLineupOptions.getParameters(), config, fileService, imageService);
        final Map<String, List<ScreenshotComparisonResult>> comparisonResults = screenshotsComparator.compare();
        final ReportGenerator reportGenerator = new ReportGenerator();
        final Report report = reportGenerator.generateReport(comparisonResults);
        JSONReportWriter jsonReportWriter;
        if (Util.shouldUseLegacyReportFormat(config)) {
            jsonReportWriter = new JSONReportWriter_V1(fileService);
        } else {
            jsonReportWriter = new JSONReportWriter_V2(fileService);
        }
        jsonReportWriter.writeComparisonReportAsJson(report);
        final HTMLReportWriter htmlReportWriter = new HTMLReportWriter(fileService);
        htmlReportWriter.writeReport(report);
        final Set<Map.Entry<String, UrlReport>> entries = report.screenshotComparisonsForUrl.entrySet();
        for (Map.Entry<String, UrlReport> entry : entries) {
            System.out.println("Sum of screenshot differences for " + entry.getKey() + ":\n" + entry.getValue().summary.differenceSum + " (" + Math.round(entry.getValue().summary.differenceSum * 100d) + " %)");
            System.out.println("Max difference of a single screenshot for " + entry.getKey() + ":\n" + entry.getValue().summary.differenceMax + " (" + Math.round(entry.getValue().summary.differenceMax * 100d) + " %)");
            System.out.println("");
        }
        System.out.println("Sum of overall screenshot differences:\n" + report.summary.differenceSum + " (" + Math.round(report.summary.differenceSum * 100d) + " %)");
        System.out.println("Max difference of a single screenshot:\n" + report.summary.differenceMax + " (" + Math.round(report.summary.differenceMax * 100d) + " %)");
        if (!Util.shouldUseLegacyReportFormat(config)) {
            for (Map.Entry<String, UrlReport> entry : entries) {
                // Exit with exit code 1 if at least one url report has a bigger difference than configured
                if (config.urls != null && entry.getValue().summary.differenceMax > config.urls.get(entry.getKey()).maxDiff) {
                    System.out.println("JLineup finished. There was a difference between before and after. Return code is 1.");
                    System.exit(1);
                }
            }
        }
    }
    System.out.printf("JLineup run finished for step '%s'%n", jLineupOptions.getParameters().getStep());
    return 0;
}
Also used : FileService(de.otto.jlineup.file.FileService) ImageService(de.otto.jlineup.image.ImageService) IOException(java.io.IOException) List(java.util.List) BrowserUtils(de.otto.jlineup.browser.BrowserUtils) Map(java.util.Map) Browser(de.otto.jlineup.browser.Browser)

Example 2 with Browser

use of de.otto.jlineup.browser.Browser in project jlineup by otto-de.

the class BrowserTest method setup.

@Before
public void setup() throws IOException {
    initMocks(this);
    when(webDriverMock.manage()).thenReturn(webDriverOptionsMock);
    when(webDriverOptionsMock.timeouts()).thenReturn(webDriverTimeoutMock);
    when(webDriverOptionsMock.window()).thenReturn(webDriverWindowMock);
    when(webDriverOptionsMock.logs()).thenReturn(webDriverLogs);
    when(browserUtilsMock.getWebDriverByConfig(any(Config.class))).thenReturn(webDriverMock);
    when(browserUtilsMock.getWebDriverByConfig(any(Config.class), anyInt())).thenReturn(webDriverMock);
    Config config = configBuilder().build();
    testee = new Browser(parameters, config, fileService, browserUtilsMock);
    testee.initializeWebDriver();
}
Also used : UrlConfig(de.otto.jlineup.config.UrlConfig) Config(de.otto.jlineup.config.Config) Browser(de.otto.jlineup.browser.Browser) Before(org.junit.Before)

Example 3 with Browser

use of de.otto.jlineup.browser.Browser 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 4 with Browser

use of de.otto.jlineup.browser.Browser 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)

Aggregations

Browser (de.otto.jlineup.browser.Browser)4 Config (de.otto.jlineup.config.Config)3 UrlConfig (de.otto.jlineup.config.UrlConfig)3 File (java.io.File)2 Test (org.junit.Test)2 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 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 Before (org.junit.Before)1 Dimension (org.openqa.selenium.Dimension)1