{"id":1464,"date":"2017-11-05T16:52:56","date_gmt":"2017-11-05T17:22:56","guid":{"rendered":"https:\/\/mind4y.com\/itstuff\/?p=1464"},"modified":"2020-03-19T23:23:04","modified_gmt":"2020-03-19T23:53:04","slug":"phantomjs-adding-timeout-waiting-responses","status":"publish","type":"post","link":"https:\/\/ultering.com\/it4us\/?p=1464","title":{"rendered":"PHANTOMJS: ADDING TIMEOUT FOR WAITING RESPONSES"},"content":{"rendered":"<pre>\/**\r\n * Wait until the test condition is true or a timeout occurs. Useful for waiting\r\n * on a server response or for a ui change (fadeIn, etc.) to occur.\r\n *\r\n * @param testFx javascript condition that evaluates to a boolean,\r\n * it can be passed in as a string (e.g.: \"1 == 1\" or \"$('#bar').is(':visible')\" or\r\n * as a callback function.\r\n * @param onReady what to do when testFx condition is fulfilled,\r\n * it can be passed in as a string (e.g.: \"1 == 1\" or \"$('#bar').is(':visible')\" or\r\n * as a callback function.\r\n * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.\r\n *\/\r\n\r\n\"use strict\";\r\nfunction waitFor(testFx, onReady, timeOutMillis) {\r\n    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, \/\/&lt; Default Max Timout is 3s\r\n        start = new Date().getTime(),\r\n        condition = false,\r\n        interval = setInterval(function() {\r\n            if ( (new Date().getTime() - start &lt; maxtimeOutMillis) &amp;&amp; !condition ) {\r\n                \/\/ If not time-out yet and condition not yet fulfilled\r\n                condition = (typeof(testFx) === \"string\" ? eval(testFx) : testFx()); \/\/&lt; defensive code\r\n            } else {\r\n                if(!condition) {\r\n                    \/\/ If condition still not fulfilled (timeout but condition is 'false')\r\n                    console.log(\"'waitFor()' timeout\");\r\n                    phantom.exit(1);\r\n                } else {\r\n                    \/\/ Condition fulfilled (timeout and\/or condition is 'true')\r\n                    console.log(\"'waitFor()' finished in \" + (new Date().getTime() - start) + \"ms.\");\r\n                    typeof(onReady) === \"string\" ? eval(onReady) : onReady(); \/\/&lt; Do what it's supposed to do once the condition is fulfilled\r\n                    clearInterval(interval); \/\/&lt; Stop this interval\r\n                }\r\n            }\r\n        }, 250); \/\/&lt; repeat check every 250ms\r\n};\r\n\r\n\r\nvar page = require('webpage').create();\r\n\r\n\/\/ Open Twitter on 'sencha' profile and, onPageLoad, do...\r\npage.open(\"http:\/\/twitter.com\/#!\/sencha\", function (status) {\r\n    \/\/ Check for page load success\r\n    if (status !== \"success\") {\r\n        console.log(\"Unable to access network\");\r\n    } else {\r\n        \/\/ Wait for 'signin-dropdown' to be visible\r\n        waitFor(function() {\r\n            \/\/ Check in the page if a specific element is now visible\r\n            return page.evaluate(function() {\r\n                return $(\"#signin-dropdown\").is(\":visible\");\r\n            });\r\n        }, function() {\r\n           console.log(\"The sign-in dialog should be visible now.\");\r\n           phantom.exit();\r\n        });\r\n    }\r\n});\r\n<\/pre>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\/** * 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.: &#8220;1 == 1&#8221; or &#8230; <a href=\"https:\/\/ultering.com\/it4us\/?p=1464\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &#8220;PHANTOMJS: ADDING TIMEOUT FOR WAITING RESPONSES&#8221;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[26],"tags":[10,29],"class_list":["post-1464","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-phantomjs"],"_links":{"self":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/1464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1464"}],"version-history":[{"count":1,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/1464\/revisions"}],"predecessor-version":[{"id":2638,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/1464\/revisions\/2638"}],"wp:attachment":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}