Skip to content

Commit d5af300

Browse files
committed
Refactor throwing ecxeption if element outside the viewport
1 parent 6ceb215 commit d5af300

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
package com.assertthat.selenium_shutterbug.core;
77

8+
import com.assertthat.selenium_shutterbug.utils.image.ImageProcessor;
9+
import com.assertthat.selenium_shutterbug.utils.web.Coordinates;
10+
import com.assertthat.selenium_shutterbug.utils.web.ElementOutsideViewportException;
811
import org.openqa.selenium.WebDriver;
912
import org.openqa.selenium.WebElement;
1013

14+
import java.awt.image.BufferedImage;
15+
import java.awt.image.RasterFormatException;
16+
1117
/**
1218
* Created by Glib_Briia on 17/06/2016.
1319
*/
@@ -17,6 +23,14 @@ public class ElementSnapshot extends Snapshot {
1723
this.driver = driver;
1824
}
1925

26+
protected void setImage(BufferedImage image, Coordinates coords) {
27+
try {
28+
self().image = ImageProcessor.getElement(image, coords);
29+
} catch (RasterFormatException rfe) {
30+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
31+
}
32+
}
33+
2034
@Override
2135
protected ElementSnapshot self() {
2236
return this;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public PageSnapshot highlight(WebElement element) {
3434
try {
3535
highlight(element, Color.red, 3);
3636
} catch (RasterFormatException rfe) {
37-
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
37+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
3838
}
3939
return this;
4040
}
@@ -52,7 +52,7 @@ public PageSnapshot highlight(WebElement element, Color color, int lineWidth) {
5252
try {
5353
image = ImageProcessor.highlight(image, new Coordinates(element), color, lineWidth);
5454
} catch (RasterFormatException rfe) {
55-
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
55+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
5656
}
5757
return this;
5858
}
@@ -71,7 +71,7 @@ public PageSnapshot highlightWithText(WebElement element, String text) {
7171
try {
7272
highlightWithText(element, Color.red, 3, text, Color.red, new Font("Serif", Font.BOLD, 20));
7373
} catch (RasterFormatException rfe) {
74-
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
74+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
7575
}
7676
return this;
7777
}
@@ -94,7 +94,7 @@ public PageSnapshot highlightWithText(WebElement element, Color elementColor, in
9494
Coordinates coords = new Coordinates(element);
9595
image = ImageProcessor.addText(image, coords.getX(), coords.getY() - textFont.getSize() / 2, text, textColor, textFont);
9696
} catch (RasterFormatException rfe) {
97-
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
97+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
9898
}
9999
return this;
100100
}
@@ -119,7 +119,7 @@ public PageSnapshot blur(WebElement element) {
119119
try {
120120
image = ImageProcessor.blurArea(image, new Coordinates(element));
121121
}catch(RasterFormatException rfe) {
122-
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
122+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
123123
}
124124
return this;
125125
}
@@ -135,7 +135,7 @@ public PageSnapshot monochrome(WebElement element) {
135135
try {
136136
image = ImageProcessor.monochromeArea(image, new Coordinates(element));
137137
} catch (RasterFormatException rfe) {
138-
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
138+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE, rfe);
139139
}
140140
return this;
141141
}
@@ -150,7 +150,7 @@ public PageSnapshot blurExcept(WebElement element) {
150150
try{
151151
image = ImageProcessor.blurExceptArea(image, new Coordinates(element));
152152
}catch(RasterFormatException rfe){
153-
throw new ElementOutsideViewportException("Try to apply ScrollStrategy",rfe);
153+
throw new ElementOutsideViewportException(ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE,rfe);
154154
}
155155
return this;
156156
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ public static ElementSnapshot shootElement(WebDriver driver, WebElement element)
6767
Browser browser = new Browser(driver);
6868
ElementSnapshot elementSnapshot = new ElementSnapshot(driver);
6969
browser.scrollToElement(element);
70-
elementSnapshot.setImage(browser.takeScreenshot());
71-
elementSnapshot.setImage(ImageProcessor.getElement(elementSnapshot.getImage(), browser.getBoundingClientRect(element)));
70+
elementSnapshot.setImage(browser.takeScreenshot(),browser.getBoundingClientRect(element));
7271
return elementSnapshot;
7372
}
7473
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
*/
2424
public abstract class Snapshot<T extends Snapshot> {
2525

26-
private static final String extension = "PNG";
26+
private static final String EXTENSION = "PNG";
27+
protected static final String ELEMENT_OUT_OF_VIEWPORT_EX_MESSAGE = "Use ScrollStrategy.HORIZONTALLY, ScrollStrategy.VERTICALLY or ScrollStrategy.BOTH_DIRECTIONS to shoot the element outside the viewport";
2728
protected BufferedImage image;
2829
protected BufferedImage thumbnailImage;
2930
protected WebDriver driver;
3031
private String fileName = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS").format(new Date())
31-
+ "." + extension.toLowerCase();
32+
+ "." + EXTENSION.toLowerCase();
3233
private Path location = Paths.get("./screenshots/");
3334
private String title;
3435

@@ -41,7 +42,7 @@ public abstract class Snapshot<T extends Snapshot> {
4142
*/
4243
public T withName(String name) {
4344
if (name != null) {
44-
fileName = name + "." + extension.toLowerCase();
45+
fileName = name + "." + EXTENSION.toLowerCase();
4546
}
4647
return self();
4748
}
@@ -72,7 +73,7 @@ public T withThumbnail(String path, String name, double scale) {
7273
thumbnailFile.mkdirs();
7374
}
7475
thumbnailImage=ImageProcessor.scale(image,scale);
75-
FileUtil.writeImage(thumbnailImage, extension, thumbnailFile);
76+
FileUtil.writeImage(thumbnailImage, EXTENSION, thumbnailFile);
7677
return self();
7778
}
7879

@@ -133,7 +134,7 @@ public void save() {
133134
if (title != null && !title.isEmpty()) {
134135
image = ImageProcessor.addTitle(image, title, Color.red, new Font("Serif", Font.BOLD, 20));
135136
}
136-
FileUtil.writeImage(image, extension, screenshotFile);
137+
FileUtil.writeImage(image, EXTENSION, screenshotFile);
137138
}
138139

139140
/**

0 commit comments

Comments
 (0)