Harrington Web

Scrolling to place on webpage for Protractor or Webdriver testing.

Monday, October 26, 2015
Sometimes when using a Selenium by product like Protractor for testing Angular applications, I run into issues trying to access certain elements. In some cases it is because our UI folks may have another element that hovers over an area rather than actually take up that space. In cases like these I need to move the content on the page similar to how a user would so that the content I need is the front most element on the page. To do this I made a simple scrollTo function that will take a Selenium element and tell the browser with executeScript to goto those coordinates. I also have an offset so that I can move it around the would be elements that cover my element.

function scrollToElement(element, offset) {
  offset = offset || 0;
  element.getLocation()
    .then( function(loc) {
      browser.executeScript(('window.scrollTo('+loc.x+','+ (loc.y - offset) +');'))
    });
}