puppeteer-ruby 0.35.0 → 0.37.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ require_relative './device'
2
+
1
3
  Puppeteer::DEVICES = Hash[
2
4
  [
3
5
  {
@@ -156,6 +158,84 @@ Puppeteer::DEVICES = Hash[
156
158
  isLandscape: true,
157
159
  },
158
160
  },
161
+ {
162
+ name: 'Galaxy S8',
163
+ userAgent:
164
+ 'Mozilla/5.0 (Linux; Android 7.0; SM-G950U Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36',
165
+ viewport: {
166
+ width: 360,
167
+ height: 740,
168
+ deviceScaleFactor: 3,
169
+ isMobile: true,
170
+ hasTouch: true,
171
+ isLandscape: false,
172
+ },
173
+ },
174
+ {
175
+ name: 'Galaxy S8 landscape',
176
+ userAgent:
177
+ 'Mozilla/5.0 (Linux; Android 7.0; SM-G950U Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36',
178
+ viewport: {
179
+ width: 740,
180
+ height: 360,
181
+ deviceScaleFactor: 3,
182
+ isMobile: true,
183
+ hasTouch: true,
184
+ isLandscape: true,
185
+ },
186
+ },
187
+ {
188
+ name: 'Galaxy S9+',
189
+ userAgent:
190
+ 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G965U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.111 Mobile Safari/537.36',
191
+ viewport: {
192
+ width: 320,
193
+ height: 658,
194
+ deviceScaleFactor: 4.5,
195
+ isMobile: true,
196
+ hasTouch: true,
197
+ isLandscape: false,
198
+ },
199
+ },
200
+ {
201
+ name: 'Galaxy S9+ landscape',
202
+ userAgent:
203
+ 'Mozilla/5.0 (Linux; Android 8.0.0; SM-G965U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.111 Mobile Safari/537.36',
204
+ viewport: {
205
+ width: 658,
206
+ height: 320,
207
+ deviceScaleFactor: 4.5,
208
+ isMobile: true,
209
+ hasTouch: true,
210
+ isLandscape: true,
211
+ },
212
+ },
213
+ {
214
+ name: 'Galaxy Tab S4',
215
+ userAgent:
216
+ 'Mozilla/5.0 (Linux; Android 8.1.0; SM-T837A) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.80 Safari/537.36',
217
+ viewport: {
218
+ width: 712,
219
+ height: 1138,
220
+ deviceScaleFactor: 2.25,
221
+ isMobile: true,
222
+ hasTouch: true,
223
+ isLandscape: false,
224
+ },
225
+ },
226
+ {
227
+ name: 'Galaxy Tab S4 landscape',
228
+ userAgent:
229
+ 'Mozilla/5.0 (Linux; Android 8.1.0; SM-T837A) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.80 Safari/537.36',
230
+ viewport: {
231
+ width: 1138,
232
+ height: 712,
233
+ deviceScaleFactor: 2.25,
234
+ isMobile: true,
235
+ hasTouch: true,
236
+ isLandscape: true,
237
+ },
238
+ },
159
239
  {
160
240
  name: 'iPad',
161
241
  userAgent:
@@ -1001,6 +1081,58 @@ Puppeteer::DEVICES = Hash[
1001
1081
  isLandscape: true,
1002
1082
  },
1003
1083
  },
1084
+ {
1085
+ name: 'Pixel 3',
1086
+ userAgent:
1087
+ 'Mozilla/5.0 (Linux; Android 9; Pixel 3 Build/PQ1A.181105.017.A1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36',
1088
+ viewport: {
1089
+ width: 393,
1090
+ height: 786,
1091
+ deviceScaleFactor: 2.75,
1092
+ isMobile: true,
1093
+ hasTouch: true,
1094
+ isLandscape: false,
1095
+ },
1096
+ },
1097
+ {
1098
+ name: 'Pixel 3 landscape',
1099
+ userAgent:
1100
+ 'Mozilla/5.0 (Linux; Android 9; Pixel 3 Build/PQ1A.181105.017.A1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.158 Mobile Safari/537.36',
1101
+ viewport: {
1102
+ width: 786,
1103
+ height: 393,
1104
+ deviceScaleFactor: 2.75,
1105
+ isMobile: true,
1106
+ hasTouch: true,
1107
+ isLandscape: true,
1108
+ },
1109
+ },
1110
+ {
1111
+ name: 'Pixel 4',
1112
+ userAgent:
1113
+ 'Mozilla/5.0 (Linux; Android 10; Pixel 4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36',
1114
+ viewport: {
1115
+ width: 353,
1116
+ height: 745,
1117
+ deviceScaleFactor: 3,
1118
+ isMobile: true,
1119
+ hasTouch: true,
1120
+ isLandscape: false,
1121
+ },
1122
+ },
1123
+ {
1124
+ name: 'Pixel 4 landscape',
1125
+ userAgent:
1126
+ 'Mozilla/5.0 (Linux; Android 10; Pixel 4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36',
1127
+ viewport: {
1128
+ width: 745,
1129
+ height: 353,
1130
+ deviceScaleFactor: 3,
1131
+ isMobile: true,
1132
+ hasTouch: true,
1133
+ isLandscape: true,
1134
+ },
1135
+ },
1004
1136
  ].map do |json|
1005
1137
  [
1006
1138
  json[:name],
@@ -233,12 +233,13 @@ class Puppeteer::DOMWorld
233
233
  # @param url [String?]
234
234
  # @param path [String?]
235
235
  # @param content [String?]
236
+ # @param id [String?]
236
237
  # @param type [String?]
237
- def add_script_tag(url: nil, path: nil, content: nil, type: nil)
238
+ def add_script_tag(url: nil, path: nil, content: nil, id: nil, type: nil)
238
239
  if url
239
240
  begin
240
241
  return execution_context.
241
- evaluate_handle(ADD_SCRIPT_URL, url, type || '').
242
+ evaluate_handle(ADD_SCRIPT_URL, url, id, type || '').
242
243
  as_element
243
244
  rescue Puppeteer::ExecutionContext::EvaluationError # for Chrome
244
245
  raise "Loading script from #{url} failed"
@@ -251,13 +252,13 @@ class Puppeteer::DOMWorld
251
252
  contents = File.read(path)
252
253
  contents += "//# sourceURL=#{path.gsub(/\n/, '')}"
253
254
  return execution_context.
254
- evaluate_handle(ADD_SCRIPT_CONTENT, contents, type || '').
255
+ evaluate_handle(ADD_SCRIPT_CONTENT, contents, id, type || 'text/javascript').
255
256
  as_element
256
257
  end
257
258
 
258
259
  if content
259
260
  return execution_context.
260
- evaluate_handle(ADD_SCRIPT_CONTENT, content, type || '').
261
+ evaluate_handle(ADD_SCRIPT_CONTENT, content, id, type || 'text/javascript').
261
262
  as_element
262
263
  end
263
264
 
@@ -265,11 +266,11 @@ class Puppeteer::DOMWorld
265
266
  end
266
267
 
267
268
  ADD_SCRIPT_URL = <<~JAVASCRIPT
268
- async (url, type) => {
269
+ async (url, id, type) => {
269
270
  const script = document.createElement('script');
270
271
  script.src = url;
271
- if (type)
272
- script.type = type;
272
+ if (id) script.id = id;
273
+ if (type) script.type = type;
273
274
  const promise = new Promise((res, rej) => {
274
275
  script.onload = res;
275
276
  script.onerror = rej;
@@ -281,11 +282,11 @@ class Puppeteer::DOMWorld
281
282
  JAVASCRIPT
282
283
 
283
284
  ADD_SCRIPT_CONTENT = <<~JAVASCRIPT
284
- (content, type) => {
285
- if (type === undefined) type = 'text/javascript';
285
+ (content, id, type) => {
286
286
  const script = document.createElement('script');
287
287
  script.type = type;
288
288
  script.text = content;
289
+ if (id) script.id = id;
289
290
  let error = null;
290
291
  script.onerror = e => error = e;
291
292
  document.head.appendChild(script);
@@ -0,0 +1,28 @@
1
+ class Puppeteer::ElementHandle < Puppeteer::JSHandle
2
+ # A class to represent (x, y)-offset coordinates
3
+ class Offset
4
+ def initialize(x:, y:)
5
+ @x = x
6
+ @y = y
7
+ end
8
+
9
+ def self.from(offset)
10
+ case offset
11
+ when nil
12
+ nil
13
+ when Hash
14
+ if offset[:x] && offset[:y]
15
+ Offset.new(x: offset[:x], y: offset[:y])
16
+ else
17
+ raise ArgumentError.new('offset parameter must have x, y coordinates')
18
+ end
19
+ when Offset
20
+ offset
21
+ else
22
+ raise ArgumentError.new('Offset.from(Hash|Offset)')
23
+ end
24
+ end
25
+
26
+ attr_reader :x, :y
27
+ end
28
+ end
@@ -21,6 +21,17 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
21
21
  )
22
22
  end
23
23
 
24
+ def ==(other)
25
+ case other
26
+ when Hash
27
+ @x == other[:x] && @y == other[:y]
28
+ when Point
29
+ @x == other.x && @y == other.y
30
+ else
31
+ super
32
+ end
33
+ end
34
+
24
35
  attr_reader :x, :y
25
36
  end
26
37
  end
@@ -1,5 +1,6 @@
1
1
  require_relative './element_handle/bounding_box'
2
2
  require_relative './element_handle/box_model'
3
+ require_relative './element_handle/offset'
3
4
  require_relative './element_handle/point'
4
5
 
5
6
  class Puppeteer::ElementHandle < Puppeteer::JSHandle
@@ -79,7 +80,9 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
79
80
  end
80
81
  end
81
82
 
82
- def clickable_point
83
+ def clickable_point(offset = nil)
84
+ offset_param = Offset.from(offset)
85
+
83
86
  result =
84
87
  begin
85
88
  @remote_object.content_quads(@client)
@@ -105,6 +108,19 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
105
108
  raise ElementNotVisibleError.new
106
109
  end
107
110
 
111
+ if offset_param
112
+ # Return the point of the first quad identified by offset.
113
+ quad = quads.first
114
+ min_x = quad.map(&:x).min
115
+ min_y = quad.map(&:y).min
116
+ if min_x && min_y
117
+ return Point.new(
118
+ x: min_x + offset_param.x,
119
+ y: min_y + offset_param.y,
120
+ )
121
+ end
122
+ end
123
+
108
124
  # Return the middle point of the first quad.
109
125
  quads.first.reduce(:+) / 4
110
126
  end
@@ -139,14 +155,56 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
139
155
  # @param delay [Number]
140
156
  # @param button [String] "left"|"right"|"middle"
141
157
  # @param click_count [Number]
142
- def click(delay: nil, button: nil, click_count: nil)
158
+ # @param offset [Hash]
159
+ def click(delay: nil, button: nil, click_count: nil, offset: nil)
143
160
  scroll_into_view_if_needed
144
- point = clickable_point
161
+ point = clickable_point(offset)
145
162
  @page.mouse.click(point.x, point.y, delay: delay, button: button, click_count: click_count)
146
163
  end
147
164
 
148
165
  define_async_method :async_click
149
166
 
167
+ class DragInterceptionNotEnabledError < StandardError
168
+ def initialize
169
+ super('Drag Interception is not enabled!')
170
+ end
171
+ end
172
+
173
+ def drag(x:, y:)
174
+ unless @page.drag_interception_enabled?
175
+ raise DragInterceptionNotEnabledError.new
176
+ end
177
+ scroll_into_view_if_needed
178
+ start = clickable_point
179
+ @page.mouse.drag(start, Point.new(x: x, y: y))
180
+ end
181
+
182
+ def drag_enter(data)
183
+ scroll_into_view_if_needed
184
+ target = clickable_point
185
+ @page.mouse.drag_enter(target, data)
186
+ end
187
+
188
+ def drag_over(data)
189
+ scroll_into_view_if_needed
190
+ target = clickable_point
191
+ @page.mouse.drag_over(target, data)
192
+ end
193
+
194
+ def drop(data)
195
+ scroll_into_view_if_needed
196
+ target = clickable_point
197
+ @page.mouse.drop(target, data)
198
+ end
199
+
200
+ # @param target [ElementHandle]
201
+ def drag_and_drop(target, delay: nil)
202
+ scroll_into_view_if_needed
203
+ start_point = clickable_point
204
+ target_point = target.clickable_point
205
+ @page.mouse.drag_and_drop(start_point, target_point, delay: delay)
206
+ end
207
+
150
208
  # @return [Array<String>]
151
209
  def select(*values)
152
210
  if nonstring = values.find { |value| !value.is_a?(String) }
@@ -395,10 +453,12 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
395
453
  define_async_method :async_Sx
396
454
 
397
455
  # in JS, #isIntersectingViewport.
456
+ # @param threshold [Float|nil]
398
457
  # @return [Boolean]
399
- def intersecting_viewport?
458
+ def intersecting_viewport?(threshold: nil)
459
+ option_threshold = threshold || 0
400
460
  js = <<~JAVASCRIPT
401
- async element => {
461
+ async (element, threshold) => {
402
462
  const visibleRatio = await new Promise(resolve => {
403
463
  const observer = new IntersectionObserver(entries => {
404
464
  resolve(entries[0].intersectionRatio);
@@ -406,11 +466,12 @@ class Puppeteer::ElementHandle < Puppeteer::JSHandle
406
466
  });
407
467
  observer.observe(element);
408
468
  });
409
- return visibleRatio > 0;
469
+ if (threshold === 1) return visibleRatio === 1;
470
+ else return visibleRatio > threshold;
410
471
  }
411
472
  JAVASCRIPT
412
473
 
413
- evaluate(js)
474
+ evaluate(js, option_threshold)
414
475
  end
415
476
 
416
477
  # @param quad [Array<Point>]
@@ -157,8 +157,9 @@ class Puppeteer::Frame
157
157
  # @param path [String?]
158
158
  # @param content [String?]
159
159
  # @param type [String?]
160
- def add_script_tag(url: nil, path: nil, content: nil, type: nil)
161
- @main_world.add_script_tag(url: url, path: path, content: content, type: type)
160
+ # @param id [String?]
161
+ def add_script_tag(url: nil, path: nil, content: nil, type: nil, id: nil)
162
+ @main_world.add_script_tag(url: url, path: path, content: content, type: type, id: id)
162
163
  end
163
164
 
164
165
  # @param url [String?]
@@ -12,6 +12,14 @@ class Puppeteer::JSCoverage
12
12
  attr_reader :url, :ranges, :text
13
13
  end
14
14
 
15
+ class ItemWithRawScriptCoverage < Item
16
+ def initialize(url:, ranges:, text:, raw_script_coverage:)
17
+ super(url: url, ranges: ranges, text: text)
18
+ @raw_script_coverage = raw_script_coverage
19
+ end
20
+ attr_reader :raw_script_coverage
21
+ end
22
+
15
23
  # @param client [Puppeteer::CDPSession]
16
24
  def initialize(client)
17
25
  @client = client
@@ -20,7 +28,10 @@ class Puppeteer::JSCoverage
20
28
  @script_sources = {}
21
29
  end
22
30
 
23
- def start(reset_on_navigation: nil, report_anonymous_scripts: nil)
31
+ def start(
32
+ reset_on_navigation: nil,
33
+ report_anonymous_scripts: nil,
34
+ include_raw_script_coverage: nil)
24
35
  raise 'JSCoverage is already enabled' if @enabled
25
36
 
26
37
  @reset_on_navigation =
@@ -30,6 +41,7 @@ class Puppeteer::JSCoverage
30
41
  true
31
42
  end
32
43
  @report_anonymous_scripts = report_anonymous_scripts || false
44
+ @include_raw_script_coverage = include_raw_script_coverage || false
33
45
  @enabled = true
34
46
  @script_urls.clear
35
47
  @script_sources.clear
@@ -43,7 +55,7 @@ class Puppeteer::JSCoverage
43
55
  await_all(
44
56
  @client.async_send_message('Profiler.enable'),
45
57
  @client.async_send_message('Profiler.startPreciseCoverage',
46
- callCount: false,
58
+ callCount: @include_raw_script_coverage,
47
59
  detailed: true,
48
60
  ),
49
61
  @client.async_send_message('Debugger.enable'),
@@ -107,11 +119,20 @@ class Puppeteer::JSCoverage
107
119
  end
108
120
  end
109
121
 
110
- coverage << Item.new(
111
- url: url,
112
- ranges: convert_to_disjoint_ranges(flatten_ranges),
113
- text: text,
114
- )
122
+ if @include_raw_script_coverage
123
+ coverage << ItemWithRawScriptCoverage.new(
124
+ url: url,
125
+ ranges: convert_to_disjoint_ranges(flatten_ranges),
126
+ text: text,
127
+ raw_script_coverage: entry,
128
+ )
129
+ else
130
+ coverage << Item.new(
131
+ url: url,
132
+ ranges: convert_to_disjoint_ranges(flatten_ranges),
133
+ text: text,
134
+ )
135
+ end
115
136
  end
116
137
 
117
138
  coverage
@@ -35,9 +35,9 @@ module Puppeteer::Launcher
35
35
  @channel = options[:channel]
36
36
  @executable_path = options[:executable_path]
37
37
  @ignore_default_args = options[:ignore_default_args] || false
38
- @handle_SIGINT = options[:handle_SIGINT] || true
39
- @handle_SIGTERM = options[:handle_SIGTERM] || true
40
- @handle_SIGHUP = options[:handle_SIGHUP] || true
38
+ @handle_SIGINT = options.fetch(:handle_SIGINT, true)
39
+ @handle_SIGTERM = options.fetch(:handle_SIGTERM, true)
40
+ @handle_SIGHUP = options.fetch(:handle_SIGHUP, true)
41
41
  @timeout = options[:timeout] || 30000
42
42
  @dumpio = options[:dumpio] || false
43
43
  @env = options[:env] || ENV
@@ -94,6 +94,8 @@ class Puppeteer::Mouse
94
94
  )
95
95
  end
96
96
 
97
+ define_async_method :async_up
98
+
97
99
  # Dispatches a `mousewheel` event.
98
100
  #
99
101
  # @param delta_x [Integer]
@@ -110,5 +112,56 @@ class Puppeteer::Mouse
110
112
  )
111
113
  end
112
114
 
113
- define_async_method :async_up
115
+ def drag(start, target)
116
+ promise = resolvable_future do |f|
117
+ @client.once('Input.dragIntercepted') do |event|
118
+ f.fulfill(event['data'])
119
+ end
120
+ end
121
+ move(start.x, start.y)
122
+ down
123
+ move(target.x, target.y)
124
+ promise.value!
125
+ end
126
+
127
+ def drag_enter(target, data)
128
+ @client.send_message('Input.dispatchDragEvent',
129
+ type: 'dragEnter',
130
+ x: target.x,
131
+ y: target.y,
132
+ modifiers: @keyboard.modifiers,
133
+ data: data,
134
+ )
135
+ end
136
+
137
+ def drag_over(target, data)
138
+ @client.send_message('Input.dispatchDragEvent',
139
+ type: 'dragOver',
140
+ x: target.x,
141
+ y: target.y,
142
+ modifiers: @keyboard.modifiers,
143
+ data: data,
144
+ )
145
+ end
146
+
147
+ def drop(target, data)
148
+ @client.send_message('Input.dispatchDragEvent',
149
+ type: 'drop',
150
+ x: target.x,
151
+ y: target.y,
152
+ modifiers: @keyboard.modifiers,
153
+ data: data,
154
+ )
155
+ end
156
+
157
+ def drag_and_drop(start, target, delay: nil)
158
+ data = drag(start, target)
159
+ drag_enter(target, data)
160
+ drag_over(target, data)
161
+ if delay
162
+ sleep(delay / 1000.0)
163
+ end
164
+ drop(target, data)
165
+ up
166
+ end
114
167
  end
@@ -0,0 +1,12 @@
1
+ class Puppeteer::NetworkCondition
2
+ # @param download [Number] Download speed (bytes/s)
3
+ # @param upload [Number] Upload speed (bytes/s)
4
+ # @param latency [Number] Latency (ms)
5
+ def initialize(download:, upload:, latency:)
6
+ @download = download
7
+ @upload = upload
8
+ @latency = latency
9
+ end
10
+
11
+ attr_reader :download, :upload, :latency
12
+ end
@@ -0,0 +1,24 @@
1
+ require_relative './network_condition'
2
+
3
+ Puppeteer::NETWORK_CONDITIONS = {
4
+ 'Slow 3G' => Puppeteer::NetworkCondition.new(
5
+ download: ((500 * 1000) / 8) * 0.8,
6
+ upload: ((500 * 1000) / 8) * 0.8,
7
+ latency: 400 * 5,
8
+ ),
9
+ 'Fast 3G' => Puppeteer::NetworkCondition.new(
10
+ download: ((1.6 * 1000 * 1000) / 8) * 0.9,
11
+ upload: ((750 * 1000) / 8) * 0.9,
12
+ latency: 150 * 3.75,
13
+ ),
14
+ }
15
+
16
+ module Puppeteer::NetworkConditions
17
+ module_function def slow_3g
18
+ Puppeteer::NETWORK_CONDITIONS['Slow 3G']
19
+ end
20
+
21
+ module_function def fast_3g
22
+ Puppeteer::NETWORK_CONDITIONS['Fast 3G']
23
+ end
24
+ end
@@ -13,6 +13,46 @@ class Puppeteer::NetworkManager
13
13
  attr_reader :username, :password
14
14
  end
15
15
 
16
+ class InternalNetworkCondition
17
+ attr_writer :offline, :upload, :download, :latency
18
+
19
+ def initialize(client)
20
+ @client = client
21
+ @offline = false
22
+ @upload = -1
23
+ @download = -1
24
+ @latency = 0
25
+ end
26
+
27
+ def offline_mode=(value)
28
+ return if @offline == value
29
+ @offline = value
30
+ update_network_conditions
31
+ end
32
+
33
+ def network_condition=(network_condition)
34
+ if network_condition
35
+ @upload = network_condition.upload
36
+ @download = network_condition.download
37
+ @latency = network_condition.latency
38
+ else
39
+ @upload = -1
40
+ @download = -1
41
+ @latency = 0
42
+ end
43
+ update_network_conditions
44
+ end
45
+
46
+ private def update_network_conditions
47
+ @client.send_message('Network.emulateNetworkConditions',
48
+ offline: @offline,
49
+ latency: @latency,
50
+ downloadThroughput: @download,
51
+ uploadThroughput: @upload,
52
+ )
53
+ end
54
+ end
55
+
16
56
  # @param {!Puppeteer.CDPSession} client
17
57
  # @param {boolean} ignoreHTTPSErrors
18
58
  # @param {!Puppeteer.FrameManager} frameManager
@@ -29,13 +69,12 @@ class Puppeteer::NetworkManager
29
69
 
30
70
  @extra_http_headers = {}
31
71
 
32
- @offline = false
33
-
34
72
  @attempted_authentications = Set.new
35
73
  @user_request_interception_enabled = false
36
74
  @protocol_request_interception_enabled = false
37
75
  @user_cache_disabled = false
38
76
  @request_id_to_interception_id = {}
77
+ @internal_network_condition = InternalNetworkCondition.new(@client)
39
78
 
40
79
  @client.on_event('Fetch.requestPaused') do |event|
41
80
  handle_request_paused(event)
@@ -94,21 +133,23 @@ class Puppeteer::NetworkManager
94
133
 
95
134
  # @param value [TrueClass|FalseClass]
96
135
  def offline_mode=(value)
97
- return if @offline == value
98
- @offline = value
99
- @client.send_message('Network.emulateNetworkConditions',
100
- offline: @offline,
101
- # values of 0 remove any active throttling. crbug.com/456324#c9
102
- latency: 0,
103
- downloadThroughput: -1,
104
- uploadThroughput: -1,
105
- )
136
+ @internal_network_condition.offline_mode=(value)
137
+ end
138
+
139
+ # @param network_condition [Puppeteer::NetworkCondition|nil]
140
+ def emulate_network_conditions(network_condition)
141
+ @internal_network_condition.network_condition = network_condition
106
142
  end
107
143
 
108
144
  # @param user_agent [String]
109
- def user_agent=(user_agent)
110
- @client.send_message('Network.setUserAgentOverride', userAgent: user_agent)
145
+ # @param user_agent_metadata [Hash]
146
+ def set_user_agent(user_agent, user_agent_metadata = nil)
147
+ @client.send_message('Network.setUserAgentOverride', {
148
+ userAgent: user_agent,
149
+ userAgentMetadata: user_agent_metadata,
150
+ }.compact)
111
151
  end
152
+ alias_method :user_agent=, :set_user_agent
112
153
 
113
154
  def cache_enabled=(enabled)
114
155
  @user_cache_disabled = !enabled