puppeteer-ruby 0.0.22 → 0.0.23

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,7 +46,7 @@ class Puppeteer::Page
46
46
  @screenshot_task_queue = screenshot_task_queue
47
47
 
48
48
  @workers = {}
49
- @client.on_event 'Target.attachedToTarget' do |event|
49
+ @client.on_event('Target.attachedToTarget') do |event|
50
50
  if event['targetInfo']['type'] != 'worker'
51
51
  # If we don't detach from service workers, they will never die.
52
52
  await @client.send_message('Target.detachFromTarget', sessionId: event['sessionId'])
@@ -56,65 +56,69 @@ class Puppeteer::Page
56
56
  session = Puppeteer::Connection.from_session(@client).session(event['sessionId']) # rubocop:disable Lint/UselessAssignment
57
57
  # const worker = new Worker(session, event.targetInfo.url, this._addConsoleMessage.bind(this), this._handleException.bind(this));
58
58
  # this._workers.set(event.sessionId, worker);
59
- # this.emit(Events.Page.WorkerCreated, worker);
59
+ # this.emit(PageEmittedEvents::WorkerCreated, worker);
60
60
  end
61
- @client.on_event 'Target.detachedFromTarget' do |event|
61
+ @client.on_event('Target.detachedFromTarget') do |event|
62
62
  session_id = event['sessionId']
63
63
  worker = @workers[session_id]
64
64
  next unless worker
65
65
 
66
- emit_event('Events.Page.WorkerDestroyed', worker)
66
+ emit_event(PageEmittedEvents::WorkerDestroyed, worker)
67
67
  @workers.delete(session_id)
68
68
  end
69
69
 
70
- @frame_manager.on_event 'Events.FrameManager.FrameAttached' do |event|
71
- emit_event 'Events.Page.FrameAttached', event
70
+ @frame_manager.on_event(FrameManagerEmittedEvents::FrameAttached) do |event|
71
+ emit_event(PageEmittedEvents::FrameAttached, event)
72
72
  end
73
- @frame_manager.on_event 'Events.FrameManager.FrameDetached' do |event|
74
- emit_event 'Events.Page.FrameDetached', event
73
+ @frame_manager.on_event(FrameManagerEmittedEvents::FrameDetached) do |event|
74
+ emit_event(PageEmittedEvents::FrameDetached, event)
75
75
  end
76
- @frame_manager.on_event 'Events.FrameManager.FrameNavigated' do |event|
77
- emit_event 'Events.Page.FrameNavigated', event
76
+ @frame_manager.on_event(FrameManagerEmittedEvents::FrameNavigated) do |event|
77
+ emit_event(PageEmittedEvents::FrameNavigated, event)
78
78
  end
79
79
 
80
80
  network_manager = @frame_manager.network_manager
81
- network_manager.on_event 'Events.NetworkManager.Request' do |event|
82
- emit_event 'Events.Page.Request', event
81
+ network_manager.on_event(NetworkManagerEmittedEvents::Request) do |event|
82
+ emit_event(PageEmittedEvents::Request, event)
83
83
  end
84
- network_manager.on_event 'Events.NetworkManager.Response' do |event|
85
- emit_event 'Events.Page.Response', event
84
+ network_manager.on_event(NetworkManagerEmittedEvents::Response) do |event|
85
+ emit_event(PageEmittedEvents::Response, event)
86
86
  end
87
- network_manager.on_event 'Events.NetworkManager.RequestFailed' do |event|
88
- emit_event 'Events.Page.RequestFailed', event
87
+ network_manager.on_event(NetworkManagerEmittedEvents::RequestFailed) do |event|
88
+ emit_event(PageEmittedEvents::RequestFailed, event)
89
89
  end
90
- network_manager.on_event 'Events.NetworkManager.RequestFinished' do |event|
91
- emit_event 'Events.Page.RequestFinished', event
90
+ network_manager.on_event(NetworkManagerEmittedEvents::RequestFinished) do |event|
91
+ emit_event(PageEmittedEvents::RequestFinished, event)
92
92
  end
93
93
  @file_chooser_interception_is_disabled = false
94
94
  @file_chooser_interceptors = Set.new
95
95
 
96
- @client.on_event 'Page.domContentEventFired' do |event|
97
- emit_event 'Events.Page.DOMContentLoaded'
96
+ @client.on_event('Page.domContentEventFired') do |event|
97
+ emit_event(PageEmittedEvents::DOMContentLoaded)
98
98
  end
99
- @client.on_event 'Page.loadEventFired' do |event|
100
- emit_event 'Events.Page.Load'
99
+ @client.on_event('Page.loadEventFired') do |event|
100
+ emit_event(PageEmittedEvents::Load)
101
101
  end
102
102
  # client.on('Runtime.consoleAPICalled', event => this._onConsoleAPI(event));
103
103
  # client.on('Runtime.bindingCalled', event => this._onBindingCalled(event));
104
- @client.on_event 'Page.javascriptDialogOpening' do |event|
104
+ @client.on_event('Page.javascriptDialogOpening') do |event|
105
105
  handle_dialog_opening(event)
106
106
  end
107
- # client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails));
108
- # client.on('Inspector.targetCrashed', event => this._onTargetCrashed());
107
+ @client.on_event('Runtime.exceptionThrown') do |exception|
108
+ handle_exception(exception['exceptionDetails'])
109
+ end
110
+ @client.on_event('Inspector.targetCrashed') do |event|
111
+ handle_target_crashed
112
+ end
109
113
  # client.on('Performance.metrics', event => this._emitMetrics(event));
110
- @client.on_event 'Log.entryAdded' do |event|
114
+ @client.on_event('Log.entryAdded') do |event|
111
115
  handle_log_entry_added(event)
112
116
  end
113
- @client.on_event 'Page.fileChooserOpened' do |event|
117
+ @client.on_event('Page.fileChooserOpened') do |event|
114
118
  handle_file_chooser(event)
115
119
  end
116
120
  @target.is_closed_promise.then do
117
- emit_event 'Events.Page.Close'
121
+ emit_event(PageEmittedEvents::Close)
118
122
  @closed = true
119
123
  end
120
124
  end
@@ -128,43 +132,22 @@ class Puppeteer::Page
128
132
  )
129
133
  end
130
134
 
131
- EVENT_MAPPINGS = {
132
- close: 'Events.Page.Close',
133
- # console: 'Events.Page.Console',
134
- dialog: 'Events.Page.Dialog',
135
- domcontentloaded: 'Events.Page.DOMContentLoaded',
136
- # error:
137
- frameattached: 'Events.Page.FrameAttached',
138
- framedetached: 'Events.Page.FrameDetached',
139
- framenavigated: 'Events.Page.FrameNavigated',
140
- load: 'Events.Page.Load',
141
- # metrics: 'Events.Page.Metrics',
142
- # pageerror: 'Events.Page.PageError',
143
- popup: 'Events.Page.Popup',
144
- request: 'Events.Page.Request',
145
- requestfailed: 'Events.Page.RequestFailed',
146
- requestfinished: 'Events.Page.RequestFinished',
147
- response: 'Events.Page.Response',
148
- # workercreated: 'Events.Page.WorkerCreated',
149
- # workerdestroyed: 'Events.Page.WorkerDestroyed',
150
- }
151
-
152
135
  # @param event_name [Symbol]
153
136
  def on(event_name, &block)
154
- unless EVENT_MAPPINGS.has_key?(event_name.to_sym)
155
- raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{EVENT_MAPPINGS.keys.join(", ")}")
137
+ unless PageEmittedEvents.values.include?(event_name.to_s)
138
+ raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{PageEmittedEvents.values.to_a.join(", ")}")
156
139
  end
157
140
 
158
- add_event_listener(EVENT_MAPPINGS[event_name.to_sym], &block)
141
+ super(event_name.to_s, &block)
159
142
  end
160
143
 
161
144
  # @param event_name [Symbol]
162
145
  def once(event_name, &block)
163
- unless EVENT_MAPPINGS.has_key?(event_name.to_sym)
164
- raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{EVENT_MAPPINGS.keys.join(", ")}")
146
+ unless PageEmittedEvents.values.include?(event_name.to_s)
147
+ raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{PageEmittedEvents.values.to_a.join(", ")}")
165
148
  end
166
149
 
167
- observe_first(EVENT_MAPPINGS[event_name.to_sym], &block)
150
+ super(event_name.to_s, &block)
168
151
  end
169
152
 
170
153
  def handle_file_chooser(event)
@@ -211,19 +194,10 @@ class Puppeteer::Page
211
194
 
212
195
  define_async_method :async_wait_for_file_chooser
213
196
 
214
- # /**
215
- # * @param {!{longitude: number, latitude: number, accuracy: (number|undefined)}} options
216
- # */
217
- # async setGeolocation(options) {
218
- # const { longitude, latitude, accuracy = 0} = options;
219
- # if (longitude < -180 || longitude > 180)
220
- # throw new Error(`Invalid longitude "${longitude}": precondition -180 <= LONGITUDE <= 180 failed.`);
221
- # if (latitude < -90 || latitude > 90)
222
- # throw new Error(`Invalid latitude "${latitude}": precondition -90 <= LATITUDE <= 90 failed.`);
223
- # if (accuracy < 0)
224
- # throw new Error(`Invalid accuracy "${accuracy}": precondition 0 <= ACCURACY failed.`);
225
- # await this._client.send('Emulation.setGeolocationOverride', {longitude, latitude, accuracy});
226
- # }
197
+ # @param [Puppeteer::Geolocation]
198
+ def geolocation=(geolocation)
199
+ @client.send_message('Emulation.setGeolocationOverride', geolocation.to_h)
200
+ end
227
201
 
228
202
  attr_reader :javascript_enabled, :target
229
203
 
@@ -238,7 +212,7 @@ class Puppeteer::Page
238
212
  class TargetCrashedError < StandardError; end
239
213
 
240
214
  private def handle_target_crashed
241
- emit_event 'error', TargetCrashedError.new('Page crashed!')
215
+ emit_event(PageEmittedEvents::Error, TargetCrashedError.new('Page crashed!'))
242
216
  end
243
217
 
244
218
  private def handle_log_entry_added(event)
@@ -259,7 +233,7 @@ class Puppeteer::Page
259
233
  url: url,
260
234
  line_number: line_number,
261
235
  )
262
- emit_event('Events.Page.Console',
236
+ emit_event(PageEmittedEvents::Console,
263
237
  Puppeteer::ConsoleMessage.new(level, text, [], console_message_location))
264
238
  end
265
239
  end
@@ -502,7 +476,7 @@ class Puppeteer::Page
502
476
  # * @param {!Protocol.Performance.metricsPayload} event
503
477
  # */
504
478
  # _emitMetrics(event) {
505
- # this.emit(Events.Page.Metrics, {
479
+ # this.emit(PageEmittedEvents::Metrics, {
506
480
  # title: event.title,
507
481
  # metrics: this._buildMetricsObject(event.metrics)
508
482
  # });
@@ -521,15 +495,14 @@ class Puppeteer::Page
521
495
  # return result;
522
496
  # }
523
497
 
524
- # /**
525
- # * @param {!Protocol.Runtime.ExceptionDetails} exceptionDetails
526
- # */
527
- # _handleException(exceptionDetails) {
528
- # const message = helper.getExceptionMessage(exceptionDetails);
529
- # const err = new Error(message);
530
- # err.stack = ''; // Don't report clientside error with a node stack attached
531
- # this.emit(Events.Page.PageError, err);
532
- # }
498
+ class PageError < StandardError ; end
499
+
500
+ private def handle_exception(exception_details)
501
+ message = Puppeteer::ExceptionDetails.new(exception_details).message
502
+ err = PageError.new(message)
503
+ # err.stack = ''; // Don't report clientside error with a node stack attached
504
+ emit_event(PageEmittedEvents::PageError, err)
505
+ end
533
506
 
534
507
  # /**
535
508
  # * @param {!Protocol.Runtime.consoleAPICalledPayload} event
@@ -613,7 +586,7 @@ class Puppeteer::Page
613
586
  # * @param {Protocol.Runtime.StackTrace=} stackTrace
614
587
  # */
615
588
  # _addConsoleMessage(type, args, stackTrace) {
616
- # if (!this.listenerCount(Events.Page.Console)) {
589
+ # if (!this.listenerCount(PageEmittedEvents::Console)) {
617
590
  # args.forEach(arg => arg.dispose());
618
591
  # return;
619
592
  # }
@@ -631,7 +604,7 @@ class Puppeteer::Page
631
604
  # columnNumber: stackTrace.callFrames[0].columnNumber,
632
605
  # } : {};
633
606
  # const message = new ConsoleMessage(type, textTokens.join(' '), args, location);
634
- # this.emit(Events.Page.Console, message);
607
+ # this.emit(PageEmittedEvents::Console, message);
635
608
  # }
636
609
 
637
610
  private def handle_dialog_opening(event)
@@ -643,7 +616,7 @@ class Puppeteer::Page
643
616
  type: dialog_type,
644
617
  message: event['message'],
645
618
  default_value: event['defaultPrompt'])
646
- emit_event('Events.Page.Dialog', dialog)
619
+ emit_event(PageEmittedEvents::Dialog, dialog)
647
620
  end
648
621
 
649
622
  # @return [String]
@@ -726,7 +699,7 @@ class Puppeteer::Page
726
699
 
727
700
  private def session_close_promise
728
701
  @disconnect_promise ||= resolvable_future do |future|
729
- @client.observe_first('Events.CDPSession.Disconnected') do
702
+ @client.observe_first(CDPSessionEmittedEvents::Disconnected) do
730
703
  future.reject(Puppeteer::CDPSession::Error.new('Target Closed'))
731
704
  end
732
705
  end
@@ -746,7 +719,7 @@ class Puppeteer::Page
746
719
  -> (request) { predicate.call(request) }
747
720
  end
748
721
 
749
- wait_for_network_manager_event('Events.NetworkManager.Request',
722
+ wait_for_network_manager_event(NetworkManagerEmittedEvents::Request,
750
723
  predicate: request_predicate,
751
724
  timeout: timeout,
752
725
  )
@@ -780,7 +753,7 @@ class Puppeteer::Page
780
753
  -> (response) { predicate.call(response) }
781
754
  end
782
755
 
783
- wait_for_network_manager_event('Events.NetworkManager.Response',
756
+ wait_for_network_manager_event(NetworkManagerEmittedEvents::Response,
784
757
  predicate: response_predicate,
785
758
  timeout: timeout,
786
759
  )
@@ -1043,6 +1016,12 @@ class Puppeteer::Page
1043
1016
  else
1044
1017
  @client.connection.send_message('Target.closeTarget', targetId: @target.target_id)
1045
1018
  await @target.is_closed_promise
1019
+
1020
+ # @closed sometimes remains false, so wait for @closed = true with 100ms timeout.
1021
+ 25.times do
1022
+ break if @closed
1023
+ sleep 0.004
1024
+ end
1046
1025
  end
1047
1026
  end
1048
1027
 
@@ -68,10 +68,10 @@ class Puppeteer::Target
68
68
  if opener_page.nil? || type != 'page'
69
69
  return true
70
70
  end
71
- # if (!openerPage.listenerCount(Events.Page.Popup))
71
+ # if (!openerPage.listenerCount(PageEmittedEvents::Popup))
72
72
  # return true;
73
73
  popup_page = page
74
- opener_page.emit_event('Events.Page.Popup', popup_page)
74
+ opener_page.emit_event(PageEmittedEvents::Popup, popup_page)
75
75
 
76
76
  true
77
77
  end
@@ -1,3 +1,3 @@
1
1
  class Puppeteer
2
- VERSION = '0.0.22'
2
+ VERSION = '0.0.23'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppeteer-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.0.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - YusukeIwaki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-15 00:00:00.000000000 Z
11
+ date: 2020-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -223,10 +223,13 @@ files:
223
223
  - lib/puppeteer/env.rb
224
224
  - lib/puppeteer/errors.rb
225
225
  - lib/puppeteer/event_callbackable.rb
226
+ - lib/puppeteer/events.rb
227
+ - lib/puppeteer/exception_details.rb
226
228
  - lib/puppeteer/execution_context.rb
227
229
  - lib/puppeteer/file_chooser.rb
228
230
  - lib/puppeteer/frame.rb
229
231
  - lib/puppeteer/frame_manager.rb
232
+ - lib/puppeteer/geolocation.rb
230
233
  - lib/puppeteer/if_present.rb
231
234
  - lib/puppeteer/js_handle.rb
232
235
  - lib/puppeteer/keyboard.rb