| 1 | // The purpose of this is to show how and when events fire, considering 5 steps |
| 2 | // happening as follows: |
| 3 | // |
| 4 | // 1. Load URL |
| 5 | // 2. Load same URL, but adding an internal FRAGMENT to it |
| 6 | // 3. Click on an internal Link, that points to another internal FRAGMENT |
| 7 | // 4. Click on an external Link, that will send the page somewhere else |
| 8 | // 5. Close page |
| 9 | // |
| 10 | // Take particular care when going through the output, to understand when |
| 11 | // things happen (and in which order). Particularly, notice what DOESN'T |
| 12 | // happen during step 3. |
| 13 | // |
| 14 | // If invoked with "-v" it will print out the Page Resources as they are |
| 15 | // Requested and Received. |
| 16 | // |
| 17 | // NOTE.1: The "onConsoleMessage/onAlert/onPrompt/onConfirm" events are |
| 18 | // registered but not used here. This is left for you to have fun with. |
| 19 | // NOTE.2: This script is not here to teach you ANY JavaScript. It's aweful! |
| 20 | // NOTE.3: Main audience for this are people new to PhantomJS. |
| 21 | |
| 22 | "use strict"; |
| 23 | var sys = require("system"), |
| 24 | page = require("webpage").create(), |
| 25 | logResources = false, |
| 26 | step1url = "http://en.wikipedia.org/wiki/DOM_events", |
| 27 | step2url = "http://en.wikipedia.org/wiki/DOM_events#Event_flow"; |
| 28 | |
| 29 | if (sys.args.length > 1 && sys.args[1] === "-v") { |
| 30 | logResources = true; |
| 31 | } |
| 32 | |
| 33 | function printArgs() { |
| 34 | var i, ilen; |
| 35 | for (i = 0, ilen = arguments.length; i < ilen; ++i) { |
| 36 | console.log(" arguments[" + i + "] = " + JSON.stringify(arguments[i])); |
| 37 | } |
| 38 | console.log(""); |
| 39 | } |
| 40 | |
| 41 | //////////////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | page.onInitialized = function() { |
| 44 | console.log("page.onInitialized"); |
| 45 | printArgs.apply(this, arguments); |
| 46 | }; |
| 47 | page.onLoadStarted = function() { |
| 48 | console.log("page.onLoadStarted"); |
| 49 | printArgs.apply(this, arguments); |
| 50 | }; |
| 51 | page.onLoadFinished = function() { |
| 52 | console.log("page.onLoadFinished"); |
| 53 | printArgs.apply(this, arguments); |
| 54 | }; |
| 55 | page.onUrlChanged = function() { |
| 56 | console.log("page.onUrlChanged"); |
| 57 | printArgs.apply(this, arguments); |
| 58 | }; |
| 59 | page.onNavigationRequested = function() { |
| 60 | console.log("page.onNavigationRequested"); |
| 61 | printArgs.apply(this, arguments); |
| 62 | }; |
| 63 | page.onRepaintRequested = function() { |
| 64 | console.log("page.onRepaintRequested"); |
| 65 | printArgs.apply(this, arguments); |
| 66 | }; |
| 67 | |
| 68 | if (logResources === true) { |
| 69 | page.onResourceRequested = function() { |
| 70 | console.log("page.onResourceRequested"); |
| 71 | printArgs.apply(this, arguments); |
| 72 | }; |
| 73 | page.onResourceReceived = function() { |
| 74 | console.log("page.onResourceReceived"); |
| 75 | printArgs.apply(this, arguments); |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | page.onClosing = function() { |
| 80 | console.log("page.onClosing"); |
| 81 | printArgs.apply(this, arguments); |
| 82 | }; |
| 83 | |
| 84 | // window.console.log(msg); |
| 85 | page.onConsoleMessage = function() { |
| 86 | console.log("page.onConsoleMessage"); |
| 87 | printArgs.apply(this, arguments); |
| 88 | }; |
| 89 | |
| 90 | // window.alert(msg); |
| 91 | page.onAlert = function() { |
| 92 | console.log("page.onAlert"); |
| 93 | printArgs.apply(this, arguments); |
| 94 | }; |
| 95 | // var confirmed = window.confirm(msg); |
| 96 | page.onConfirm = function() { |
| 97 | console.log("page.onConfirm"); |
| 98 | printArgs.apply(this, arguments); |
| 99 | }; |
| 100 | // var user_value = window.prompt(msg, default_value); |
| 101 | page.onPrompt = function() { |
| 102 | console.log("page.onPrompt"); |
| 103 | printArgs.apply(this, arguments); |
| 104 | }; |
| 105 | |
| 106 | //////////////////////////////////////////////////////////////////////////////// |
| 107 | |
| 108 | setTimeout(function() { |
| 109 | console.log(""); |
| 110 | console.log("### STEP 1: Load '" + step1url + "'"); |
| 111 | page.open(step1url); |
| 112 | }, 0); |
| 113 | |
| 114 | setTimeout(function() { |
| 115 | console.log(""); |
| 116 | console.log("### STEP 2: Load '" + step2url + "' (load same URL plus FRAGMENT)"); |
| 117 | page.open(step2url); |
| 118 | }, 5000); |
| 119 | |
| 120 | setTimeout(function() { |
| 121 | console.log(""); |
| 122 | console.log("### STEP 3: Click on page internal link (aka FRAGMENT)"); |
| 123 | page.evaluate(function() { |
| 124 | var ev = document.createEvent("MouseEvents"); |
| 125 | ev.initEvent("click", true, true); |
| 126 | document.querySelector("a[href='#Event_object']").dispatchEvent(ev); |
| 127 | }); |
| 128 | }, 10000); |
| 129 | |
| 130 | setTimeout(function() { |
| 131 | console.log(""); |
| 132 | console.log("### STEP 4: Click on page external link"); |
| 133 | page.evaluate(function() { |
| 134 | var ev = document.createEvent("MouseEvents"); |
| 135 | ev.initEvent("click", true, true); |
| 136 | document.querySelector("a[title='JavaScript']").dispatchEvent(ev); |
| 137 | }); |
| 138 | }, 15000); |
| 139 | |
| 140 | setTimeout(function() { |
| 141 | console.log(""); |
| 142 | console.log("### STEP 5: Close page and shutdown (with a delay)"); |
| 143 | page.close(); |
| 144 | setTimeout(function(){ |
| 145 | phantom.exit(); |
| 146 | }, 100); |
| 147 | }, 20000); |