Skip to content

Commit 31d0730

Browse files
committed
Add cut out option
1 parent 69f72b7 commit 31d0730

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,38 @@ public PageSnapshot cropAround(WebElement element, int offsetX, int offsetY) {
173173
return this;
174174
}
175175

176+
/**
177+
* Cut out specified element with offset.
178+
*
179+
* @param element WebElement to crop
180+
* @param offsetX offsetX around element in px
181+
* @param offsetY offsetY around element in px
182+
* @return instance of type PageSnapshot
183+
*/
184+
public PageSnapshot cutOut(WebElement element, int offsetX, int offsetY) {
185+
try {
186+
image = ImageProcessor.cutOut(image, new Coordinates(element, devicePixelRatio), offsetX, offsetY);
187+
} catch (RasterFormatException rfe) {
188+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
189+
}
190+
return this;
191+
}
192+
193+
/**
194+
* Cut out specified element.
195+
*
196+
* @param element WebElement to crop
197+
* @return instance of type PageSnapshot
198+
*/
199+
public PageSnapshot cutOut(WebElement element) {
200+
try {
201+
image = ImageProcessor.cutOut(image, new Coordinates(element, devicePixelRatio), 0, 0);
202+
} catch (RasterFormatException rfe) {
203+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
204+
}
205+
return this;
206+
}
207+
176208
@Override
177209
protected PageSnapshot self() {
178210
return this;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ public static BufferedImage cropAround(BufferedImage sourceImage, Coordinates co
9292
return sourceImage.getSubimage(coords.getX() - offsetX, coords.getY() - offsetY, coords.getWidth() + offsetX * 2, coords.getHeight() + offsetY * 2);
9393
}
9494

95+
public static BufferedImage cutOut(BufferedImage sourceImage, Coordinates coords, int offsetX, int offsetY) {
96+
Graphics bg = sourceImage.getGraphics();
97+
bg.setColor(Color.white);
98+
bg.fillRect(coords.getX() - offsetX, coords.getY() - offsetY, coords.getScrollWidth() + offsetX * 2, coords.getScrollHeight() + offsetY * 2);
99+
return sourceImage;
100+
}
101+
95102
public static BufferedImage addTitle(BufferedImage sourceImage, String title, Color color, Font textFont) {
96103
int textOffset = 5;
97104
BufferedImage combined = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight() + textFont.getSize(), BufferedImage.TYPE_INT_ARGB);

0 commit comments

Comments
 (0)