source: other-projects/FileTransfer-WebSocketPair/Themes/themebuilder/bin/phantomjs-1.9.2-windows/examples/waitfor.js@ 31525

Last change on this file since 31525 was 31525, checked in by ak19, 7 years ago

Nathan provided more stuff: Themes folder contains Sencha's Themebuilder which generates GXT Themes. It includes the .theme and generated .jar files for the project theme.

File size: 2.5 KB
Line 
1/**
2 * Wait until the test condition is true or a timeout occurs. Useful for waiting
3 * on a server response or for a ui change (fadeIn, etc.) to occur.
4 *
5 * @param testFx javascript condition that evaluates to a boolean,
6 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
7 * as a callback function.
8 * @param onReady what to do when testFx condition is fulfilled,
9 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
10 * as a callback function.
11 * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
12 */
13function waitFor(testFx, onReady, timeOutMillis) {
14 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
15 start = new Date().getTime(),
16 condition = false,
17 interval = setInterval(function() {
18 if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
19 // If not time-out yet and condition not yet fulfilled
20 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
21 } else {
22 if(!condition) {
23 // If condition still not fulfilled (timeout but condition is 'false')
24 console.log("'waitFor()' timeout");
25 phantom.exit(1);
26 } else {
27 // Condition fulfilled (timeout and/or condition is 'true')
28 console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
29 typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
30 clearInterval(interval); //< Stop this interval
31 }
32 }
33 }, 250); //< repeat check every 250ms
34};
35
36
37var page = require('webpage').create();
38
39// Open Twitter on 'sencha' profile and, onPageLoad, do...
40page.open("http://twitter.com/#!/sencha", function (status) {
41 // Check for page load success
42 if (status !== "success") {
43 console.log("Unable to access network");
44 } else {
45 // Wait for 'signin-dropdown' to be visible
46 waitFor(function() {
47 // Check in the page if a specific element is now visible
48 return page.evaluate(function() {
49 return $("#signin-dropdown").is(":visible");
50 });
51 }, function() {
52 console.log("The sign-in dialog should be visible now.");
53 phantom.exit();
54 });
55 }
56});
57
Note: See TracBrowser for help on using the repository browser.