This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Coffeescript version of waitfor.js.
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
### | ||
Wait until the test condition is true or a timeout occurs. Useful for waiting | ||
on a server response or for a ui change (fadeIn, etc.) to occur. | ||
@param testFx javascript condition that evaluates to a boolean, | ||
it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or | ||
as a callback function. | ||
@param message a message describing e.g. the ui change | ||
@param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used. | ||
### | ||
waitFor = (testFx, message, maxtimeOutMillis = 3000) -> | ||
start = new Date().getTime() | ||
condition = false | ||
while (new Date().getTime() - start) < maxtimeOutMillis | ||
phantom.sleep(250) | ||
if typeof(testFx) is "string" | ||
condition = eval testFx | ||
else | ||
condition = testFx() | ||
if condition | ||
break | ||
if not condition | ||
console.log "Timeout: " + message | ||
phantom.exit 1 | ||
else | ||
console.log message | ||
console.log "+++ waitUntil finished in " + (new Date().getTime() - start) + " millis." | ||
|
||
# example use: | ||
if not phantom.state | ||
# load a twitter page | ||
phantom.state = "loaded" | ||
phantom.open "http://twitter.com/#!/senchainc" | ||
else | ||
# click the "sign in" link | ||
$(".signin-link").click() | ||
|
||
# wait for the sign in dialog to pop up | ||
waitFor (-> $("#signin-dropdown").is ":visible"), "The sign-in dialog should be visible" | ||
phantom.exit() |