Skip to content

Commit 5970423

Browse files
committed
Add exception on element outside viewport; prevent from screlling if full page screen already taken
1 parent b3f26ad commit 5970423

File tree

6 files changed

+84
-20
lines changed

6 files changed

+84
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class ElementSnapshot extends Snapshot {
1515

16-
ElementSnapshot(WebDriver driver, WebElement element) {
16+
ElementSnapshot(WebDriver driver) {
1717
this.driver = driver;
1818
}
1919

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

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

66
package com.assertthat.selenium_shutterbug.core;
77

8+
import com.assertthat.selenium_shutterbug.utils.web.ElementOutsideViewportException;
89
import com.assertthat.selenium_shutterbug.utils.image.ImageProcessor;
910
import com.assertthat.selenium_shutterbug.utils.web.Coordinates;
1011
import org.openqa.selenium.WebDriver;
1112
import org.openqa.selenium.WebElement;
1213

1314
import java.awt.*;
15+
import java.awt.image.RasterFormatException;
1416

1517
/**
1618
* Created by Glib_Briia on 17/06/2016.
@@ -29,7 +31,11 @@ public class PageSnapshot extends Snapshot {
2931
* @return instance of type PageSnapshot
3032
*/
3133
public PageSnapshot highlight(WebElement element) {
32-
highlight(element, Color.red, 3);
34+
try {
35+
highlight(element, Color.red, 3);
36+
} catch (RasterFormatException rfe) {
37+
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
38+
}
3339
return this;
3440
}
3541

@@ -43,7 +49,11 @@ public PageSnapshot highlight(WebElement element) {
4349
* @return instance of type PageSnapshot
4450
*/
4551
public PageSnapshot highlight(WebElement element, Color color, int lineWidth) {
46-
image = ImageProcessor.highlight(image, new Coordinates(element), color, lineWidth);
52+
try {
53+
image = ImageProcessor.highlight(image, new Coordinates(element), color, lineWidth);
54+
} catch (RasterFormatException rfe) {
55+
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
56+
}
4757
return this;
4858
}
4959

@@ -58,7 +68,11 @@ public PageSnapshot highlight(WebElement element, Color color, int lineWidth) {
5868
* @return instance of type PageSnapshot
5969
*/
6070
public PageSnapshot highlightWithText(WebElement element, String text) {
61-
highlightWithText(element, Color.red, 3, text, Color.red, new Font("Serif", Font.BOLD, 20));
71+
try {
72+
highlightWithText(element, Color.red, 3, text, Color.red, new Font("Serif", Font.BOLD, 20));
73+
} catch (RasterFormatException rfe) {
74+
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
75+
}
6276
return this;
6377
}
6478

@@ -75,9 +89,13 @@ public PageSnapshot highlightWithText(WebElement element, String text) {
7589
* @return instance of type PageSnapshot
7690
*/
7791
public PageSnapshot highlightWithText(WebElement element, Color elementColor, int lineWidth, String text, Color textColor, Font textFont) {
78-
highlight(element, elementColor, 0);
79-
Coordinates coords = new Coordinates(element);
80-
image = ImageProcessor.addText(image, coords.getX(), coords.getY() - textFont.getSize() / 2, text, textColor, textFont);
92+
try {
93+
highlight(element, elementColor, 0);
94+
Coordinates coords = new Coordinates(element);
95+
image = ImageProcessor.addText(image, coords.getX(), coords.getY() - textFont.getSize() / 2, text, textColor, textFont);
96+
} catch (RasterFormatException rfe) {
97+
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
98+
}
8199
return this;
82100
}
83101

@@ -98,7 +116,11 @@ public PageSnapshot blur() {
98116
* @return instance of type PageSnapshot
99117
*/
100118
public PageSnapshot blur(WebElement element) {
101-
image = ImageProcessor.blurArea(image, new Coordinates(element));
119+
try {
120+
image = ImageProcessor.blurArea(image, new Coordinates(element));
121+
}catch(RasterFormatException rfe) {
122+
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
123+
}
102124
return this;
103125
}
104126

@@ -110,7 +132,11 @@ public PageSnapshot blur(WebElement element) {
110132
* @return instance of type PageSnapshot
111133
*/
112134
public PageSnapshot monochrome(WebElement element) {
113-
image = ImageProcessor.monochromeArea(image, new Coordinates(element));
135+
try {
136+
image = ImageProcessor.monochromeArea(image, new Coordinates(element));
137+
} catch (RasterFormatException rfe) {
138+
throw new ElementOutsideViewportException("Try to apply ScrollStrategy", rfe);
139+
}
114140
return this;
115141
}
116142

@@ -121,7 +147,11 @@ public PageSnapshot monochrome(WebElement element) {
121147
* @return instance of type PageSnapshot
122148
*/
123149
public PageSnapshot blurExcept(WebElement element) {
124-
image = ImageProcessor.blurExceptArea(image, new Coordinates(element));
150+
try{
151+
image = ImageProcessor.blurExceptArea(image, new Coordinates(element));
152+
}catch(RasterFormatException rfe){
153+
throw new ElementOutsideViewportException("Try to apply ScrollStrategy",rfe);
154+
}
125155
return this;
126156
}
127157

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll) {
6565
*/
6666
public static ElementSnapshot shootElement(WebDriver driver, WebElement element) {
6767
Browser browser = new Browser(driver);
68-
ElementSnapshot elementScreenshot = new ElementSnapshot(driver, element);
68+
ElementSnapshot elementSnapshot = new ElementSnapshot(driver);
6969
browser.scrollToElement(element);
70-
elementScreenshot.setImage(browser.takeScreenshot());
71-
elementScreenshot.setImage(ImageProcessor.getElement(elementScreenshot.getImage(), browser.getBoundingClientRect(element)));
72-
return elementScreenshot;
70+
elementSnapshot.setImage(browser.takeScreenshot());
71+
elementSnapshot.setImage(ImageProcessor.getElement(elementSnapshot.getImage(), browser.getBoundingClientRect(element)));
72+
return elementSnapshot;
7373
}
7474
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public T withTitle(String title) {
6868
*/
6969
public T withThumbnail(String path, String name, double scale) {
7070
File thumbnailFile = new File(path.toString(), name);
71-
if(Files.exists(Paths.get(path))) {
71+
if(!Files.exists(Paths.get(path))) {
7272
thumbnailFile.mkdirs();
7373
}
7474
thumbnailImage=ImageProcessor.scale(image,scale);
@@ -127,7 +127,7 @@ protected void setImage(BufferedImage image) {
127127
*/
128128
public void save() {
129129
File screenshotFile = new File(location.toString(), fileName);
130-
if(Files.exists(location)) {
130+
if(!Files.exists(location)) {
131131
screenshotFile.mkdirs();
132132
}
133133
if (title != null && !title.isEmpty()) {

src/main/java/com/assertthat/selenium_shutterbug/utils/web/Browser.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,17 @@ public BufferedImage takeScreenshotEntirePage() {
6666
Graphics2D g = combinedImage.createGraphics();
6767
int horizontalIterations = (int) Math.ceil(((double) this.getDocWidth()) / this.getViewportWidth());
6868
int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight());
69+
outerloop:
6970
for (int j = 0; j < verticalIterations; j++) {
7071
this.scrollTo(0, j * this.getViewportHeight());
7172
for (int i = 0; i < horizontalIterations; i++) {
7273
this.scrollTo(i * this.getViewportWidth(), this.getViewportHeight() * j);
7374
wait(50);
74-
g.drawImage(takeScreenshot(), this.getCurrentScrollX(), this.getCurrentScrollY(), null);
75+
Image image = takeScreenshot();
76+
g.drawImage(image, this.getCurrentScrollX(), this.getCurrentScrollY(), null);
77+
if(this.getDocWidth() == image.getWidth(null) && this.getDocHeight() == image.getHeight(null)){
78+
break outerloop;
79+
}
7580
}
7681
}
7782
g.dispose();
@@ -84,7 +89,11 @@ public BufferedImage takeScreenshotScrollHorizontally() {
8489
int horizontalIterations = (int) Math.ceil(((double) this.getDocWidth()) / this.getViewportWidth());
8590
for (int i = 0; i < horizontalIterations; i++) {
8691
this.scrollTo(i * this.getViewportWidth(), 0);
87-
g.drawImage(takeScreenshot(), this.getCurrentScrollX(), 0, null);
92+
Image image = takeScreenshot();
93+
g.drawImage(image, this.getCurrentScrollX(), 0, null);
94+
if(this.getDocWidth() == image.getWidth(null)){
95+
break;
96+
}
8897
}
8998
g.dispose();
9099
return combinedImage;
@@ -96,8 +105,11 @@ public BufferedImage takeScreenshotScrollVertically() {
96105
int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight());
97106
for (int j = 0; j < verticalIterations; j++) {
98107
this.scrollTo(0, j * this.getViewportHeight());
99-
g.drawImage(takeScreenshot(), 0, this.getCurrentScrollY(), null);
100-
108+
Image image = takeScreenshot();
109+
g.drawImage(image, 0, this.getCurrentScrollY(), null);
110+
if(this.getDocHeight() == image.getHeight(null)){
111+
break;
112+
}
101113
}
102114
g.dispose();
103115
return combinedImage;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.assertthat.selenium_shutterbug.utils.web;
2+
3+
/**
4+
* Created by Glib_Briia on 10/07/2016.
5+
*/
6+
public class ElementOutsideViewportException extends RuntimeException {
7+
public ElementOutsideViewportException() {
8+
super();
9+
}
10+
11+
public ElementOutsideViewportException(String message) {
12+
super(message);
13+
}
14+
15+
public ElementOutsideViewportException(Throwable cause) {
16+
super(cause);
17+
}
18+
19+
public ElementOutsideViewportException(String message, Throwable cause) {
20+
super(message, cause);
21+
}
22+
}

0 commit comments

Comments
 (0)