1 # The purpose of this is to show how and when events fire, considering 5 steps
2 # happening as follows:
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
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.
14 # If invoked with "-v" it will print out the Page Resources as they are
15 # Requested and Received.
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.
25 ilen = arguments_.length
28 console.log " arguments[" + i + "] = " + JSON.stringify(arguments_[i])
31 sys = require("system")
32 page = require("webpage").create()
34 step1url = "http://en.wikipedia.org/wiki/DOM_events"
35 step2url = "http://en.wikipedia.org/wiki/DOM_events#Event_flow"
36 logResources = true if sys.args.length > 1 and sys.args[1] is "-v"
38 #//////////////////////////////////////////////////////////////////////////////
39 page.onInitialized = ->
40 console.log "page.onInitialized"
41 printArgs.apply this, arguments_
43 page.onLoadStarted = ->
44 console.log "page.onLoadStarted"
45 printArgs.apply this, arguments_
47 page.onLoadFinished = ->
48 console.log "page.onLoadFinished"
49 printArgs.apply this, arguments_
51 page.onUrlChanged = ->
52 console.log "page.onUrlChanged"
53 printArgs.apply this, arguments_
55 page.onNavigationRequested = ->
56 console.log "page.onNavigationRequested"
57 printArgs.apply this, arguments_
59 if logResources is true
60 page.onResourceRequested = ->
61 console.log "page.onResourceRequested"
62 printArgs.apply this, arguments_
64 page.onResourceReceived = ->
65 console.log "page.onResourceReceived"
66 printArgs.apply this, arguments_
68 console.log "page.onClosing"
69 printArgs.apply this, arguments_
72 # window.console.log(msg);
73 page.onConsoleMessage = ->
74 console.log "page.onConsoleMessage"
75 printArgs.apply this, arguments_
80 console.log "page.onAlert"
81 printArgs.apply this, arguments_
84 # var confirmed = window.confirm(msg);
86 console.log "page.onConfirm"
87 printArgs.apply this, arguments_
90 # var user_value = window.prompt(msg, default_value);
92 console.log "page.onPrompt"
93 printArgs.apply this, arguments_
96 #//////////////////////////////////////////////////////////////////////////////
99 console.log "### STEP 1: Load '" + step1url + "'"
104 console.log "### STEP 2: Load '" + step2url + "' (load same URL plus FRAGMENT)"
109 console.log "### STEP 3: Click on page internal link (aka FRAGMENT)"
111 ev = document.createEvent("MouseEvents")
112 ev.initEvent "click", true, true
113 document.querySelector("a[href='#Event_object']").dispatchEvent ev
118 console.log "### STEP 4: Click on page external link"
120 ev = document.createEvent("MouseEvents")
121 ev.initEvent "click", true, true
122 document.querySelector("a[title='JavaScript']").dispatchEvent ev
127 console.log "### STEP 5: Close page and shutdown (with a delay)"