Skip to content

Instantly share code, notes, and snippets.

@AamuLumi
Created April 25, 2017 07:58
Show Gist options
  • Save AamuLumi/7a310295b1654eeb260296e350f159a7 to your computer and use it in GitHub Desktop.
Save AamuLumi/7a310295b1654eeb260296e350f159a7 to your computer and use it in GitHub Desktop.
Nightmare action to check if an element is in the viewport
Nightmare.action('inViewport', function(selector, done) {
this.evaluate_now(function(selector) {
var element = document.querySelector(selector);
if (!element){
return false;
}
var rect = element.getBoundingClientRect();
var height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
return !(rect.top >= height || rect.bottom <= 0 ||
rect.left >= width || rect.right <= 0);
}, done, selector);
});
// Example with chai.js
nightmare
.inViewport('.my-button')
.then((isInViewport) => {
expect(isInViewport).to.be.true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment