Created
February 9, 2016 00:00
-
-
Save wassname/26e7b730cd1588adf663 to your computer and use it in GitHub Desktop.
Wait for a jQuery selector in the browser using promises
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
/** | |
* This is too show the use of waiting for a jquery selector in the browser | |
* using promises. Much like how webdriverio works from outside the browser. | |
* | |
* Tested with jQuery v1.11.3 and chrome Version 48.0.* (64-bit) | |
**/ | |
/** | |
* Wait for a jquery css selector | |
* @param {String} selector - Css selector String | |
* @param {Integer} timeout - max time ot wait in milliseconds | |
* @return {Array} - Jquery selections | |
*/ | |
function waitForExists(selector, timeout, interval) { | |
// init | |
if (interval===undefined) interval=250; | |
if (timeout===undefined) timeout=15000; | |
var start = Date.now(); | |
/** check is exists, or for timeout, or call itself **/ | |
function checkExists(resolve, reject) { | |
if (selector && $(selector).length) { | |
console.debug('Succeeded in ', (Date.now() - start),', checkExists:',selector); | |
return resolve($(selector)); | |
} else if (timeout && (Date.now() - start) >= timeout) | |
return reject(new Error('Timed out in '+timeout+', checkExists:'+selector)); | |
else | |
return setTimeout(checkExists.bind(this, resolve, reject), interval); | |
} | |
return new Promise(checkExists); | |
} | |
function waitForNotExists(selector, timeout, interval) { | |
// init | |
if (interval===undefined) interval=250; | |
if (timeout===undefined) timeout=15000; | |
var start = Date.now(); | |
/** check is not exists, or for timeout, or call itself **/ | |
function checkNotExists(resolve, reject) { | |
if (selector && !$(selector).length) { | |
console.debug('Succeeded in ', (Date.now() - start),', checkNotExists:',selector); | |
return resolve($(selector)); | |
} else if (timeout && (Date.now() - start) >= timeout) | |
return reject(new Error('Timed out in '+timeout+', checkNotExists:'+selector)); | |
else | |
return setTimeout(checkNotExists.bind(this, resolve, reject), interval); | |
} | |
return new Promise(checkNotExists); | |
} | |
/** Utility to click a selector when it exists **/ | |
function clickWhenExists(selector, timeout, interval) { | |
return waitForExists(selector, timeout, interval) | |
.then($dom => { | |
return $dom.click(); | |
}); | |
} | |
/** Utility to click a selector if it exists **/ | |
function clickIfExists(selector, reject) { | |
var $dom = $(selector); | |
if ($dom.length){ | |
console.debug('clickIfExists '+'found selector '+selector); | |
return $dom.click(); | |
} else { | |
console.debug('clickIfExists '+'did not find selector '+selector); | |
return reject ? reject($dom): false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment