Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. function waitFor(testFx, onReady, timeOutMillis) {
  2. var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
  3. start = new Date().getTime(),
  4. condition = false,
  5. interval = setInterval(function() {
  6. if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
  7. // If not time-out yet and condition not yet fulfilled
  8. condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
  9. } else {
  10. if(!condition) {
  11. // If condition still not fulfilled (timeout but condition is 'false')
  12. console.log("'waitFor()' timeout");
  13. phantom.exit(1);
  14. } else {
  15. // Condition fulfilled (timeout and/or condition is 'true')
  16. console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
  17. typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
  18. clearInterval(interval); //< Stop this interval
  19. }
  20. }
  21. }, 250); //< repeat check every 250ms
  22. };
  23.  
  24. var page = require('webpage').create(),
  25. system = require('system'),
  26. t, address;
  27.  
  28. if (system.args.length === 1) {
  29. console.log('Usage: loadspeed.js <some URL>');
  30. phantom.exit();
  31. }
  32.  
  33. t = Date.now();
  34. address = system.args[1];
  35.  
  36. page.open(address, function(status) {
  37. if (status !== 'success') {
  38. console.log('FAIL to load the address');
  39. } else {
  40. t = Date.now() - t;
  41. console.log('Loading time ' + t + ' msec');
  42. waitFor(function() {
  43. var btn = page.evaluate(function() {
  44. return Ext.getCmp('export_to_excel_btn');
  45. });
  46. return btn !== null;
  47. },
  48. function() {
  49. var xl;
  50. waitFor(function() {
  51. xl = page.evaluate(function() {
  52. var btn = Ext.getCmp('export_to_excel_btn');
  53. return btn.handler(); // Never works.
  54. });
  55. console.log(xl)
  56. return xl !== null;
  57. },
  58. function() {
  59. console.log('In the inner waitFor');
  60. phantom.exit();
  61. }, 10000);
  62. }, 10000);
  63. }
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement