Class: Ferrum::Frame
- Inherits:
-
Object
- Object
- Ferrum::Frame
- Defined in:
- lib/ferrum/frame.rb,
lib/ferrum/frame/dom.rb,
lib/ferrum/frame/runtime.rb
Overview
Defined Under Namespace
Constant Summary collapse
- STATE_VALUES =
i[ started_loading navigated stopped_loading canceled ].freeze
Constants included from Runtime
Runtime::INTERMITTENT_ATTEMPTS, Runtime::INTERMITTENT_SLEEP
Constants included from DOM
DOM::LINK_TAG, DOM::SCRIPT_SRC_TAG, DOM::SCRIPT_TEXT_TAG, DOM::STYLE_TAG
Instance Attribute Summary collapse
-
#id ⇒ String
The Frame's unique id.
-
#lifecycle_events ⇒ Array<Hash{String => (String|Float)}>
readonly
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
(also: #set_content)
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! ⇒ Integer
Execution context id which is used by JS, each frame has it's own context in which JS evaluates.
-
#execution_id=(value) ⇒ Integer?
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.
-
#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.
-
#title ⇒ String
Returns current frame's title.
-
#url ⇒ String
Returns current frame's
location.href.
Methods included from Runtime
#evaluate, #evaluate_async, #evaluate_func, #evaluate_on, #execute
Methods included from DOM
#add_script_tag, #add_style_tag, #at_css, #at_xpath, #body, #css, #current_title, #current_url, #doctype, #frame_element, #xpath
Constructor Details
#initialize(id, page, parent_id = nil) ⇒ Frame
Returns a new instance of Frame.
59 60 61 62 63 64 65 |
# File 'lib/ferrum/frame.rb', line 59 def initialize(id, page, parent_id = nil) @id = id @page = page @parent_id = parent_id @lifecycle_events = [] @execution_id = Concurrent::MVar.new end |
Instance Attribute Details
#id ⇒ String
The Frame's unique id.
27 28 29 |
# File 'lib/ferrum/frame.rb', line 27 def id @id end |
#lifecycle_events ⇒ Array<Hash{String => (String|Float)}> (readonly)
Frame's lifecycle events (navigation, load, paint, etc.).
57 58 59 |
# File 'lib/ferrum/frame.rb', line 57 def lifecycle_events @lifecycle_events end |
#loader_id ⇒ String?
Frame loader id.
52 53 54 |
# File 'lib/ferrum/frame.rb', line 52 def loader_id @loader_id end |
#name ⇒ String?
If frame was given a name it should be here.
32 33 34 |
# File 'lib/ferrum/frame.rb', line 32 def name @name end |
#page ⇒ Page (readonly)
The page the frame belongs to.
37 38 39 |
# File 'lib/ferrum/frame.rb', line 37 def page @page end |
#parent_id ⇒ String? (readonly)
Parent frame id if this one is nested in another one.
42 43 44 |
# File 'lib/ferrum/frame.rb', line 42 def parent_id @parent_id end |
#state ⇒ :started_loading, ...
One of the states frame's in.
47 48 49 |
# File 'lib/ferrum/frame.rb', line 47 def state @state end |
Instance Method Details
#content=(html) ⇒ Object Also known as: set_content
Sets a content of a given frame.
167 168 169 170 171 172 173 174 175 |
# File 'lib/ferrum/frame.rb', line 167 def content=(html) evaluate_async(%( document.open(); document.write(arguments[0]); document.close(); arguments[1](true); ), @page.timeout, html) @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.
201 202 203 204 205 206 |
# File 'lib/ferrum/frame.rb', line 201 def execution_id value = @execution_id.value return if value.instance_of?(Object) value end |
#execution_id! ⇒ 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.
188 189 190 191 192 193 |
# File 'lib/ferrum/frame.rb', line 188 def execution_id! value = @execution_id.borrow(@page.timeout, &:itself) raise NoExecutionContextError if value.instance_of?(Object) value end |
#execution_id=(value) ⇒ Integer?
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).
217 218 219 220 221 222 223 |
# File 'lib/ferrum/frame.rb', line 217 def execution_id=(value) if value.nil? @execution_id.try_take! else @execution_id.try_put!(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.
134 135 136 |
# File 'lib/ferrum/frame.rb', line 134 def idle? state == :stopped_loading end |
#inspect ⇒ String
Debug representation of the frame, including its internal state.
230 231 232 233 234 235 236 237 238 239 |
# File 'lib/ferrum/frame.rb', line 230 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 |
#main? ⇒ Boolean
If current frame is the main frame of the page (top of the tree).
120 121 122 |
# File 'lib/ferrum/frame.rb', line 120 def main? @parent_id.nil? end |
#parent ⇒ Frame?
Returns the parent frame if this frame is nested in another one.
151 152 153 |
# File 'lib/ferrum/frame.rb', line 151 def parent @page.frame_by(id: @parent_id) if @parent_id end |
#title ⇒ String
Returns current frame's title.
106 107 108 |
# File 'lib/ferrum/frame.rb', line 106 def title evaluate("document.title") end |
#url ⇒ String
Returns current frame's location.href.
92 93 94 |
# File 'lib/ferrum/frame.rb', line 92 def url evaluate("document.location.href") end |