Class: Ferrum::Frame
- Inherits:
-
Object
- Object
- Ferrum::Frame
- Defined in:
- lib/ferrum/frame.rb,
lib/ferrum/frame/dom.rb,
lib/ferrum/frame/script.rb,
lib/ferrum/frame/runtime.rb,
sig/ferrum/frame.rbs,
sig/ferrum/frame/dom.rbs,
sig/ferrum/frame/script.rbs,
sig/ferrum/frame/runtime.rbs
Overview
Defined Under Namespace
Modules: DOM, Runtime Classes: Script
Constant Summary collapse
- STATE_VALUES =
%i[ started_loading navigated stopped_loading canceled ].freeze
Constants included from Runtime
Runtime::DEEP_SERIALIZATION, Runtime::INTERMITTENT_ATTEMPTS, Runtime::INTERMITTENT_SLEEP, Runtime::SHALLOW_SERIALIZATION, Runtime::UNSERIALIZABLE_NUMBERS
Constants included from DOM
DOM::LINK_TAG, DOM::SCRIPT_SRC_TAG, DOM::SCRIPT_TEXT_TAG, DOM::STYLE_TAG, DOM::WAIT_INTERVAL
Instance Attribute Summary collapse
-
#id ⇒ String
The Frame's unique id.
-
#lifecycle_events ⇒ Array<Hash{String => (String|Float)}>
readonly
private
Frame's lifecycle events (navigation, load, paint, etc.).
-
#loader_id ⇒ String?
Frame loader id.
-
#name ⇒ String?
If frame was given a name it should be here.
-
#page ⇒ Page
readonly
The page the frame belongs to.
-
#parent_id ⇒ String?
readonly
Parent frame id if this one is nested in another one.
-
#state ⇒ :started_loading, ...
One of the states frame's in.
Instance Method Summary collapse
-
#content=(html) ⇒ Object
Sets a content of a given frame.
-
#execution_id ⇒ Integer?
Execution context id which is used by JS, each frame has it's own context in which JS evaluates.
-
#execution_id!(timeout = nil) ⇒ Integer
Execution context id which is used by JS, each frame has it's own context in which JS evaluates.
-
#execution_id=(value) ⇒ Integer?
private
Sets the execution context id, or clears it if
nilis given (e.g. when the context is torn down mid-navigation and hasn't been replaced yet). -
#idle? ⇒ Boolean
Returns whether the frame has finished loading (+:stopped_loading+ state).
-
#initialize(id, page, parent_id = nil) ⇒ Frame
constructor
A new instance of Frame.
-
#inspect ⇒ String
Debug representation of the frame, including its internal state.
-
#isolated_world(name) ⇒ IsolatedWorld
Returns the frame's isolated world with the given name: its own JavaScript globals over the frame's DOM.
-
#isolated_worlds ⇒ Array<IsolatedWorld>
The frame's isolated worlds used so far, by #isolated_world, Page#evaluate_on_new_document with
world_name:or init scripts. -
#main? ⇒ Boolean
If current frame is the main frame of the page (top of the tree).
-
#parent ⇒ Frame?
Returns the parent frame if this frame is nested in another one.
-
#release_waiters ⇒ void
private
Wakes the threads waiting for the frame's execution context for good, the frame is gone and no context is coming.
-
#title ⇒ String
Returns current frame's title.
-
#url ⇒ String
Returns current frame's
location.href.
Methods included from Runtime
#evaluate, #evaluate_handle, #evaluate_in, #execute
Methods included from DOM
#add_script_tag, #add_style_tag, #at_css, #at_xpath, #css, #doctype, #frame_element, #html, #wait_css, #wait_css!, #wait_xpath, #wait_xpath!, #xpath
Constructor Details
#initialize(id, page, parent_id = nil) ⇒ Frame
Returns a new instance of Frame.
79 80 81 82 83 84 85 86 |
# File 'lib/ferrum/frame.rb', line 79 def initialize(id, page, parent_id = nil) @id = id @page = page @parent_id = parent_id @lifecycle_events = [] @execution_id = Concurrency::Cell.new @isolated_worlds = Concurrent::Map.new end |
Instance Attribute Details
#id ⇒ String
The writer is a private API, Ferrum sets it.
The Frame's unique id.
30 31 32 |
# File 'lib/ferrum/frame.rb', line 30 def id @id end |
#lifecycle_events ⇒ Array<Hash{String => (String|Float)}> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Frame's lifecycle events (navigation, load, paint, etc.).
77 78 79 |
# File 'lib/ferrum/frame.rb', line 77 def lifecycle_events @lifecycle_events end |
#loader_id ⇒ String?
The writer is a private API, Ferrum sets it.
Frame loader id.
67 68 69 |
# File 'lib/ferrum/frame.rb', line 67 def loader_id @loader_id end |
#name ⇒ String?
The writer is a private API, Ferrum sets it.
If frame was given a name it should be here.
40 41 42 |
# File 'lib/ferrum/frame.rb', line 40 def name @name end |
#page ⇒ Page (readonly)
The page the frame belongs to.
48 49 50 |
# File 'lib/ferrum/frame.rb', line 48 def page @page end |
#parent_id ⇒ String? (readonly)
Parent frame id if this one is nested in another one.
53 54 55 |
# File 'lib/ferrum/frame.rb', line 53 def parent_id @parent_id end |
#state ⇒ :started_loading, ...
The writer is a private API, Ferrum sets it.
One of the states frame's in.
60 61 62 |
# File 'lib/ferrum/frame.rb', line 60 def state @state end |
Instance Method Details
#content=(html) ⇒ Object
Sets a content of a given frame.
198 199 200 201 202 203 204 205 |
# File 'lib/ferrum/frame.rb', line 198 def content=(html) execute(<<~JS, html: html) document.open(); document.write(html); document.close(); JS @page.document_node_id end |
#execution_id ⇒ Integer?
Execution context id which is used by JS, each frame has it's own context in which JS evaluates.
231 232 233 |
# File 'lib/ferrum/frame.rb', line 231 def execution_id @execution_id.value end |
#execution_id!(timeout = nil) ⇒ Integer
Execution context id which is used by JS, each frame has it's own context in which JS evaluates. Locks for a page timeout and raises an error if an execution id hasn't been set yet, if id is set returns immediately.
220 221 222 223 |
# File 'lib/ferrum/frame.rb', line 220 def execution_id!(timeout = nil) @execution_id.wait(timeout || @page.timeout) @execution_id.value || raise(NoExecutionContextError) end |
#execution_id=(value) ⇒ Integer?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the execution context id, or clears it if nil is given (e.g.
when the context is torn down mid-navigation and hasn't been
replaced yet).
245 246 247 248 249 250 251 |
# File 'lib/ferrum/frame.rb', line 245 def execution_id=(value) if value.nil? @execution_id.clear elsif !@execution_id.set? @execution_id.set(value) end end |
#idle? ⇒ Boolean
Returns whether the frame has finished loading (+:stopped_loading+ state).
Frames in :canceled state (execution context torn down mid-navigation)
are not considered idle.
163 164 165 |
# File 'lib/ferrum/frame.rb', line 163 def idle? state == :stopped_loading end |
#inspect ⇒ String
Debug representation of the frame, including its internal state.
297 298 299 300 301 302 303 304 305 306 |
# File 'lib/ferrum/frame.rb', line 297 def inspect "#<#{self.class} " \ "@id=#{@id.inspect} " \ "@parent_id=#{@parent_id.inspect} " \ "@name=#{@name.inspect} " \ "@loader_id=#{@loader_id.inspect} " \ "@lifecycle_events=#{@lifecycle_events.inspect} " \ "@state=#{@state.inspect} " \ "@execution_id=#{execution_id.inspect}>" end |
#isolated_world(name) ⇒ IsolatedWorld
Returns the frame's isolated world with the given name: its own JavaScript globals over the frame's DOM. Created on first use.
278 279 280 |
# File 'lib/ferrum/frame.rb', line 278 def isolated_world(name) @isolated_worlds.compute_if_absent(name) { IsolatedWorld.new(self, name) } end |
#isolated_worlds ⇒ Array<IsolatedWorld>
The frame's isolated worlds used so far, by #isolated_world,
Page#evaluate_on_new_document with world_name: or init scripts.
288 289 290 |
# File 'lib/ferrum/frame.rb', line 288 def isolated_worlds @isolated_worlds.values end |
#main? ⇒ Boolean
If current frame is the main frame of the page (top of the tree).
149 150 151 |
# File 'lib/ferrum/frame.rb', line 149 def main? @parent_id.nil? end |
#parent ⇒ Frame?
Returns the parent frame if this frame is nested in another one.
180 181 182 |
# File 'lib/ferrum/frame.rb', line 180 def parent @page.frame_by(id: @parent_id) if @parent_id end |
#release_waiters ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Wakes the threads waiting for the frame's execution context for good, the frame is gone and no context is coming.
260 261 262 |
# File 'lib/ferrum/frame.rb', line 260 def release_waiters @execution_id.close end |
#title ⇒ String
Returns current frame's title.
135 136 137 |
# File 'lib/ferrum/frame.rb', line 135 def title evaluate("document.title") end |
#url ⇒ String
Returns current frame's location.href.
119 120 121 |
# File 'lib/ferrum/frame.rb', line 119 def url evaluate("document.location.href") end |