Skip to content

Commit 1500311

Browse files
committed
Add cropAround element; Fixes #32
1 parent c627f3f commit 1500311

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/main/java/com/assertthat/selenium_shutterbug/core/PageSnapshot.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ public PageSnapshot blurExcept(WebElement element) {
155155
return this;
156156
}
157157

158+
/**
159+
* Crop the image around specified element with offset.
160+
*
161+
* @param element WebElement to crop around
162+
* @param offsetX offsetX around element in px
163+
* @param offsetY offsetY around element in px
164+
* @return instance of type PageSnapshot
165+
*/
166+
public PageSnapshot cropAround(WebElement element, int offsetX, int offsetY) {
167+
try{
168+
image = ImageProcessor.cropAround(image, new Coordinates(element), offsetX, offsetY);
169+
}catch(RasterFormatException rfe){
170+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE,rfe);
171+
}
172+
return this;
173+
}
174+
158175
@Override
159176
protected PageSnapshot self() {
160177
return this;

src/main/java/com/assertthat/selenium_shutterbug/utils/image/ImageProcessor.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public static BufferedImage blurExceptArea(BufferedImage sourceImage, Coordinate
8787
return combined;
8888
}
8989

90+
public static BufferedImage cropAround(BufferedImage sourceImage, Coordinates coords, int offsetX, int offsetY) {
91+
BufferedImage subImage = sourceImage.getSubimage(coords.getX()-offsetX, coords.getY()-offsetY, coords.getWidth()+offsetX*2, coords.getHeight()+offsetY*2);
92+
return subImage;
93+
}
94+
9095
public static BufferedImage addTitle(BufferedImage sourceImage, String title, Color color, Font textFont) {
9196
int textOffset = 5;
9297
BufferedImage combined = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight() + textFont.getSize(), BufferedImage.TYPE_INT_ARGB);
@@ -186,14 +191,6 @@ public static boolean imagesAreEqualsWithDiff(BufferedImage image1, BufferedImag
186191
return pixelError == 0 || pixelError <= deviation;
187192
}
188193

189-
/**
190-
* Gives back the pixel error set by imagesAreEqualsWithDiff. 'getPixelError' should be called after 'imagesAreEqualsWithDiff.'
191-
* @return pixelError The pixel error
192-
*/
193-
public static double getPixelError(){
194-
return pixelError;
195-
}
196-
197194
public static BufferedImage scale(BufferedImage source, double ratio) {
198195
int w = (int) (source.getWidth() * ratio);
199196
int h = (int) (source.getHeight() * ratio);

0 commit comments

Comments
 (0)