Skip to content

Commit cdcc63e

Browse files
committed
Add override annotations
1 parent f807f0e commit cdcc63e

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/main/java/aquality/selenium/core/visualization/ImageComparator.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ private int getComparisonWidth() {
2323
return visualConfiguration.getComparisonWidth();
2424
}
2525

26+
@Override
27+
public float percentageDifference(Image thisOne, Image theOtherOne) {
28+
return percentageDifference(thisOne, theOtherOne, visualConfiguration.getDefaultThreshold());
29+
}
30+
31+
@Override
2632
public float percentageDifference(Image thisOne, Image theOtherOne, float threshold) {
2733
if (threshold < 0 || threshold > 1) {
2834
throw new IllegalArgumentException(String.format("Threshold should be between 0 and 1, but was [%s]", threshold));
@@ -32,10 +38,6 @@ public float percentageDifference(Image thisOne, Image theOtherOne, float thresh
3238
return percentageDifference(thisOne, theOtherOne, intThreshold);
3339
}
3440

35-
public float percentageDifference(Image thisOne, Image theOtherOne) {
36-
return percentageDifference(thisOne, theOtherOne, visualConfiguration.getDefaultThreshold());
37-
}
38-
3941
protected float percentageDifference(Image thisOne, Image theOtherOne, int threshold) {
4042
int[][] differences = getDifferences(thisOne, theOtherOne);
4143

src/main/java/aquality/selenium/core/visualization/ImageFunctions.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import org.openqa.selenium.OutputType;
66
import org.openqa.selenium.remote.RemoteWebElement;
77

8+
import javax.imageio.IIOImage;
89
import javax.imageio.ImageIO;
10+
import javax.imageio.ImageWriteParam;
11+
import javax.imageio.ImageWriter;
12+
import javax.imageio.stream.FileImageInputStream;
913
import java.awt.*;
1014
import java.awt.image.BufferedImage;
1115
import java.awt.image.RenderedImage;
12-
import java.io.ByteArrayInputStream;
13-
import java.io.IOException;
14-
import java.io.InputStream;
16+
import java.io.*;
1517

1618
import static java.awt.image.BufferedImage.TYPE_INT_RGB;
1719

@@ -102,4 +104,26 @@ public static BufferedImage resize(Image image, int width, int height) {
102104
graphics.dispose();
103105
return resizedImage;
104106
}
107+
108+
/**
109+
* Saves image in the highest quality.
110+
* @param image source image.
111+
* @param name target name without extension.
112+
* @param format target format.
113+
*/
114+
public static void save(RenderedImage image, String name, String format) {
115+
final float highestQuality = 1.0F;
116+
ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
117+
ImageWriteParam param = writer.getDefaultWriteParam();
118+
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
119+
param.setCompressionQuality(highestQuality);
120+
String fileName = String.format("%s.%s", name, format.startsWith(".") ? format.substring(1) : format);
121+
try {
122+
writer.setOutput(new FileImageInputStream(new File(fileName)));
123+
writer.write(null, new IIOImage(image, null, null), param);
124+
} catch (IOException e) {
125+
Logger.getInstance().fatal("Failed to save the image: " + e.getMessage(), e);
126+
throw new UncheckedIOException(e);
127+
}
128+
}
105129
}

src/main/java/aquality/selenium/core/visualization/VisualStateProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.function.Supplier;
1010

1111
public class VisualStateProvider implements IVisualStateProvider {
12-
1312
private final IImageComparator imageComparator;
1413
private final IElementActionRetrier actionRetrier;
1514
private final Supplier<RemoteWebElement> getElement;
@@ -22,20 +21,23 @@ public VisualStateProvider(IImageComparator imageComparator, IElementActionRetri
2221
this.stateLogger = stateLogger;
2322
}
2423

24+
@Override
2525
public Dimension getSize() {
2626
return getLoggedValue("size", element -> {
2727
final org.openqa.selenium.Dimension size = element.getSize();
2828
return new Dimension(size.getWidth(), size.getHeight());
2929
}, null);
3030
}
3131

32+
@Override
3233
public Point getLocation() {
3334
return getLoggedValue("location", element -> {
3435
final org.openqa.selenium.Point location = element.getLocation();
3536
return new Point(location.getX(), location.getY());
3637
}, null);
3738
}
3839

40+
@Override
3941
public Image getImage() {
4042
return getLoggedValue("image", ImageFunctions::getScreenshotAsImage,
4143
image -> getStringValue(ImageFunctions.getSize(image)));

0 commit comments

Comments
 (0)