google-nomulus/javatests/google/registry/webdriver/ChromeWebDriverPlusScreenDiffer.java
shicong 8af9090fb0 Use reflection to inject the attempt number
This CL is to address the public static field in RepeatableRunner
for caller to get the current attempt number. We tried to have
a JUnit TestRule to achieve the purpose but it ended up with having
a RuleChain in each class where we already have multiple rules and
need to add the retry rule. This is because we have to make sure
the retry rule is the last one to wrap the test statement so that
the actual retry can include the actions defined in other rules.
Having a rule chain is not scalable and confuses engineer so we
gave it up.

Instead, we decided to expand the current RepeatableRunner to
use reflection to inject the attempt number to the test class.
Doing it this way can reduce the burden from the caller and it also
gets rid of the global state from the previous public static field.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=240789045
2019-03-29 16:17:35 -04:00

272 lines
9.9 KiB
Java

// Copyright 2019 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.webdriver;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.Math.abs;
import static java.util.stream.Collectors.joining;
import com.google.auto.value.AutoValue;
import com.google.common.collect.Lists;
import com.google.common.flogger.FluentLogger;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
import javax.imageio.ImageIO;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
/** Implementation of {@link WebDriverPlusScreenDiffer} that uses {@link ChromeDriver}. */
class ChromeWebDriverPlusScreenDiffer implements WebDriverPlusScreenDiffer {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
// Url address to the remote Webdriver service. In this case, we use
// Docker to provision the service and it is configured in core/build.gradle
private static final String CHROME_DRIVER_SERVICE_URL =
checkNotNull(System.getProperty("webdriver.chromeDriverServiceUrl"));
private final WebDriver webDriver;
private final String goldensPath;
private final int maxColorDiff;
private final int maxPixelDiff;
private final List<ActualScreenshot> actualScreenshots;
private String screenshotDir = System.getProperty("test.screenshot.dir", "build/screenshots");
public ChromeWebDriverPlusScreenDiffer(String goldensPath, int maxColorDiff, int maxPixelDiff) {
this.webDriver = createChromeDriver();
this.goldensPath = goldensPath;
this.maxColorDiff = maxColorDiff;
this.maxPixelDiff = maxPixelDiff;
this.actualScreenshots = Lists.newArrayList();
}
private WebDriver createChromeDriver() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setHeadless(true);
try {
return new RemoteWebDriver(new URL(CHROME_DRIVER_SERVICE_URL), chromeOptions);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
@AutoValue
abstract static class ComparisonResult {
abstract ActualScreenshot actualScreenshot();
abstract boolean isConsideredSimilar();
abstract boolean isMissingGoldenImage();
abstract boolean isSizeDifferent();
abstract int numDiffPixels();
static Builder builder() {
return new AutoValue_ChromeWebDriverPlusScreenDiffer_ComparisonResult.Builder();
}
@AutoValue.Builder
abstract static class Builder {
abstract Builder setActualScreenshot(ActualScreenshot actualScreenshot);
abstract Builder setIsConsideredSimilar(boolean isConsideredSimilar);
abstract Builder setIsMissingGoldenImage(boolean isMissingGoldenImage);
abstract Builder setIsSizeDifferent(boolean isSizeDifferent);
abstract Builder setNumDiffPixels(int numDiffPixels);
abstract ComparisonResult build();
}
}
static class ScreenshotNotSimilarException extends RuntimeException {
public ScreenshotNotSimilarException(String message) {
super(message);
}
}
@Override
public WebDriver getWebDriver() {
return webDriver;
}
@Override
public void diffElement(WebElement element, String imageKey, int attempt) {
ActualScreenshot elementImage =
ActualScreenshot.create(imageKey, attempt, takeScreenshot())
.getSubimage(
element.getLocation().getX(),
element.getLocation().getY(),
element.getSize().getWidth(),
element.getSize().getHeight());
actualScreenshots.add(elementImage);
}
@Override
public void diffPage(String imageKey, int attempt) {
actualScreenshots.add(ActualScreenshot.create(imageKey, attempt, takeScreenshot()));
}
@Override
public void verifyAndQuit() {
String errorMessage =
actualScreenshots
.parallelStream()
.map(this::compareScreenshots)
.map(
result -> {
String imageName = result.actualScreenshot().getImageName();
String goldenImagePath = goldenImagePath(imageName);
Path persistedScreenshot = persistScreenshot(result);
if (result.isConsideredSimilar()) {
logger.atInfo().log(
String.format(
"Screenshot test for [%s] passed:\n"
+ " - golden image location: %s\n"
+ " - screenshot image location: %s",
imageName, goldenImagePath, persistedScreenshot.toAbsolutePath()));
return "";
} else {
String diffReason =
String.format("it differed by %d pixels", result.numDiffPixels());
if (result.isMissingGoldenImage()) {
diffReason = "the golden image was missing";
} else if (result.isSizeDifferent()) {
diffReason = "the size of image was different";
}
return String.format(
"Screenshot test for [%s] failed because %s:\n"
+ " - golden image location: %s\n"
+ " - screenshot image location: %s",
imageName,
diffReason,
result.isMissingGoldenImage() ? "missing" : goldenImagePath(imageName),
persistedScreenshot.toAbsolutePath());
}
})
.filter(message -> !message.isEmpty())
.collect(joining("\n"));
if (!errorMessage.isEmpty()) {
errorMessage =
String.format(
"Following screenshot comparison comparisonInputs failed: \n%s", errorMessage);
logger.atSevere().log(errorMessage);
throw new ScreenshotNotSimilarException(errorMessage);
}
}
private byte[] takeScreenshot() {
checkArgument(webDriver instanceof TakesScreenshot);
TakesScreenshot takesScreenshot = (TakesScreenshot) webDriver;
return takesScreenshot.getScreenshotAs(OutputType.BYTES);
}
private ComparisonResult compareScreenshots(ActualScreenshot screenshot) {
int totalPixels = screenshot.getWidth() * screenshot.getHeight();
Optional<BufferedImage> maybeGoldenImage = loadGoldenImageByName(screenshot.getImageName());
ComparisonResult.Builder commonBuilder =
ComparisonResult.builder()
.setActualScreenshot(screenshot)
.setIsConsideredSimilar(false)
.setIsMissingGoldenImage(false)
.setIsSizeDifferent(false)
.setNumDiffPixels(totalPixels);
if (!maybeGoldenImage.isPresent()) {
return commonBuilder.setIsMissingGoldenImage(true).build();
}
BufferedImage goldenImage = maybeGoldenImage.get();
if ((screenshot.getWidth() != goldenImage.getWidth())
|| (screenshot.getHeight() != goldenImage.getHeight())) {
return commonBuilder.setIsSizeDifferent(true).build();
}
int currPixelDiff = 0;
for (int x = 0; x < screenshot.getWidth(); x++) {
for (int y = 0; y < screenshot.getHeight(); y++) {
Color screenshotColor = new Color(screenshot.getRGB(x, y));
Color goldenImageColor = new Color(goldenImage.getRGB(x, y));
int currColorDiff =
IntStream.of(
abs(screenshotColor.getRed() - goldenImageColor.getRed()),
abs(screenshotColor.getGreen() - goldenImageColor.getGreen()),
abs(screenshotColor.getBlue() - goldenImageColor.getBlue()))
.max()
.getAsInt();
if (currColorDiff > maxColorDiff) {
currPixelDiff++;
}
}
}
commonBuilder.setNumDiffPixels(currPixelDiff);
if (currPixelDiff <= maxPixelDiff) {
commonBuilder.setIsConsideredSimilar(true);
}
return commonBuilder.build();
}
private Path persistScreenshot(ComparisonResult result) {
File thisScreenshotDir =
Paths.get(
screenshotDir,
"attempt_" + result.actualScreenshot().getAttempt(),
result.isConsideredSimilar() ? "similar" : "different")
.toFile();
thisScreenshotDir.mkdirs();
File thisScreenshotFile = new File(thisScreenshotDir, result.actualScreenshot().getImageName());
result.actualScreenshot().writeTo(thisScreenshotFile);
return thisScreenshotFile.toPath();
}
private String goldenImagePath(String imageName) {
return Paths.get(goldensPath, imageName).toString();
}
private Optional<BufferedImage> loadGoldenImageByName(String imageName) {
File imageFile = new File(goldenImagePath(imageName));
if (!imageFile.isFile()) {
return Optional.empty();
}
try {
return Optional.of(ImageIO.read(imageFile));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}