ferrum 0.1.2 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 858608216b6e3a287e7d47621bf9c44415ea2e5411cdc53e34c2e18c3fbd8153
4
- data.tar.gz: 74d290a312092c690b77ef6afac4ae65839417559749188da22e34b655789dc5
3
+ metadata.gz: 977220d1be605d449fd593a263f464707ce1e0bcc9dd3c6cc0a187f64b7e2385
4
+ data.tar.gz: 47a490f0c7b99c597fb069eec517c2e42c4466fc1791f0a63d4807e9ec9d7016
5
5
  SHA512:
6
- metadata.gz: fe68b7a02885080677a38cc347f7bf79ed094ed09dcd7f111aee613b345e3a6bfdaf1b744b266e3b1de11770bcc37f4cb5408a499b1ad4640e370e3cd0887f49
7
- data.tar.gz: 721e01402142f201cf21aa3e91d9ac21e698a277dc276b32a72c93e044abe9c81c7707e4606ec82780b89214e4108bbd0730d4d47a9509510186aa4b856e8ebf
6
+ metadata.gz: c0f09cb6f46996f2532add8556ac8c3a7adf4787955274cff1592c36215224f81a2d9835fd66316e61f2cdd0f5919ca3cbbb293ef1d381c1898c5dd4d061b672
7
+ data.tar.gz: 8a4e05fe95de8400e5c93c7e192d7903d819ab30831db10b180c705ea87430f6a064404e438f9d79b767d23074968330d0178dbd6375445064e35071c773e96a
data/README.md CHANGED
@@ -1,15 +1,17 @@
1
- # Ferrum - fearless Ruby Chrome/Chromium driver
1
+ # Ferrum - fearless Ruby Chrome driver
2
2
 
3
- As simple as Puppeteer, though even simpler. It is Ruby clean and high-level API
4
- to Chrome/Chromium through the DevTools Protocol. Runs headless by default,
5
- but you can configure it to run in a non-headless mode.
3
+ As simple as Puppeteer, though even simpler.
6
4
 
7
- Navigate to `example.com` and save a screenshot:
5
+ It is Ruby clean and high-level API to Chrome. Runs headless by default,
6
+ but you can configure it to run in a non-headless mode. All you need is Ruby and
7
+ Chrome/Chromium. Ferrum connects to the browser via DevTools Protocol.
8
+
9
+ Navigate to a website and save a screenshot:
8
10
 
9
11
  ```ruby
10
12
  browser = Ferrum::Browser.new
11
- browser.goto("https://example.com")
12
- browser.screenshot(path: "example.png")
13
+ browser.goto("https://google.com")
14
+ browser.screenshot(path: "google.png")
13
15
  browser.quit
14
16
  ```
15
17
 
@@ -18,10 +20,511 @@ Interact with a page:
18
20
  ```ruby
19
21
  browser = Ferrum::Browser.new
20
22
  browser.goto("https://google.com")
21
- input = browser.at_css("input[title='Search']")
22
- input.send_keys("Ruby headless driver for Capybara", :Enter)
23
+ input = browser.at_xpath("//div[@id='searchform']/form//input[@type='text']")
24
+ input.focus.type("Ruby headless driver for Capybara", :Enter)
23
25
  browser.at_css("a > h3").text # => "machinio/cuprite: Headless Chrome driver for Capybara - GitHub"
24
26
  browser.quit
25
27
  ```
26
28
 
27
- The README will be updated soon. Meanwhile take a look at specs.
29
+ Evaluate some JavaScript and get full width/height:
30
+
31
+ ```ruby
32
+ browser = Ferrum::Browser.new
33
+ browser.goto("https://www.google.com/search?q=Ruby+headless+driver+for+Capybara")
34
+ width, height = browser.evaluate <<~JS
35
+ [document.documentElement.offsetWidth,
36
+ document.documentElement.offsetHeight]
37
+ JS
38
+ # => [1024, 1931]
39
+ browser.quit
40
+ ```
41
+
42
+ Do any mouse movements you like:
43
+
44
+ ```ruby
45
+ # Trace a 100x100 square
46
+ browser = Ferrum::Browser.new
47
+ browser.goto("https://google.com")
48
+ browser.mouse
49
+ .move(x: 0, y: 0)
50
+ .down
51
+ .move(x: 0, y: 100)
52
+ .move(x: 100, y: 100)
53
+ .move(x: 100, y: 0)
54
+ .move(x: 0, y: 0)
55
+ .up
56
+
57
+ browser.quit
58
+ ```
59
+
60
+ #### The API below is correct but a subject to change before `1.0`
61
+
62
+ ## Navigation
63
+
64
+ #### goto(url) : `String`
65
+
66
+ Navigate page to.
67
+
68
+ * url `String` The url should include scheme unless you set `base_url` when
69
+ configuring driver.
70
+
71
+ ```ruby
72
+ browser.goto("https://github.com/")
73
+ ```
74
+
75
+ #### back
76
+
77
+ Navigate to the previous page in history.
78
+
79
+ ```ruby
80
+ browser.goto("https://github.com/")
81
+ browser.at_xpath("//a").click
82
+ browser.back
83
+ ```
84
+
85
+ #### forward
86
+
87
+ Navigate to the next page in history.
88
+
89
+ ```ruby
90
+ browser.goto("https://github.com/")
91
+ browser.at_xpath("//a").click
92
+ browser.back
93
+ browser.forward
94
+ ```
95
+
96
+ #### refresh
97
+
98
+ Reload current page.
99
+
100
+ ```ruby
101
+ browser.goto("https://github.com/")
102
+ browser.refresh
103
+ ```
104
+
105
+ #### status : `Integer`
106
+
107
+ Contains the status code of the response (e.g., 200 for a success).
108
+
109
+ ```ruby
110
+ browser.goto("https://github.com/")
111
+ browser.status # => 200
112
+ ```
113
+
114
+
115
+ ## Finders
116
+
117
+ #### at_css(selector, \*\*options) : `Node` | `nil`
118
+
119
+ Find node by selector. Runs `document.querySelector` within the document or
120
+ provided node.
121
+
122
+ * selector `String`
123
+ * options `Hash`
124
+ * :within `Node` | `nil`
125
+
126
+ ```ruby
127
+ browser.goto("https://github.com/")
128
+ browser.at_css("a[aria-label='Issues you created']") # => Node
129
+ ```
130
+
131
+
132
+ #### css(selector, \*\*options) : `Array<Node>` | `[]`
133
+
134
+ Find nodes by selector. The method runs `document.querySelectorAll` within the
135
+ document or provided node.
136
+
137
+ * selector `String`
138
+ * options `Hash`
139
+ * :within `Node` | `nil`
140
+
141
+ ```ruby
142
+ browser.goto("https://github.com/")
143
+ browser.css("a[aria-label='Issues you created']") # => [Node]
144
+ ```
145
+
146
+ #### at_xpath(selector, \*\*options) : `Node` | `nil`
147
+
148
+ Find node by xpath.
149
+
150
+ * selector `String`
151
+ * options `Hash`
152
+ * :within `Node` | `nil`
153
+
154
+ ```ruby
155
+ browser.goto("https://github.com/")
156
+ browser.at_xpath("//a[@aria-label='Issues you created']") # => Node
157
+ ```
158
+
159
+ #### xpath(selector, \*\*options) : `Array<Node>` | `[]`
160
+
161
+ Find nodes by xpath.
162
+
163
+ * selector `String`
164
+ * options `Hash`
165
+ * :within `Node` | `nil`
166
+
167
+ ```ruby
168
+ browser.goto("https://github.com/")
169
+ browser.xpath("//a[@aria-label='Issues you created']") # => [Node]
170
+ ```
171
+
172
+ #### current_url : `String`
173
+
174
+ Returns current window location href.
175
+
176
+ ```ruby
177
+ browser.goto("https://google.com/")
178
+ browser.current_url # => "https://www.google.com/"
179
+ ```
180
+
181
+ #### title : `String`
182
+
183
+ Returns current window title
184
+
185
+ ```ruby
186
+ browser.goto("https://google.com/")
187
+ browser.title # => "Google"
188
+ ```
189
+
190
+ #### body : `String`
191
+
192
+ Returns current page's html.
193
+
194
+ ```ruby
195
+ browser.goto("https://google.com/")
196
+ browser.body # => '<html itemscope="" itemtype="http://schema.org/WebPage" lang="ru"><head>...
197
+ ```
198
+
199
+ ## Screenshots
200
+
201
+ #### screenshot(\*\*options) : `String` | `Integer`
202
+
203
+ Saves screenshot on a disk or returns it as base64.
204
+
205
+ * options `Hash`
206
+ * :path `String` to save a screenshot on the disk. If passed `:encoding` is
207
+ set to `:binary` automatically
208
+ * :encoding `Symbol` `:base64` | `:binary` you can set it to return image as
209
+ Base64
210
+ * :format `String` "jpeg" | "png"
211
+ * :quality `Integer` 0-100 works for jpeg only
212
+ * :full `Boolean` whether you need full page screenshot or a viewport
213
+ * :selector `String` css selector for given element
214
+ * :scale `Float` zoom in/out
215
+
216
+
217
+ ```ruby
218
+ browser.goto("https://google.com/")
219
+ # Save on the disk in PNG
220
+ browser.screenshot(path: "google.png") # => 134660
221
+ # Save on the disk in JPG
222
+ browser.screenshot(path: "google.jpg") # => 30902
223
+ # Save to Base64 the whole page not only viewport and reduce quality
224
+ browser.screenshot(full: true, quality: 60) # "iVBORw0KGgoAAAANSUhEUgAABAAAAAMACAYAAAC6uhUNAAAAAXNSR0IArs4c6Q...
225
+ ```
226
+
227
+ #### pdf(\*\*options) : `String` | `Integer`
228
+
229
+ Saves PDF on a disk or returns it as base64.
230
+
231
+ * options `Hash`
232
+ * :path `String` to save a screenshot on the disk. If passed `:encoding` is
233
+ set to `:binary` automatically
234
+ * :encoding `Symbol` `:base64` | `:binary` you can set it to return pdf as
235
+ Base64
236
+ * :landscape `Boolean` paper orientation. Defaults to false.
237
+ * :scale `Float` zoom in/out
238
+ * :paper_width `Float` set paper width
239
+ * :paper_height `Float` set paper height
240
+ * See other [native options](https://chromedevtools.github.io/devtools-protocol/tot/Page#method-printToPDF) you can pass
241
+
242
+
243
+ ```ruby
244
+ browser.goto("https://google.com/")
245
+ # Save on the disk in PNG
246
+ browser.pdf(path: "google.pdf", paper_width: 1.0, paper_height: 1.0) # => 14983
247
+ ```
248
+
249
+
250
+ ## Network
251
+
252
+ #### network_traffic : `Array<Network::Request>`
253
+
254
+ Returns all information about network traffic as a request/response array.
255
+
256
+ #### clear_network_traffic
257
+
258
+ Cleans up collected data.
259
+
260
+ #### response_headers : `Hash`
261
+
262
+ Returns all headers for a given request in `goto` method.
263
+
264
+
265
+ ## Input
266
+
267
+ #### scroll_to(x, y)
268
+
269
+ Scroll page to a given x, y
270
+
271
+ * x `Integer` the pixel along the horizontal axis of the document that you
272
+ want displayed in the upper left
273
+ * y `Integer` the pixel along the vertical axis of the document that you want
274
+ displayed in the upper left
275
+
276
+ ### Mouse
277
+
278
+ browser.mouse
279
+
280
+ #### click(\*\*options) : `Mouse`
281
+
282
+ Click given coordinates, fires mouse move, down and up events.
283
+
284
+ * options `Hash`
285
+ * :x `Integer`
286
+ * :y `Integer`
287
+ * :delay `Float` defaults to 0. Delay between mouse down and mouse up events
288
+ * :button `Symbol` :left | :right, defaults to :left
289
+ * :count `Integer` defaults to 1
290
+ * :modifiers `Integer` bitfield for key modifiers. See`keyboard.modifiers`
291
+
292
+ #### down(\*\*options) : `Mouse`
293
+
294
+ Mouse down for given coordinates.
295
+
296
+ * options `Hash`
297
+ * :button `Symbol` :left | :right, defaults to :left
298
+ * :count `Integer` defaults to 1
299
+ * :modifiers `Integer` bitfield for key modifiers. See`keyboard.modifiers`
300
+
301
+ #### up(\*\*options) : `Mouse`
302
+
303
+ Mouse up for given coordinates.
304
+
305
+ * options `Hash`
306
+ * :button `Symbol` :left | :right, defaults to :left
307
+ * :count `Integer` defaults to 1
308
+ * :modifiers `Integer` bitfield for key modifiers. See`keyboard.modifiers`
309
+
310
+ #### move(x:, y:, steps: 1) : `Mouse`
311
+
312
+ Mouse move to given x and y.
313
+
314
+ * options `Hash`
315
+ * :x `Integer`
316
+ * :y `Integer`
317
+ * :steps `Integer` defaults to 1. Sends intermediate mousemove events.
318
+
319
+ ### Keyboard
320
+
321
+ browser.keyboard
322
+
323
+ #### down(key) : `Keyboard`
324
+
325
+ Dispatches a keydown event.
326
+
327
+ * key `String` | `Symbol` Name of key such as "a", :enter, :backspace
328
+
329
+ #### up(key) : `Keyboard`
330
+
331
+ Dispatches a keyup event.
332
+
333
+ * key `String` | `Symbol` Name of key such as "b", :enter, :backspace
334
+
335
+ #### type(\*keys) : `Keyboard`
336
+
337
+ Sends a keydown, keypress/input, and keyup event for each character in the text.
338
+
339
+ * text `String` | `Array<String> | Array<Symbol>` A text to type into a focused
340
+ element, `[:Shift, "s"], "tring"`
341
+
342
+ #### modifiers(keys) : `Integer`
343
+
344
+ Returns bitfield for a given keys
345
+
346
+ * keys `Array<Symbol>` :alt | :ctrl | :command | :shift
347
+
348
+
349
+ ## Cookies
350
+
351
+ browser.cookies
352
+
353
+ #### all : `Hash<String, Cookie>`
354
+
355
+ Returns cookies hash
356
+
357
+
358
+ ```ruby
359
+ browser.cookies.all # => {"NID"=>#<Ferrum::Cookies::Cookie:0x0000558624b37a40 @attributes={"name"=>"NID", "value"=>"...", "domain"=>".google.com", "path"=>"/", "expires"=>1583211046.575681, "size"=>178, "httpOnly"=>true, "secure"=>false, "session"=>false}>}
360
+ ```
361
+
362
+ #### [](value) : `Cookie`
363
+
364
+ Returns cookie
365
+
366
+ * value `String`
367
+
368
+ ```ruby
369
+ browser.cookies["NID"] # => <Ferrum::Cookies::Cookie:0x0000558624b67a88 @attributes={"name"=>"NID", "value"=>"...", "domain"=>".google.com", "path"=>"/", "expires"=>1583211046.575681, "size"=>178, "httpOnly"=>true, "secure"=>false, "session"=>false}>
370
+ ```
371
+
372
+ #### set(\*\*options) : `Boolean`
373
+
374
+ Sets given values as cookie
375
+
376
+ * options `Hash`
377
+ * :name `String`
378
+ * :value `String`
379
+ * :domain `String`
380
+ * :expires `Integer`
381
+
382
+ ```ruby
383
+ browser.cookies.set(name: "stealth", value: "omg", domain: "google.com") # => true
384
+ ```
385
+
386
+ #### remove(\*\*options) : `Boolean`
387
+
388
+ Removes given cookie
389
+
390
+ * options `Hash`
391
+ * :name `String`
392
+ * :domain `String`
393
+ * :url `String`
394
+
395
+ ```ruby
396
+ browser.cookies.remove(name: "stealth", domain: "google.com") # => true
397
+ ```
398
+
399
+ #### clear : `Boolean`
400
+
401
+ Removes all cookies for current page
402
+
403
+ ```ruby
404
+ browser.cookies.clear # => true
405
+ ```
406
+
407
+ ## Headers
408
+
409
+ browser.headers
410
+
411
+ #### get : `Hash`
412
+
413
+ Get all headers
414
+
415
+ #### set(headers) : `Boolean`
416
+
417
+ Set given headers. Eventually clear all headers and set given ones.
418
+
419
+ * headers `Hash` key-value pairs for example `"User-Agent" => "Browser"`
420
+
421
+ #### add(headers) : `Boolean`
422
+
423
+ Adds given headers to already set ones.
424
+
425
+ * headers `Hash` key-value pairs for example `"Referer" => "http://example.com"`
426
+
427
+ #### clear : `Boolean`
428
+
429
+ Clear all headers.
430
+
431
+
432
+ ## JavaScript
433
+
434
+ #### evaluate(expression, \*args)
435
+
436
+ Evaluate and return result for given JS expression
437
+
438
+ * expression `String` should be valid JavaScript
439
+ * args `Object` you can pass arguments, though it should be a valid `Node` or a
440
+ simple value.
441
+
442
+ ```ruby
443
+ browser.evaluate("[window.scrollX, window.scrollY]")
444
+ ```
445
+
446
+ #### evaluate_async(expression, wait_time, \*args)
447
+
448
+ Evaluate asynchronous expression and return result
449
+
450
+ * expression `String` should be valid JavaScript
451
+ * wait_time How long we should wait for Promise to resolve or reject
452
+ * args `Object` you can pass arguments, though it should be a valid `Node` or a
453
+ simple value.
454
+
455
+ ```ruby
456
+ browser.evaluate_async(%(arguments[0]({foo: "bar"})), 5) # => { "foo" => "bar" }
457
+ ```
458
+
459
+ #### execute(expression, \*args)
460
+
461
+ Execute expression. Doesn't return the result
462
+
463
+ * expression `String` should be valid JavaScript
464
+ * args `Object` you can pass arguments, though it should be a valid `Node` or a
465
+ simple value.
466
+
467
+ ```ruby
468
+ browser.execute(%(1 + 1)) # => true
469
+ ```
470
+
471
+
472
+ ## Frames
473
+
474
+ #### frame_url
475
+ #### frame_title
476
+ #### within_frame(frame, &block)
477
+
478
+ Play around inside given frame
479
+
480
+ ```ruby
481
+ browser.goto("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
482
+ frame = browser.at_xpath("//iframe")
483
+ browser.within_frame(frame) do
484
+ puts browser.frame_url # => https://interactive-examples.mdn.mozilla.net/pages/tabbed/iframe.html
485
+ end
486
+ ```
487
+
488
+
489
+ ## Modals
490
+
491
+ #### find_modal
492
+ #### accept_confirm
493
+ #### dismiss_confirm
494
+ #### accept_prompt
495
+ #### dismiss_prompt
496
+ #### reset_modals
497
+
498
+
499
+ ## Auth
500
+
501
+ #### authorize(user, password)
502
+
503
+ If site uses authorization you can provide credentials using this method.
504
+
505
+ * user `String`
506
+ * passowrd `String`
507
+
508
+ #### proxy_authorize(user, password)
509
+
510
+ If you want to use proxy that requires authentication this is the method you need.
511
+
512
+ * user `String`
513
+ * passowrd `String`
514
+
515
+
516
+ ## Interception
517
+
518
+ #### intercept_request
519
+
520
+ ```ruby
521
+ browser = Ferrum::Browser.new
522
+ browser.intercept_request do |request|
523
+ if request.match?(/bla-bla/)
524
+ request.abort
525
+ else
526
+ request.continue
527
+ end
528
+ end
529
+ browser.goto("https://google.com")
530
+ ```