Created
April 25, 2017 07:58
-
-
Save AamuLumi/7a310295b1654eeb260296e350f159a7 to your computer and use it in GitHub Desktop.
Nightmare action to check if an element is in the viewport
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
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