Skip to content

Instantly share code, notes, and snippets.

@defx
Created March 25, 2015 14:20
Show Gist options
  • Save defx/beda5522d4a9b28cb446 to your computer and use it in GitHub Desktop.
Save defx/beda5522d4a9b28cb446 to your computer and use it in GitHub Desktop.
Protractor utility function for testing whether an absolutely positioned element is "in view" or not.
function inView(el, fn) {
var location, size;
el.getCssValue('display')
.then(function (display) {
if (display === 'none') {
fn(false);
} else {
return el.getSize()
}
})
.then(function (s) {
if (!s.width || !s.height) {
fn(false);
} else {
size = s
return el.getCssValue('position')
}
})
.then(function (position) {
if( position === "absolute" ){
var location = {
x: 0,
y: 0
};
return el.getCssValue('top').then(function(top){
location.y = top.replace('px', '');
return el.getCssValue('left')
})
.then(function(left){
location.x = left.replace('px', '');
return location
})
}else {
return el.getLocation()
}
})
.then(function (loc) {
if(loc !== undefined) {
if (loc.x + size.width <= 0 || loc.y + size.height <= 0) {
fn(false);
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment