Last active
January 2, 2019 12:24
-
-
Save TuHuynhVan/55854776e18cd56c4dfdf7344ae764d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* To scroll into view for an element as a workaround for chromedriver issue | |
* @param {Object} selector - CSS selector only | |
* | |
* */ | |
export default function scrollToElement(selector) { | |
let viewportObj = browser.getViewportSize(); | |
let viewportHeight = viewportObj.height; | |
let viewportWidth = viewportObj.width; | |
let topLeft = {height: 0, width: 0}; | |
let topRight = {height: 0, width: viewportWidth}; | |
let bottomLeft = {height: viewportHeight, width: 0}; | |
let bottomRight = {height: viewportHeight, width: viewportWidth}; | |
let middleLeftSide = {height: viewportHeight / 2, width: 0}; | |
let middleRightSide = {height: viewportHeight / 2, width: viewportWidth}; | |
let inspectionArea = [topLeft, topRight, bottomLeft, bottomRight, middleLeftSide, middleRightSide]; | |
browser.execute((inspectionArea, selector) => { | |
let isInViewport = elem => { | |
let bounding = elem.getBoundingClientRect(); | |
return ( | |
bounding.top >= 0 && | |
bounding.left >= 0 && | |
bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) && | |
bounding.right <= (window.innerWidth || document.documentElement.clientWidth) | |
); | |
}; | |
for (let i = 0; i < inspectionArea.length; i++) { | |
window.scrollTo(inspectionArea[i].width, inspectionArea[i].height); | |
let avail = isInViewport(document.querySelector(selector)); | |
if (avail) { | |
return; | |
} | |
} | |
}, inspectionArea, selector); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment