webkit_remote 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -2,8 +2,6 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  before_install:
5
- - "export DISPLAY=:99.0"
6
- - "sh -e /etc/init.d/xvfb start"
7
5
  - "sudo sh -c \"echo \\\"deb https://dl.google.com/linux/deb/ stable main\\\" >> /etc/apt/sources.list\""
8
6
  - "sudo wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -"
9
7
  - "sudo apt-get -qq update"
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ gem 'posix-spawn', '>= 0.3.6'
5
5
 
6
6
  group :development do
7
7
  gem 'bundler', '>= 1.2.1'
8
+ gem 'debugger', '>= 1.2.1'
8
9
  gem 'jeweler', '>= 1.8.4'
9
10
  gem 'minitest', '>= 4.1.0'
10
11
  gem 'puma', '>= 1.6.3'
data/Gemfile.lock CHANGED
@@ -1,6 +1,14 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ columnize (0.3.6)
5
+ debugger (1.2.1)
6
+ columnize (>= 0.3.1)
7
+ debugger-linecache (~> 1.1.1)
8
+ debugger-ruby_core_source (~> 1.1.4)
9
+ debugger-linecache (1.1.2)
10
+ debugger-ruby_core_source (>= 1.1.1)
11
+ debugger-ruby_core_source (1.1.4)
4
12
  eventmachine (1.0.0)
5
13
  faye-websocket (0.4.6)
6
14
  eventmachine (>= 0.12.0)
@@ -19,7 +27,7 @@ GEM
19
27
  rack (1.4.1)
20
28
  rack-contrib (1.1.0)
21
29
  rack (>= 0.9.1)
22
- rake (0.9.2.2)
30
+ rake (10.0.1)
23
31
  rdoc (3.12)
24
32
  json (~> 1.4)
25
33
  ruby-prof (0.11.2)
@@ -34,6 +42,7 @@ PLATFORMS
34
42
 
35
43
  DEPENDENCIES
36
44
  bundler (>= 1.2.1)
45
+ debugger (>= 1.2.1)
37
46
  eventmachine (>= 1.0.0)
38
47
  faye-websocket (>= 0.4.6)
39
48
  jeweler (>= 1.8.4)
data/README.md CHANGED
@@ -25,6 +25,8 @@ Currently, the following sections of the
25
25
  have been implemented:
26
26
 
27
27
  * Console
28
+ * DOM (incomplete)
29
+ * Network
28
30
  * Page
29
31
  * Remote
30
32
 
@@ -36,6 +38,23 @@ platform-dependent functionality is launching and shutting down the browser
36
38
  process, everything else should work for any WebKit-based browser that
37
39
  implements the remote debugging protocol.
38
40
 
41
+ Google Chrome can be used in a headless environment with
42
+ [Xvfb](http://en.wikipedia.org/wiki/Xvfb).
43
+
44
+ Install Xvfb on Fedora.
45
+
46
+ ```bash
47
+ sudo yum install xorg-x11-server-Xvfb
48
+ ```
49
+
50
+ Install Xvfb on Ubuntu.
51
+
52
+ ```bash
53
+ sudo apt-get install xvfb
54
+ ```
55
+
56
+ Xvfb is included in OSX.
57
+
39
58
 
40
59
  ## Installation
41
60
 
@@ -123,7 +142,8 @@ client.remote_eval '(function() { console.warn("hello ruby"); })();'
123
142
  Take a look at it.
124
143
 
125
144
  ```ruby
126
- message = client.wait_for(type: WebkitRemote::Event::ConsoleMessage).first
145
+ client.wait_for type: WebkitRemote::Event::ConsoleMessage
146
+ message = client.console_messages.first
127
147
  message.text
128
148
  message.level
129
149
  message.params
@@ -171,6 +191,21 @@ Clean up.
171
191
  client.clear_all
172
192
  ```
173
193
 
194
+ ### Explore the DOM
195
+
196
+ Find a node and inspect its attributes.
197
+
198
+ ```ruby
199
+ node = client.dom_root.query_selector '[name=text]'
200
+ node.attributes['name']
201
+ node.attributes['id']
202
+ ```
203
+
204
+ Get the JavaScript DOM object for the node and explore its properties.
205
+
206
+ ```ruby
207
+ node.js_object.properties['nodeName'].value
208
+ ```
174
209
 
175
210
  ### Close the Browser
176
211
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.4.0
data/lib/webkit_remote.rb CHANGED
@@ -9,6 +9,8 @@ require 'webkit_remote/client.rb'
9
9
  require 'webkit_remote/event.rb'
10
10
  require 'webkit_remote/client/console.rb'
11
11
  require 'webkit_remote/client/console_events.rb'
12
+ require 'webkit_remote/client/dom.rb'
13
+ require 'webkit_remote/client/dom_events.rb'
12
14
  require 'webkit_remote/client/network.rb'
13
15
  require 'webkit_remote/client/network_events.rb'
14
16
  require 'webkit_remote/client/page.rb'
@@ -22,15 +22,31 @@ module Console
22
22
  # @return [WebkitRemote::Client] self
23
23
  def clear_console
24
24
  @rpc.call 'Console.clearMessages'
25
+ console_cleared
25
26
  self
26
27
  end
27
28
 
28
29
  # @return [Boolean] true if the debugger generates Console.* events
29
30
  attr_reader :console_events
30
31
 
32
+ # @return [Array<WebkitRemote::Client::ConsoleMessage>]
33
+ attr_reader :console_messages
34
+
35
+ # @private Called by the ConsoleMessage event constructor
36
+ def console_add_message(message)
37
+ @console_messages << message
38
+ end
39
+
40
+ # @private Called by the ConsoleCleared event constructor.
41
+ def console_cleared
42
+ @console_messages.each(&:release_params)
43
+ @console_messages.clear
44
+ end
45
+
31
46
  # @private Called by the Client constructor to set up Console data.
32
47
  def initialize_console
33
48
  @console_events = false
49
+ @console_messages = []
34
50
  end
35
51
  end # module WebkitRemote::Client::Console
36
52
 
@@ -38,6 +54,121 @@ initializer :initialize_console
38
54
  clearer :clear_console
39
55
  include WebkitRemote::Client::Console
40
56
 
57
+ # Data about an entry in the debugger console.
58
+ class ConsoleMessage
59
+ # @return [String] the message text
60
+ attr_reader :text
61
+
62
+ # @return [Array<WebkitRemote::Client::JsObject>] extra arguments given
63
+ # to the message
64
+ attr_reader :params
65
+
66
+ # @return [Symbol] message severity
67
+ #
68
+ # The documented values are :debug, :error, :log, :tip, and :warning.
69
+ attr_reader :level
70
+
71
+ # @return [Integer] how many times this message was repeated
72
+ attr_accessor :count
73
+
74
+ # @return [Symbol] the component that produced this message
75
+ #
76
+ # The documented values are :console_api, :html, :javascript, :network,
77
+ # :other, :wml, and :xml.
78
+ attr_reader :reason
79
+
80
+ # @return [WebkitRemote::Client::NetworkResource] resource associated with
81
+ # this message
82
+ #
83
+ # This is set for console messages that indicate network errors.
84
+ attr_reader :network_resource
85
+
86
+ # @return [Symbol] the behavior that produced this message
87
+ #
88
+ # The documented values are :assert, :dir, :dirxml, :endGroup, :log,
89
+ # :startGroup, :startGroupCollapsed, and :trace.
90
+ attr_reader :type
91
+
92
+ # @return [String] the URL of the file that caused this message
93
+ attr_reader :source_url
94
+
95
+ # @return [Integer] the line number of the statement that caused this message
96
+ attr_reader :source_line
97
+
98
+ # @return [Array<Hash<Symbol, Object>>] JavaScript stack trace to the
99
+ # statement that caused this message
100
+ attr_reader :stack_trace
101
+
102
+ # @private Use Event#for instead of calling this constructor directly.
103
+ #
104
+ # @param [Hash<String, Object>] the raw JSON for a Message object in the
105
+ # Console domain, returned by a RPC call to a Webkit debugging server
106
+ # @
107
+ def initialize(raw_message, client)
108
+ @level = (raw_message['level'] || 'error').to_sym
109
+ @source_line = raw_message['line'] ? raw_message['line'].to_i : nil
110
+ if raw_message['networkRequestId']
111
+ @network_resource =
112
+ client.network_resource raw_message['networkRequestId']
113
+ else
114
+ @network_resource = nil
115
+ end
116
+ if raw_message['parameters']
117
+ @params = raw_message['parameters'].map do |raw_object|
118
+ WebkitRemote::Client::JsObject.for raw_object, client, nil
119
+ end
120
+ else
121
+ @params = []
122
+ end
123
+ @params.freeze
124
+ @count = raw_message['repeatCount'] ? raw_message['repeatCount'].to_i : 1
125
+ if raw_message['source']
126
+ @reason = raw_message['source'].gsub('-', '_').to_sym
127
+ else
128
+ @reason = :other
129
+ end
130
+ @stack_trace = self.class.parse_stack_trace raw_message['stackTrace']
131
+ @text = raw_message['text']
132
+ @type = raw_message['type'] ? raw_message['type'].to_sym : nil
133
+ @source_url = raw_message['url']
134
+ end
135
+
136
+ # Releases the JavaScript objects referenced by this message's parameters.
137
+ def release_params
138
+ @params.each do |param|
139
+ if param.kind_of?(WebkitRemote::Client::JsObject)
140
+ param.release
141
+ end
142
+ end
143
+ end
144
+
145
+ # Parses a StackTrace object returned by a RPC request.
146
+ #
147
+ # @param [Array<String, Object>] raw_stack_trace the raw StackTrace object
148
+ # in the Console domain returned by a RPC request
149
+ # @return [Array<Symbol, Object>] Ruby-friendly stack trace
150
+ def self.parse_stack_trace(raw_stack_trace)
151
+ return nil unless raw_stack_trace
152
+
153
+ raw_stack_trace.map do |raw_frame|
154
+ frame = {}
155
+ if raw_frame['columnNumber']
156
+ frame[:column] = raw_frame['columnNumber'].to_i
157
+ end
158
+ if raw_frame['lineNumber']
159
+ frame[:line] = raw_frame['lineNumber'].to_i
160
+ end
161
+ if raw_frame['functionName']
162
+ frame[:function] = raw_frame['functionName']
163
+ end
164
+ if raw_frame['url']
165
+ frame[:url] = raw_frame['url']
166
+ end
167
+ frame
168
+ end
169
+ end
170
+ end # class WebkitRemote::Client::ConsoleMessage
171
+
41
172
  end # namespace WebkitRemote::Client
42
173
 
43
174
  end # namespace WebkitRemote
@@ -6,82 +6,39 @@ class Event
6
6
  class ConsoleMessage < WebkitRemote::Event
7
7
  register 'Console.messageAdded'
8
8
 
9
- # @return [String] the message text
10
- attr_reader :text
9
+ # @return [WebkitRemote::Client::ConsoleMessage] the new message
10
+ attr_reader :message
11
11
 
12
- # @return [Array<WebkitRemote::Client::RemoteObject>] extra arguments given
13
- # to the message
14
- attr_reader :params
12
+ # @return [String] the message text
13
+ def text
14
+ @message.text
15
+ end
15
16
 
16
17
  # @return [Symbol] message severity
17
18
  #
18
19
  # The documented values are :debug, :error, :log, :tip, and :warning.
19
- attr_reader :level
20
-
21
- # @return [Integer] how many times this message was repeated
22
- attr_reader :count
20
+ def level
21
+ @message.level
22
+ end
23
23
 
24
24
  # @return [Symbol] the component that produced this message
25
25
  #
26
26
  # The documented values are :console_api, :html, :javascript, :network,
27
27
  # :other, :wml, and :xml.
28
- attr_reader :reason
29
-
30
- # @return [Symbol] the behavior that produced this message
31
- #
32
- # The documented values are :assert, :dir, :dirxml, :endGroup, :log,
33
- # :startGroup, :startGroupCollapsed, and :trace.
34
- attr_reader :type
35
-
36
- # @return [String] the URL of the file that caused this message
37
- attr_reader :source_url
38
-
39
- # @return [Integer] the line number of the statement that caused this message
40
- attr_reader :source_line
41
-
42
- # @return [Array<Hash<Symbol, Object>>] JavaScript stack trace to the
43
- # statement that caused this message
44
- attr_reader :stack_trace
28
+ def reason
29
+ @message.reason
30
+ end
45
31
 
46
32
  # @private Use Event#for instead of calling this constructor directly.
47
33
  def initialize(rpc_event, client)
48
34
  super
49
35
 
50
36
  if raw_message = raw_data['message']
51
- @level = (raw_message['level'] || 'error').to_sym
52
- @source_line = raw_message['line'] ? raw_message['line'].to_i : nil
53
- # TODO(pwnall): parse networkRequestId when Network is implemented
54
- if raw_message['parameters']
55
- @params = raw_message['parameters'].map do |raw_object|
56
- WebkitRemote::Client::RemoteObject.for raw_object, client, nil
57
- end
58
- else
59
- @params = []
60
- end
61
- @params.freeze
62
- @count = raw_message['repeatCount'] ? raw_message['repeatCount'].to_i : 1
63
- if raw_message['source']
64
- @reason = raw_message['source'].gsub('-', '_').to_sym
65
- else
66
- @reason = :other
67
- end
68
- @stack_trace = WebkitRemote::Event::ConsoleMessage.parse_stack_trace(
69
- raw_message['stackTrace'])
70
- @text = raw_message['text']
71
- @type = raw_message['type'] ? raw_message['type'].to_sym : nil
72
- @source_url = raw_message['url']
73
- end
74
-
75
- # NOTE(pwnall): if the client will keep track of console messages, this is
76
- # the right place to call into the client and register the message
77
- end
78
-
79
- # Releases the JavaScript objects referenced by this message's parameters.
80
- def release_params()
81
- @params.each do |param|
82
- if param.kind_of?(WebkitRemote::Client::RemoteObject)
83
- param.release
84
- end
37
+ @message = WebkitRemote::Client::ConsoleMessage.new raw_data['message'],
38
+ client
39
+ client.console_add_message @message
40
+ else
41
+ @message = nil
85
42
  end
86
43
  end
87
44
 
@@ -89,50 +46,25 @@ class ConsoleMessage < WebkitRemote::Event
89
46
  def self.can_reach?(client)
90
47
  client.console_events
91
48
  end
92
-
93
-
94
- # Parses a StackTrace object returned by a RPC request.
95
- #
96
- # @param [Array<String, Object>] raw_stack_trace the raw StackTrace object
97
- # in the Console domain returned by a RPC request
98
- # @return [Array<Symbol, Object>] Ruby-friendly stack trace
99
- def self.parse_stack_trace(raw_stack_trace)
100
- return nil unless raw_stack_trace
101
-
102
- raw_stack_trace.map do |raw_frame|
103
- frame = {}
104
- if raw_frame['columnNumber']
105
- frame[:column] = raw_frame['columnNumber'].to_i
106
- end
107
- if raw_frame['lineNumber']
108
- frame[:line] = raw_frame['lineNumber'].to_i
109
- end
110
- if raw_frame['functionName']
111
- frame[:function] = raw_frame['functionName']
112
- end
113
- if raw_frame['url']
114
- frame[:url] = raw_frame['url']
115
- end
116
- frame
117
- end
118
- end
119
49
  end # class WebkitRemote::Event::ConsoleMessage
120
50
 
121
51
  # Emitted when the same console message is produced repeatedly.
122
52
  class ConsoleMessageRepeated < WebkitRemote::Event
123
53
  register 'Console.messageRepeatCountUpdated'
124
54
 
55
+ # @return [WebkitRemote::Client::ConsoleMessage] the repeated message
56
+ attr_reader :message
57
+
125
58
  # @return [Number] the number of times that the message was repeated
126
59
  attr_reader :count
127
60
 
128
61
  # @private Use Event#for instead of calling this constructor directly.
129
62
  def initialize(rpc_event, client)
130
63
  super
131
- @count = raw_data['count'] ? raw_data['count'].to_i : nil
132
64
 
133
- # NOTE(pwnall): if the client will keep track of console messages, this is
134
- # the right place to call into the client and have it update the repeat
135
- # count of the most recent message that it received
65
+ @message = client.console_messages.last
66
+ @count = raw_data['count'] ? raw_data['count'].to_i : nil
67
+ @message.count = @count if @count
136
68
  end
137
69
 
138
70
  # @private Use Event#can_receive instead of calling this directly.
@@ -148,10 +80,7 @@ class ConsoleCleared < WebkitRemote::Event
148
80
  # @private Use Event#for instead of calling this constructor directly.
149
81
  def initialize(rpc_event, client)
150
82
  super
151
-
152
- # NOTE(pwnall): if the client will keep track of console messages, this is
153
- # the right place to call into the client and have it wipe its message
154
- # list, and call #release_params on the messages
83
+ client.console_cleared
155
84
  end
156
85
 
157
86
  # @private Use Event#can_receive instead of calling this directly.