Class: Ferrum::Page
- Inherits:
-
Object
- Object
- Ferrum::Page
- Includes:
- DOM, Frame, Net, Runtime, Screenshot
- Defined in:
- lib/ferrum/page.rb,
lib/ferrum/page/dom.rb,
lib/ferrum/page/net.rb,
lib/ferrum/page/frame.rb,
lib/ferrum/page/runtime.rb,
lib/ferrum/page/screenshot.rb
Defined Under Namespace
Modules: DOM, Frame, Net, Runtime, Screenshot Classes: Event
Constant Summary collapse
- NEW_WINDOW_WAIT =
ENV.fetch("FERRUM_NEW_WINDOW_WAIT", 0.3).to_f
Constants included from Runtime
Runtime::DEFAULT_OPTIONS, Runtime::EVALUATE_ASYNC_OPTIONS, Runtime::EXECUTE_OPTIONS, Runtime::INTERMITTENT_ATTEMPTS, Runtime::INTERMITTENT_SLEEP
Constants included from Net
Net::AUTHORIZE_TYPE, Net::RESOURCE_TYPES
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#keyboard ⇒ Object
readonly
Returns the value of attribute keyboard.
-
#mouse ⇒ Object
readonly
Returns the value of attribute mouse.
-
#referrer ⇒ Object
Returns the value of attribute referrer.
-
#response_headers ⇒ Object
readonly
Returns the value of attribute response_headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#target_id ⇒ Object
readonly
Returns the value of attribute target_id.
Instance Method Summary collapse
- #back ⇒ Object
- #clear_network_traffic ⇒ Object
- #close ⇒ Object
- #close_connection ⇒ Object
- #command(method, wait: 0, **params) ⇒ Object
- #forward ⇒ Object
- #goto(url = nil) ⇒ Object
-
#initialize(target_id, browser, new_window = false) ⇒ Page
constructor
A new instance of Page.
- #network_traffic(type = nil) ⇒ Object
- #on(name, &block) ⇒ Object
- #refresh ⇒ Object
- #resize(width: nil, height: nil, fullscreen: false) ⇒ Object
- #timeout ⇒ Object
Methods included from DOM
#at_css, #at_xpath, #body, #css, #current_url, #title, #xpath
Methods included from Runtime
#evaluate, #evaluate_async, #evaluate_on, #execute
Methods included from Frame
#execution_context_id, #frame_name, #frame_title, #frame_url, #within_frame
Methods included from Net
#abort_request, #authorize, #continue_request, #intercept_request
Methods included from Screenshot
Constructor Details
#initialize(target_id, browser, new_window = false) ⇒ Page
Returns a new instance of Page.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ferrum/page.rb', line 62 def initialize(target_id, browser, new_window = false) @target_id, @browser = target_id, browser @network_traffic = [] @event = Event.new.tap(&:set) @frames = {} @waiting_frames ||= Set.new @frame_stack = [] # Dirty hack because new window doesn't have events at all sleep(NEW_WINDOW_WAIT) if new_window @session_id = @browser.command("Target.attachToTarget", targetId: @target_id)["sessionId"] host = @browser.process.host port = @browser.process.port ws_url = "ws://#{host}:#{port}/devtools/page/#{@target_id}" @client = Browser::Client.new(browser, ws_url, 1000) @mouse, @keyboard = Mouse.new(self), Keyboard.new(self) @headers, @cookies = Headers.new(self), Cookies.new(self) subscribe prepare_page end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
57 58 59 |
# File 'lib/ferrum/page.rb', line 57 def browser @browser end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
57 58 59 |
# File 'lib/ferrum/page.rb', line 57 def @cookies end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
57 58 59 |
# File 'lib/ferrum/page.rb', line 57 def headers @headers end |
#keyboard ⇒ Object (readonly)
Returns the value of attribute keyboard.
57 58 59 |
# File 'lib/ferrum/page.rb', line 57 def keyboard @keyboard end |
#mouse ⇒ Object (readonly)
Returns the value of attribute mouse.
57 58 59 |
# File 'lib/ferrum/page.rb', line 57 def mouse @mouse end |
#referrer ⇒ Object
Returns the value of attribute referrer.
56 57 58 |
# File 'lib/ferrum/page.rb', line 56 def referrer @referrer end |
#response_headers ⇒ Object (readonly)
Returns the value of attribute response_headers.
57 58 59 |
# File 'lib/ferrum/page.rb', line 57 def response_headers @response_headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
57 58 59 |
# File 'lib/ferrum/page.rb', line 57 def status @status end |
#target_id ⇒ Object (readonly)
Returns the value of attribute target_id.
57 58 59 |
# File 'lib/ferrum/page.rb', line 57 def target_id @target_id end |
Instance Method Details
#back ⇒ Object
149 150 151 |
# File 'lib/ferrum/page.rb', line 149 def back history_navigate(delta: -1) end |
#clear_network_traffic ⇒ Object
145 146 147 |
# File 'lib/ferrum/page.rb', line 145 def clear_network_traffic @network_traffic = [] end |
#close ⇒ Object
106 107 108 109 110 111 |
# File 'lib/ferrum/page.rb', line 106 def close @headers.clear @browser.command("Target.detachFromTarget", sessionId: @session_id) @browser.command("Target.closeTarget", targetId: @target_id) close_connection end |
#close_connection ⇒ Object
113 114 115 |
# File 'lib/ferrum/page.rb', line 113 def close_connection @client.close end |
#command(method, wait: 0, **params) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/ferrum/page.rb', line 157 def command(method, wait: 0, **params) iteration = @event.reset if wait > 0 result = @client.command(method, params) if wait > 0 @event.wait(wait) @event.wait(@browser.timeout) if iteration != @event.iteration end result end |
#forward ⇒ Object
153 154 155 |
# File 'lib/ferrum/page.rb', line 153 def forward history_navigate(delta: 1) end |
#goto(url = nil) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ferrum/page.rb', line 92 def goto(url = nil) = { url: combine_url!(url) } .merge!(referrer: referrer) if referrer response = command("Page.navigate", wait: timeout, **) # https://cs.chromium.org/chromium/src/net/base/net_error_list.h if %w[net::ERR_NAME_NOT_RESOLVED net::ERR_NAME_RESOLUTION_FAILED net::ERR_INTERNET_DISCONNECTED net::ERR_CONNECTION_TIMED_OUT].include?(response["errorText"]) raise StatusError, [:url] end response["frameId"] end |
#network_traffic(type = nil) ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ferrum/page.rb', line 134 def network_traffic(type = nil) case type.to_s when "all" @network_traffic when "blocked" @network_traffic.select { |r| r.response.nil? } # when request blocked else @network_traffic.select { |r| r.response } # when request isn't blocked end end |
#on(name, &block) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ferrum/page.rb', line 167 def on(name, &block) case name when :dialog @client.on("Page.javascriptDialogOpening") do |params, index, total| dialog = Dialog.new(self, params) block.call(dialog, index, total) end when :request_intercepted @client.on("Network.requestIntercepted") do |params, index, total| request = Network::InterceptedRequest.new(self, params) block.call(request, index, total) end else @client.on(name, &block) end end |
#refresh ⇒ Object
130 131 132 |
# File 'lib/ferrum/page.rb', line 130 def refresh command("Page.reload", wait: timeout) end |
#resize(width: nil, height: nil, fullscreen: false) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ferrum/page.rb', line 117 def resize(width: nil, height: nil, fullscreen: false) result = @browser.command("Browser.getWindowForTarget", targetId: @target_id) @window_id, @bounds = result.values_at("windowId", "bounds") if fullscreen @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { windowState: "fullscreen" }) else @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { windowState: "normal" }) @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { width: width, height: height, windowState: "normal" }) command("Emulation.setDeviceMetricsOverride", width: width, height: height, deviceScaleFactor: 1, mobile: false) end end |
#timeout ⇒ Object
88 89 90 |
# File 'lib/ferrum/page.rb', line 88 def timeout @browser.timeout end |