[31525] | 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 | printArgs = ->
|
---|
| 22 | i = undefined
|
---|
| 23 | ilen = undefined
|
---|
| 24 | i = 0
|
---|
| 25 | ilen = arguments_.length
|
---|
| 26 |
|
---|
| 27 | while i < ilen
|
---|
| 28 | console.log " arguments[" + i + "] = " + JSON.stringify(arguments_[i])
|
---|
| 29 | ++i
|
---|
| 30 | console.log ""
|
---|
| 31 | sys = require("system")
|
---|
| 32 | page = require("webpage").create()
|
---|
| 33 | logResources = false
|
---|
| 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"
|
---|
| 37 |
|
---|
| 38 | #//////////////////////////////////////////////////////////////////////////////
|
---|
| 39 | page.onInitialized = ->
|
---|
| 40 | console.log "page.onInitialized"
|
---|
| 41 | printArgs.apply this, arguments_
|
---|
| 42 |
|
---|
| 43 | page.onLoadStarted = ->
|
---|
| 44 | console.log "page.onLoadStarted"
|
---|
| 45 | printArgs.apply this, arguments_
|
---|
| 46 |
|
---|
| 47 | page.onLoadFinished = ->
|
---|
| 48 | console.log "page.onLoadFinished"
|
---|
| 49 | printArgs.apply this, arguments_
|
---|
| 50 |
|
---|
| 51 | page.onUrlChanged = ->
|
---|
| 52 | console.log "page.onUrlChanged"
|
---|
| 53 | printArgs.apply this, arguments_
|
---|
| 54 |
|
---|
| 55 | page.onNavigationRequested = ->
|
---|
| 56 | console.log "page.onNavigationRequested"
|
---|
| 57 | printArgs.apply this, arguments_
|
---|
| 58 |
|
---|
| 59 | if logResources is true
|
---|
| 60 | page.onResourceRequested = ->
|
---|
| 61 | console.log "page.onResourceRequested"
|
---|
| 62 | printArgs.apply this, arguments_
|
---|
| 63 |
|
---|
| 64 | page.onResourceReceived = ->
|
---|
| 65 | console.log "page.onResourceReceived"
|
---|
| 66 | printArgs.apply this, arguments_
|
---|
| 67 | page.onClosing = ->
|
---|
| 68 | console.log "page.onClosing"
|
---|
| 69 | printArgs.apply this, arguments_
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | # window.console.log(msg);
|
---|
| 73 | page.onConsoleMessage = ->
|
---|
| 74 | console.log "page.onConsoleMessage"
|
---|
| 75 | printArgs.apply this, arguments_
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | # window.alert(msg);
|
---|
| 79 | page.onAlert = ->
|
---|
| 80 | console.log "page.onAlert"
|
---|
| 81 | printArgs.apply this, arguments_
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | # var confirmed = window.confirm(msg);
|
---|
| 85 | page.onConfirm = ->
|
---|
| 86 | console.log "page.onConfirm"
|
---|
| 87 | printArgs.apply this, arguments_
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | # var user_value = window.prompt(msg, default_value);
|
---|
| 91 | page.onPrompt = ->
|
---|
| 92 | console.log "page.onPrompt"
|
---|
| 93 | printArgs.apply this, arguments_
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | #//////////////////////////////////////////////////////////////////////////////
|
---|
| 97 | setTimeout (->
|
---|
| 98 | console.log ""
|
---|
| 99 | console.log "### STEP 1: Load '" + step1url + "'"
|
---|
| 100 | page.open step1url
|
---|
| 101 | ), 0
|
---|
| 102 | setTimeout (->
|
---|
| 103 | console.log ""
|
---|
| 104 | console.log "### STEP 2: Load '" + step2url + "' (load same URL plus FRAGMENT)"
|
---|
| 105 | page.open step2url
|
---|
| 106 | ), 5000
|
---|
| 107 | setTimeout (->
|
---|
| 108 | console.log ""
|
---|
| 109 | console.log "### STEP 3: Click on page internal link (aka FRAGMENT)"
|
---|
| 110 | page.evaluate ->
|
---|
| 111 | ev = document.createEvent("MouseEvents")
|
---|
| 112 | ev.initEvent "click", true, true
|
---|
| 113 | document.querySelector("a[href='#Event_object']").dispatchEvent ev
|
---|
| 114 |
|
---|
| 115 | ), 10000
|
---|
| 116 | setTimeout (->
|
---|
| 117 | console.log ""
|
---|
| 118 | console.log "### STEP 4: Click on page external link"
|
---|
| 119 | page.evaluate ->
|
---|
| 120 | ev = document.createEvent("MouseEvents")
|
---|
| 121 | ev.initEvent "click", true, true
|
---|
| 122 | document.querySelector("a[title='JavaScript']").dispatchEvent ev
|
---|
| 123 |
|
---|
| 124 | ), 15000
|
---|
| 125 | setTimeout (->
|
---|
| 126 | console.log ""
|
---|
| 127 | console.log "### STEP 5: Close page and shutdown (with a delay)"
|
---|
| 128 | page.close()
|
---|
| 129 | setTimeout (->
|
---|
| 130 | phantom.exit()
|
---|
| 131 | ), 100
|
---|
| 132 | ), 20000 |
---|