Skip to content

Commit 13158c0

Browse files
authored
Add yard (#296)
* Setup YARDoc. * Added YARD docs to `Ferrum::Browser::VersionInfo` and `Browser#version`. * Add YARD tags for `Ferrum::Browser#initialize`. * Added YARD docs for `Ferrum::Browser#base_url=`. * Added YARD docs for `Ferrum::Page#go_to`. * Added documentation for `Ferrum::Page#back` and `#forward`. * Added documentation for `Ferrum::Page#refresh`. * Added documentation for `Ferrum::Page#stop`. * Added YARD docs for `Ferrum::Page#position`. * Added YARD docs for `Ferrum::Page#position=`. * Added YARD docs to `Ferrum::Frame::DOM#at_css`. * Added YARD docs to `Ferrum::Frame::DOM#css`. * Added YARD docs to `Ferrum::Frame::DOM#at_xpath`. * Added YARD docs to `Ferrum::Frame::DOM#xpath`. * Added YARD docs for `Ferrum::Frame::DOM#current_url`. * Added YARD docs for `Ferrum::Frame::DOM#current_title`. * Added YARD docs for `Ferrum::Frame::DOM#body`. * Added YARD docs to `Ferrum::Page::Screenshot#screenshot`. * Added YARD docs for `Ferrum::Page::Screenshot#pdf`. * Added YARD docs to `Ferrum::Page::Screenshot#mhtml`. * Added documentation to `Ferrum::Browser#reset`. * Added YARD docs to `Ferrum::Page#network`. * Added YARD docs to `Ferrum::Network#traffic`. * Added YARD docs to `Ferrum::Network#request`. * Added YARD docs to `Ferrum::Network#response`. * Added YARD docs for `Ferrum::Network#status`. * Added YARD docs for `Ferrum::Network#wait_for_idle`. * Added YARD docs to `Ferrum::Network#clear`. * Added YARD docs to `Ferrum::Network#intercept`. * Added YARD docs for `Ferrum::Network#authorize`. * Added YARD docs for `Ferrum::Network#emulate_network_conditions`. * Added YARD docs for `Ferrum::Network#offline_mode`. * Add YARD docs to `Ferrum::Page#mouse`. * Added YARD docs to `Ferrum::Mouse#scroll_to`. * Added YARD docs to `Ferrum::Mouse#click`. * Added YARD docs for `Ferrum::Mouse#down`. * Added YARD docs to `Ferrum::Mouse#up`. * Added YARD docs to `Ferrum::Mouse#move`. * Added YARD docs to `Ferrum::Page#keyboard`. * Added YARD docs for `Ferrum::Keyboard#down`. * Added YARD docs for `Ferrum::Keyboard#up`. * Added YARD docs to `Ferrum::Keyboard#type`. * Added YARD docs to `Ferrum::Keyboard#modifiers`. * Added YARD docs to `Ferrum::Page#cookies`. * Added YARD docs to `Ferrum::Cookies::Cookie`. * Added YARD docs to `Ferrum::Cookies#all`. * Added YARD docs to `Ferrum::Cookies#[]`. * Added YARD docs to `Ferrum::Cookies#set`. * Added YARD docs to `Ferrum::Cookies#remove`. * Added YARD docs to `Ferrum::Cookies#clear`. * Added YARD docs for `Ferrum::Page#headers`. * Added YARD docs for `Ferrum::Headers#get`. * Added YARD docs for `Ferrum::Headers#set`. * Added YARD docs for `Ferrum::Headers#clear`. * Added YARD docs to `Ferrum::Headers#add`. * Added YARD docs for `Ferrum::Frame::Runtime#evaluate`. * Added YARD docs for `Ferrum::Frame::Runtime#evaluate_async`. * Added YARD docs for `Ferrum::Frame::Runtime#execute`. * Added YARD docs for `Ferrum::Browser#evaluate_on_new_document`. * Added YARD docs to `Ferrum::Frame::Runtime#add_script_tag`. * Added YARD docs to `Ferrum::Frame::Runtime#add_style_tag`. * Added YARD docs to `Ferrum::Page#bypass_csp`. * Added YARD docs to `Ferrum::Page::Frames#frames`. * Added YARD docs to `Ferrum::Page::Frames#frame_by`. * Added YARD docs to `Ferrum::Page::Frames#main_frame`. * Added YARD docs to `Ferrum::Frame`'s attributes. * Added YARD docs for `Ferrum::Frame#execution_id`. * Added YARD docs to `Ferrum::Frame#url`. * Added YARD docs to `Ferrum::Frame#title`. * Added YARD docs to `Ferrum::Frame#main?`. * Added YARD docs to `Ferrum::Frame#content=`. * Added YARD docs to `Ferrum::Dialog#accept`. * Added YARD docs to `Ferrum::Dialog#dismiss`. * Added an example to `Ferrum::Dialog#accept`. * Added YARD docs to `Ferrum::Page::Animation#playback_rate`. * Added YARD docs to `Ferrum::Page::Animation#playback_rate=`. * Added YARD docs to `Ferrum::Page::Tracing#record`. * Rescue when `yard` cannot be loaded due to using an alternate CI `Gemfile`. * Added YARD tags to `Ferrum::Network::Exchange`. * Added YARD docs to `Ferrum::Network::Request`. * Added YARD docs to `Ferrum::Network::Response`.
1 parent eec1967 commit 13158c0

24 files changed

+1365
-7
lines changed

.document

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lib/**/*.rb
2+
-
3+
CHANGELOG.md
4+
LICENSE

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.byebug_history
22
Gemfile.*
3+
doc
34
pkg
45
.idea
56
.ruby-version
7+
.yardoc

.yardopts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--markup markdown --title 'Ferrum Documentation'

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ gem "rubocop", "~> 1.22"
77
gem "rubocop-rake", require: false
88
gem "rubocop-rspec", require: false
99

10+
gem 'kramdown', '~> 2.0', require: false
11+
gem 'redcarpet', require: false, platform: :mri
12+
gem 'yard', '~> 0.9', require: false
13+
1014
gemspec

Rakefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ RSpec::Core::RakeTask.new("test") do |t|
99
end
1010

1111
task default: :test
12+
13+
begin
14+
require 'yard'
15+
YARD::Rake::YardocTask.new
16+
task docs: :yard
17+
rescue LoadError
18+
end

lib/ferrum/browser.rb

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,92 @@ class Browser
3838
:proxy_server
3939
attr_writer :timeout
4040

41+
#
42+
# Initializes the browser.
43+
#
44+
# @param [Hash{Symbol => Object}, nil] options
45+
# Additional browser options.
46+
#
47+
# @option options [Boolean] :headless (true)
48+
# Set browser as headless or not.
49+
#
50+
# @option options [Boolean] :xvfb (false)
51+
# Run browser in a virtual framebuffer.
52+
#
53+
# @option options [(Integer, Integer)] :window_size ([1024, 768])
54+
# The dimensions of the browser window in which to test, expressed as a
55+
# 2-element array, e.g. `[1024, 768]`.
56+
#
57+
# @option options [Array<String, Hash>] :extensions
58+
# An array of paths to files or JS source code to be preloaded into the
59+
# browser e.g.: `["/path/to/script.js", { source: "window.secret = 'top'" }]`
60+
#
61+
# @option options [#puts] :logger
62+
# When present, debug output is written to this object.
63+
#
64+
# @option options [Integer, Float] :slowmo
65+
# Set a delay in seconds to wait before sending command.
66+
# Usefull companion of headless option, so that you have time to see
67+
# changes.
68+
#
69+
# @option options [Numeric] :timeout (5)
70+
# The number of seconds we'll wait for a response when communicating with
71+
# browser.
72+
#
73+
# @option options [Boolean] :js_errors
74+
# When true, JavaScript errors get re-raised in Ruby.
75+
#
76+
# @option options [Boolean] :pending_connection_errors (true)
77+
# When main frame is still waiting for slow responses while timeout is
78+
# reached {PendingConnectionsError} is raised. It's better to figure out
79+
# why you have slow responses and fix or block them rather than turn this
80+
# setting off.
81+
#
82+
# @option options [:chrome, :firefox] :browser_name (:chrome)
83+
# Sets the browser's name. **Note:** only experimental support for
84+
# `:firefox` for now.
85+
#
86+
# @option options [String] :browser_path
87+
# Path to Chrome binary, you can also set ENV variable as
88+
# `BROWSER_PATH=some/path/chrome bundle exec rspec`.
89+
#
90+
# @option options [Hash] :browser_options
91+
# Additional command line options, [see them all](https://peter.sh/experiments/chromium-command-line-switches/)
92+
# e.g. `{ "ignore-certificate-errors" => nil }`
93+
#
94+
# @option options [Boolean] :ignore_default_browser_options
95+
# Ferrum has a number of default options it passes to the browser,
96+
# if you set this to `true` then only options you put in
97+
# `:browser_options` will be passed to the browser, except required ones
98+
# of course.
99+
#
100+
# @option options [Integer] :port
101+
# Remote debugging port for headless Chrome.
102+
#
103+
# @option options [String] :host
104+
# Remote debugging address for headless Chrome.
105+
#
106+
# @option options [String] :url
107+
# URL for a running instance of Chrome. If this is set, a browser process
108+
# will not be spawned.
109+
#
110+
# @option options [Integer] :process_timeout
111+
# How long to wait for the Chrome process to respond on startup.
112+
#
113+
# @option options [Integer] :ws_max_receive_size
114+
# How big messages to accept from Chrome over the web socket, in bytes.
115+
# Defaults to 64MB. Incoming messages larger this will cause a
116+
# {Ferrum::DeadBrowserError}.
117+
#
118+
# @option options [Hash] :proxy
119+
# Specify proxy settings, [read more](https://github.com/rubycdp/ferrum#proxy).
120+
#
121+
# @option options [String] :save_path
122+
# Path to save attachments with [Content-Disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) header.
123+
#
124+
# @option options [Hash] :env
125+
# Environment variables you'd like to pass through to the process.
126+
#
41127
def initialize(options = nil)
42128
options ||= {}
43129

@@ -80,6 +166,15 @@ def initialize(options = nil)
80166
start
81167
end
82168

169+
#
170+
# Sets the base URL.
171+
#
172+
# @param [String] value
173+
# The new base URL value.
174+
#
175+
# @return [String]
176+
# The base URL value.
177+
#
83178
def base_url=(value)
84179
parsed = Addressable::URI.parse(value)
85180
unless BASE_URL_SCHEMA.include?(parsed.normalized_scheme)
@@ -111,6 +206,19 @@ def extensions
111206
end
112207
end
113208

209+
#
210+
# Evaluate JavaScript to modify things before a page load.
211+
#
212+
# @param [String] expression
213+
# The JavaScript to add to each new document.
214+
#
215+
# @example
216+
# browser.evaluate_on_new_document <<~JS
217+
# Object.defineProperty(navigator, "languages", {
218+
# get: function() { return ["tlh"]; }
219+
# });
220+
# JS
221+
#
114222
def evaluate_on_new_document(expression)
115223
extensions << expression
116224
end
@@ -126,6 +234,20 @@ def command(*args)
126234
raise
127235
end
128236

237+
#
238+
# Closes browser tabs opened by the `Browser` instance.
239+
#
240+
# @example
241+
# # connect to a long-running Chrome process
242+
# browser = Ferrum::Browser.new(url: 'http://localhost:9222')
243+
#
244+
# browser.go_to("https://github.com/")
245+
#
246+
# # clean up, lest the tab stays there hanging forever
247+
# browser.reset
248+
#
249+
# browser.quit
250+
#
129251
def reset
130252
@window_size = @original_window_size
131253
contexts.reset
@@ -151,6 +273,13 @@ def crash
151273
command("Browser.crash")
152274
end
153275

276+
#
277+
# Gets the version information from the browser.
278+
#
279+
# @return [VersionInfo]
280+
#
281+
# @since 0.13
282+
#
154283
def version
155284
VersionInfo.new(command("Browser.getVersion"))
156285
end

lib/ferrum/browser/version_info.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,67 @@
11
module Ferrum
22
class Browser
3+
#
4+
# The browser's version information returned by [Browser.getVersion].
5+
#
6+
# [Browser.getVersion]: https://chromedevtools.github.io/devtools-protocol/1-3/Browser/#method-getVersion
7+
#
8+
# @since 0.13
9+
#
310
class VersionInfo
411

12+
#
13+
# Initializes the browser's version information.
14+
#
15+
# @param [Hash{String => Object}] properties
16+
# The object properties returned by [Browser.getVersion](https://chromedevtools.github.io/devtools-protocol/1-3/Browser/#method-getVersion).
17+
#
18+
# @api private
19+
#
520
def initialize(properties)
621
@properties = properties
722
end
823

24+
#
25+
# The Chrome DevTools protocol version.
26+
#
27+
# @return [String]
28+
#
929
def protocol_version
1030
@properties['protocolVersion']
1131
end
1232

33+
#
34+
# The Chrome version.
35+
#
36+
# @return [String]
37+
#
1338
def product
1439
@properties['product']
1540
end
1641

42+
#
43+
# The Chrome revision properties.
44+
#
45+
# @return [String]
46+
#
1747
def revision
1848
@properties['revision']
1949
end
2050

51+
#
52+
# The Chrome `User-Agent` string.
53+
#
54+
# @return [String]
55+
#
2156
def user_agent
2257
@properties['userAgent']
2358
end
2459

60+
#
61+
# The JavaScript engine version.
62+
#
63+
# @return [String]
64+
#
2565
def js_version
2666
@properties['jsVersion']
2767
end

0 commit comments

Comments
 (0)