Skip to content

Commit c20770b

Browse files
deepthought42glibas
authored andcommitted
Added methods to capture element screenshot after scrolling into view vertically centered (#62)
* added script to allow scrolling element to center of screen In some instance using scrollIntoView(true) causes the element to be hidden beneath a fixed element such as a fixed nav bar. Using this setup the element can be scrolled into view so that it is verticlly centered in the screen * refined method signature to not include unnecessary boolean. Also updated comments
1 parent a9dced0 commit c20770b

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ public static PageSnapshot shootPage(WebDriver driver, ScrollStrategy scroll, in
113113
return pageScreenshot;
114114
}
115115

116+
/**
117+
* To be used when need to screenshot particular element by vertically centering it in viewport.
118+
*
119+
* @param driver WebDriver instance
120+
* @param element WebElement instance to be screenshotted
121+
* @return ElementSnapshot instance
122+
*/
123+
public static ElementSnapshot shootElementVerticallyCentered(WebDriver driver, WebElement element) {
124+
return shootElementVerticallyCentered(driver,element,false);
125+
}
126+
116127
/**
117128
* To be used when need to screenshot particular element.
118129
*
@@ -139,4 +150,21 @@ public static ElementSnapshot shootElement(WebDriver driver, WebElement element,
139150
elementSnapshot.setImage(browser.takeScreenshot(),browser.getBoundingClientRect(element));
140151
return elementSnapshot;
141152
}
153+
154+
/**
155+
* To be used when need to screenshot particular element by vertically centering it within viewport.
156+
*
157+
* @param driver WebDriver instance
158+
* @param element WebElement instance to be screen shot
159+
* @param useDevicePixelRatio whether or not take into account device pixel ratio
160+
*
161+
* @return ElementSnapshot instance
162+
*/
163+
public static ElementSnapshot shootElementVerticallyCentered(WebDriver driver, WebElement element, boolean useDevicePixelRatio) {
164+
Browser browser = new Browser(driver, useDevicePixelRatio);
165+
ElementSnapshot elementSnapshot = new ElementSnapshot(driver, browser.getDevicePixelRatio());
166+
browser.scrollToElementVerticalCentered(element);
167+
elementSnapshot.setImage(browser.takeScreenshot(),browser.getBoundingClientRect(element));
168+
return elementSnapshot;
169+
}
142170
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class Browser {
4747
private static final String VIEWPORT_WIDTH_JS = "js/viewport-width.js";
4848
private static final String SCROLL_TO_JS = "js/scroll-to.js";
4949
private static final String SCROLL_INTO_VIEW_JS = "js/scroll-element-into-view.js";
50+
private static final String SCROLL_INTO_VIEW_VERTICAL_CENTERED_JS = "js/scroll-element-into-view-vertical-centered.js";
5051
private static final String CURRENT_SCROLL_Y_JS = "js/get-current-scrollY.js";
5152
private static final String CURRENT_SCROLL_X_JS = "js/get-current-scrollX.js";
5253
private static final String DEVICE_PIXEL_RATIO = "js/get-device-pixel-ratio.js";
@@ -256,6 +257,10 @@ public void scrollToElement(WebElement element) {
256257
executeJsScript(SCROLL_INTO_VIEW_JS, element);
257258
}
258259

260+
public void scrollToElementVerticalCentered(WebElement element) {
261+
executeJsScript(SCROLL_INTO_VIEW_VERTICAL_CENTERED_JS, element);
262+
}
263+
259264
public void scrollTo(int x, int y) {
260265
executeJsScript(SCROLL_TO_JS, x / devicePixelRatio, y / devicePixelRatio);
261266
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arguments[0].scrollIntoView({block: 'center'});

0 commit comments

Comments
 (0)