Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. ./phantomjs "--load-images=true" ./waitfor.js >>./t.txt &
  2.  
  3. /**
  4. * Wait until the test condition is true or a timeout occurs. Useful for waiting
  5. * on a server response or for a ui change (fadeIn, etc.) to occur.
  6. *
  7. * @param testFx javascript condition that evaluates to a boolean,
  8. * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
  9. * as a callback function.
  10. * @param onReady what to do when testFx condition is fulfilled,
  11. * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
  12. * as a callback function.
  13. * @param timeOutMillis the max amount of time to wait. If not specified, 3 sec is used.
  14. */
  15.  
  16. "use strict";
  17. function waitFor(testFx, onReady, timeOutMillis) {
  18. var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5000, //< Default Max Timout is 3s
  19. start = new Date().getTime(),
  20. condition = false,
  21. interval = setInterval(function() {
  22. if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
  23. // If not time-out yet and condition not yet fulfilled
  24. condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
  25. } else {
  26. if(!condition) {
  27. // If condition still not fulfilled (timeout but condition is 'false')
  28. console.log("'waitFor()' timeout");
  29. phantom.exit(1);
  30. } else {
  31. // Condition fulfilled (timeout and/or condition is 'true')
  32. console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
  33. typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
  34. clearInterval(interval); //< Stop this interval
  35. }
  36. }
  37. }, 250); //< repeat check every 250ms
  38. };
  39.  
  40.  
  41. function click(sel){var event=document.createEvent('MouseEvents');event.initMouseEvent('click',1,1,window,1,0,0,0,0,0,0,0,0,0,null);document.querySelector(sel).dispatchEvent(event);}
  42. var page = require('webpage').create();
  43. var file_name="image", i=0;
  44.  
  45. // Open Twitter on 'sencha' profile and, onPageLoad, do...
  46. page.open("https://twitter.com/sencha", function (status) {
  47. // Check for page load success
  48. if (status !== "success") {
  49. console.log("Unable to access network");
  50. } else {
  51. page.evaluate(function(click) {click('a[id="signin-link"]');},click)//,click will be available
  52. // Wait for 'signin-dropdown' to be visible
  53. waitFor(function() {
  54. // Check in the page if a specific element is now visible
  55. return page.evaluate(function() {
  56. return $("div#signin-dropdown").is(":visible");
  57. });
  58. }, function() {
  59. console.log("The sign-in dialog should be visible now.");
  60. phantom.exit();
  61. });
  62. }
  63. page.render('/home/root2/pjs/total__'+file_name+'_snap_'+i+'.png');
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement