Skip to content

Instantly share code, notes, and snippets.

@maggiesavovska
Created May 23, 2016 06:23
Show Gist options
  • Save maggiesavovska/566889664f3f60338af547a52fbb0d4c to your computer and use it in GitHub Desktop.
Save maggiesavovska/566889664f3f60338af547a52fbb0d4c to your computer and use it in GitHub Desktop.
Scroll and take screenshots
exports.smartScreenshot = function(driver, filepath){
return new Promise(function(resolve, reject){
function scrollAndShoot(position, iteration, repetitions, viewHeight){
driver.scroll(0, position).then(function(){
var p = filepath + '_' + iteration + '.png';
driver.saveScreenshot(p).then(function(){
iteration++;
if(iteration > repetitions){
//todo
//https://wiki.saucelabs.com/display/DOCS/Annotating+Tests+with+the+Sauce+Labs+REST+API
return resolve()
}
scrollAndShoot(position + viewHeight, iteration, repetitions, viewHeight);
}).catch(function(err){reject('unable to save screenshot: ' + err)});
}).catch(function(err){reject('unable to scroll: ' + err)});
};//end scrollAndShoot
driver.getViewportSize('height').then(function(result){
var viewHeight = result;
driver.elements('body > *').then(function(result){
var bottomElement = result.value.pop().ELEMENT;
driver.elementIdName(bottomElement).then(function(result){
driver.elementIdLocation(bottomElement).then(function(result){
var footerLocation = result.value.y;
driver.elementIdSize(bottomElement).then(function(result){
var footerSize = result.value.height;
//currently cannot measure size of page so must do this
// console.log('location', footerLocation);
// console.log('footer size', footerSize);
// console.log('viewHeight', viewHeight);
var pageSize = footerLocation + footerSize;
var repetitions = Math.ceil(pageSize / viewHeight);
// console.log('repetitions', repetitions);
var i = 1;
scrollAndShoot(0, i, repetitions, viewHeight);
});
});
});
});
}).catch(function(err){reject('unable to get height:' + err)});
});//end promise
};//end smart screenshot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment