Last active
August 29, 2015 13:56
-
-
Save davidworkman9/9063210 to your computer and use it in GitHub Desktop.
wrapping webdriverjs's waitFor to act differently if passed an instance of JQuerySelector.
This file contains hidden or 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
(function () { | |
var methods = ['children', 'closest', 'find', | |
'next', 'nextAll', 'nextUntil', | |
'offsetParent', 'parent', 'parents', | |
'parentsUntil', 'prev', 'prevAll', | |
'prevUntil', 'siblings' | |
]; | |
_.each(methods, function (method) { | |
JQuerySelector.prototype[method] = function (selector) { | |
this.methods.push({ method: method, selector: selector }); | |
return this; | |
}; | |
}); | |
JQuerySelector.prototype._getMethods = function () { | |
return _.map(this.methods, function (obj) { | |
var selector = obj.selector ? '"' + obj.selector.replace(/"/g, '\\"') + '"' : ''; | |
return '.' + obj.method + '(' + selector + ')'; | |
}); | |
}; | |
JQuerySelector.prototype.toString = function () { | |
return '$("' + this.selector + '")' + this._getMethods().join(''); | |
}; | |
webdriverjs.prototype.jQuerySelector = function (selector) { | |
return new JQuerySelector(selector); | |
}; | |
var oldElement = webdriverjs.prototype.element; | |
webdriverjs.prototype.element = function (value, callback) { | |
var methodCalls; | |
if (value instanceof JQuerySelector) { | |
this.execute('return '+ value.toString() + '.get(0);', callback); | |
} else { | |
oldElement.apply(this, arguments); | |
} | |
}; | |
})(); | |
function JQuerySelector (selector) { | |
this.selector = selector.replace(/"/g, '\\"'); | |
this.methods = []; | |
} |
This file contains hidden or 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
(function () { | |
var waitForOld = webdriverjs.prototype.waitFor; | |
webdriverjs.prototype.waitFor = function (selector, ms, callback) { | |
var wrappedCallback = function (err, result) { | |
if (result) { | |
if (! (result.value && result.value.ELEMENT) ) { | |
result = null; | |
err = 'Timed out waiting ' + ms + ' ms for element by selector: ' + selector; | |
} | |
} | |
if (typeof callback === 'function') { | |
callback.call(this, err, result); | |
} | |
}; | |
if (selector instanceof JQuerySelector) { | |
var timeBetweenChecks = 50, | |
timesToCheck = ms / timeBetweenChecks; | |
this.timeouts('script', ms) | |
.executeAsync(function (selector, timesToCheck, timeBetweenChecks, cb) { | |
var timesChecked = 0; | |
var handle = setInterval(function () { | |
timesChecked++; | |
var el = eval(selector); | |
el = el.get(0); | |
if (el) { | |
cb(el); | |
} else if (timesChecked >= timesToCheck) { | |
clearInterval(handle); | |
} | |
}, timeBetweenChecks); | |
}, [selector.toString(), timesToCheck, timeBetweenChecks], wrappedCallback); | |
return this; | |
} else { | |
return waitForOld.call(this, selector, ms, wrappedCallback); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment