source: other-projects/FileTransfer-WebSocketPair/Themes/themebuilder/bin/phantomjs-1.9.2-windows/examples/page_events.coffee@ 31525

Last change on this file since 31525 was 31525, checked in by ak19, 7 years ago

Nathan provided more stuff: Themes folder contains Sencha's Themebuilder which generates GXT Themes. It includes the .theme and generated .jar files for the project theme.

File size: 3.8 KB
Line 
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.
21printArgs = ->
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 ""
31sys = require("system")
32page = require("webpage").create()
33logResources = false
34step1url = "http://en.wikipedia.org/wiki/DOM_events"
35step2url = "http://en.wikipedia.org/wiki/DOM_events#Event_flow"
36logResources = true if sys.args.length > 1 and sys.args[1] is "-v"
37
38#//////////////////////////////////////////////////////////////////////////////
39page.onInitialized = ->
40 console.log "page.onInitialized"
41 printArgs.apply this, arguments_
42
43page.onLoadStarted = ->
44 console.log "page.onLoadStarted"
45 printArgs.apply this, arguments_
46
47page.onLoadFinished = ->
48 console.log "page.onLoadFinished"
49 printArgs.apply this, arguments_
50
51page.onUrlChanged = ->
52 console.log "page.onUrlChanged"
53 printArgs.apply this, arguments_
54
55page.onNavigationRequested = ->
56 console.log "page.onNavigationRequested"
57 printArgs.apply this, arguments_
58
59if 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_
67page.onClosing = ->
68 console.log "page.onClosing"
69 printArgs.apply this, arguments_
70
71
72# window.console.log(msg);
73page.onConsoleMessage = ->
74 console.log "page.onConsoleMessage"
75 printArgs.apply this, arguments_
76
77
78# window.alert(msg);
79page.onAlert = ->
80 console.log "page.onAlert"
81 printArgs.apply this, arguments_
82
83
84# var confirmed = window.confirm(msg);
85page.onConfirm = ->
86 console.log "page.onConfirm"
87 printArgs.apply this, arguments_
88
89
90# var user_value = window.prompt(msg, default_value);
91page.onPrompt = ->
92 console.log "page.onPrompt"
93 printArgs.apply this, arguments_
94
95
96#//////////////////////////////////////////////////////////////////////////////
97setTimeout (->
98 console.log ""
99 console.log "### STEP 1: Load '" + step1url + "'"
100 page.open step1url
101), 0
102setTimeout (->
103 console.log ""
104 console.log "### STEP 2: Load '" + step2url + "' (load same URL plus FRAGMENT)"
105 page.open step2url
106), 5000
107setTimeout (->
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
116setTimeout (->
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
125setTimeout (->
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
Note: See TracBrowser for help on using the repository browser.