Skip to content

Commit

Permalink
Fix precision of max-diff value in url config. See #82
Browse files Browse the repository at this point in the history
  • Loading branch information
MediaMarco committed Aug 25, 2021
1 parent e35c236 commit e841c80
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
29 changes: 18 additions & 11 deletions core/src/main/java/de/otto/jlineup/JLineupRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,21 @@ public boolean run() {
jsonReportWriter.writeComparisonReportAsJson(report);
htmlReportWriter.writeReport(report);

final Set<Map.Entry<String, UrlReport>> entries = report.screenshotComparisonsForUrl.entrySet();
for (Map.Entry<String, UrlReport> entry : entries) {
LOG.info("Sum of screenshot differences for {}: {} ({} %)", entry.getKey(), entry.getValue().summary.differenceSum, Math.round(entry.getValue().summary.differenceSum * 100d));
LOG.info("Max difference of a single screenshot for {}: {} ({} %)", entry.getKey(), entry.getValue().summary.differenceMax, Math.round(entry.getValue().summary.differenceMax * 100d));
LOG.info("Accepted different pixels for {}: {}", entry.getKey(), entry.getValue().summary.acceptedDifferentPixels);
final Set<Map.Entry<String, UrlReport>> urlReports = report.screenshotComparisonsForUrl.entrySet();
for (Map.Entry<String, UrlReport> urlReport : urlReports) {
LOG.info("Sum of screenshot differences for {}: {} ({} %)", urlReport.getKey(), urlReport.getValue().summary.differenceSum, Math.round(urlReport.getValue().summary.differenceSum * 100d));
LOG.info("Max difference of a single screenshot for {}: {} ({} %)", urlReport.getKey(), urlReport.getValue().summary.differenceMax, Math.round(urlReport.getValue().summary.differenceMax * 100d));
LOG.info("Accepted different pixels for {}: {}", urlReport.getKey(), urlReport.getValue().summary.acceptedDifferentPixels);
}

LOG.info("Sum of overall screenshot differences: {} ({} %)", report.summary.differenceSum, Math.round(report.summary.differenceSum * 100d));
LOG.info("Max difference of a single screenshot: {} ({} %)", report.summary.differenceMax, Math.round(report.summary.differenceMax * 100d));

if (!Utils.shouldUseLegacyReportFormat(jobConfig)) {
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 (jobConfig.urls != null && entry.getValue().summary.differenceMax > jobConfig.urls.get(entry.getKey()).maxDiff) {
LOG.info("JLineup finished. There was a difference between before and after. Return code is 1.");
return false;
}
//Exit with exit code 1 if at least one url report has a bigger difference than configured
if (isDetectedDifferenceGreaterThanMaxDifference(urlReports, jobConfig)) {
LOG.info("JLineup finished. There was a difference between before and after. Return code is 1.");
return false;
}
}
}
Expand All @@ -113,6 +111,15 @@ public boolean run() {
return true;
}

static boolean isDetectedDifferenceGreaterThanMaxDifference(Set<Map.Entry<String, UrlReport>> urlReports, JobConfig jobConfig) {
for (Map.Entry<String, UrlReport> urlReport : urlReports) {
if (jobConfig.urls != null && urlReport.getValue().summary.differenceMax > jobConfig.urls.get(urlReport.getKey()).maxDiff) {
return true;
}
}
return false;
}

private void validateConfig() throws ValidationError {
JobConfigValidator.validateJobConfig(jobConfig);
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/de/otto/jlineup/config/UrlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class UrlConfig {
public final List<String> paths;
public final List<String> setupPaths;
public final List<String> cleanupPaths;
public final float maxDiff;
public final double maxDiff;
public final List<Cookie> cookies;
public final Map<String, String> envMapping;
public final Map<String, String> localStorage;
Expand Down Expand Up @@ -99,7 +99,7 @@ public List<String> getPaths() {
return paths;
}

public float getMaxDiff() {
public double getMaxDiff() {
return maxDiff;
}

Expand Down Expand Up @@ -242,7 +242,7 @@ public static final class Builder {
private List<String> paths = ImmutableList.of(DEFAULT_PATH);
private List<String> setupPaths = Collections.emptyList();
private List<String> cleanupPaths = Collections.emptyList();
private float maxDiff = DEFAULT_MAX_DIFF;
private double maxDiff = DEFAULT_MAX_DIFF;
private List<Cookie> cookies;
private Map<String, String> envMapping;
private Map<String, String> localStorage;
Expand Down Expand Up @@ -284,7 +284,7 @@ public Builder withCleanupPaths(List<String> val) {
return this;
}

public Builder withMaxDiff(float val) {
public Builder withMaxDiff(double val) {
maxDiff = val;
return this;
}
Expand Down Expand Up @@ -433,7 +433,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UrlConfig urlConfig = (UrlConfig) o;
return Float.compare(urlConfig.maxDiff, maxDiff) == 0 &&
return Double.compare(urlConfig.maxDiff, maxDiff) == 0 &&
maxScrollHeight == urlConfig.maxScrollHeight &&
Float.compare(urlConfig.waitAfterPageLoad, waitAfterPageLoad) == 0 &&
Float.compare(urlConfig.waitAfterScroll, waitAfterScroll) == 0 &&
Expand Down
27 changes: 27 additions & 0 deletions core/src/test/java/de/otto/jlineup/JLineupRunnerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package de.otto.jlineup;

import de.otto.jlineup.config.JobConfig;
import de.otto.jlineup.config.UrlConfig;
import de.otto.jlineup.report.Summary;
import de.otto.jlineup.report.UrlReport;
import org.assertj.core.data.MapEntry;
import org.junit.Test;

import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.jupiter.api.Assertions.*;

public class JLineupRunnerTest {

@Test
public void shouldNotFailIfMaxDiffIsSameAsDetectedDiff() {

UrlReport urlReport = new UrlReport(null, new Summary(false, 0, 15.1234567890123456, 0));
boolean detectedDifferenceGreaterThanMaxDifference = JLineupRunner.isDetectedDifferenceGreaterThanMaxDifference(Collections.singleton(MapEntry.entry("abc", urlReport)), JobConfig.exampleConfigBuilder().withUrls(Collections.singletonMap("abc", UrlConfig.urlConfigBuilder().withMaxDiff(15.1234567890123456).build())).build());

assertThat(detectedDifferenceGreaterThanMaxDifference, is(false));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static UrlConfig getExpectedUrlConfigForOttoDe() {

return UrlConfig.urlConfigBuilder()
.withPaths(ImmutableList.of("/", "multimedia"))
.withMaxDiff(0.05f)
.withMaxDiff(0.05d)
.withCookies(ImmutableList.of(new Cookie("testcookie1", "true"), new Cookie("testcookie2", "1")))
.withEnvMapping(ImmutableMap.of("live", "www"))
.withLocalStorage(ImmutableMap.of("teststorage", "{'testkey':{'value':true,'timestamp':9467812242358}}"))
Expand All @@ -125,7 +125,7 @@ public static UrlConfig getExpectedUrlConfigForGoogleDe() {

return UrlConfig.urlConfigBuilder()
.withPath("/")
.withMaxDiff(0.05f)
.withMaxDiff(0.05d)
.withWindowWidths(ImmutableList.of(1200))
.withMaxScrollHeight(100000)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ScreenshotsComparatorTest {

private final UrlConfig urlConfig = UrlConfig.urlConfigBuilder()
.withPath("/")
.withMaxDiff(0.05f)
.withMaxDiff(0.05d)
.withWindowWidths(singletonList(100))
.withMaxScrollHeight(10000)
.withWaitAfterPageLoad(2)
Expand Down

0 comments on commit e841c80

Please sign in to comment.