Class: Ferrum::Frame

Inherits:
Object
  • Object
show all
Includes:
DOM, Runtime
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

Represents a frame (the main document or a nested iframe) within a Page. Each frame has its own execution context for JavaScript and its own lifecycle state, tracked via #state. DOM search and JS evaluation methods are provided by the included DOM and Runtime modules.

Defined Under Namespace

Modules: DOM, Runtime Classes: Script

Constant Summary collapse

STATE_VALUES =

Returns:

  • (::Array[:started_loading | :navigated | :stopped_loading | :canceled])
%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

Instance Method Summary collapse

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.

Parameters:

  • id (Object)
  • page (Object)
  • parent_id (Object, nil) (defaults to: nil)


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

Note:

The writer is a private API, Ferrum sets it.

The Frame's unique id.

Returns:

  • (String)


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.).

Returns:

  • (Array<Hash{String => (String|Float)}>)


77
78
79
# File 'lib/ferrum/frame.rb', line 77

def lifecycle_events
  @lifecycle_events
end

#loader_id ⇒ String?

Note:

The writer is a private API, Ferrum sets it.

Frame loader id.

Returns:

  • (String, nil)


67
68
69
# File 'lib/ferrum/frame.rb', line 67

def loader_id
  @loader_id
end

#name ⇒ String?

Note:

The writer is a private API, Ferrum sets it.

If frame was given a name it should be here.

Returns:

  • (String, nil)


40
41
42
# File 'lib/ferrum/frame.rb', line 40

def name
  @name
end

#page ⇒ Page (readonly)

The page the frame belongs to.

Returns:



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.

Returns:

  • (String, nil)


53
54
55
# File 'lib/ferrum/frame.rb', line 53

def parent_id
  @parent_id
end

#state ⇒ :started_loading, ...

Note:

The writer is a private API, Ferrum sets it.

One of the states frame's in.

Returns:

  • (:started_loading, :navigated, :stopped_loading, :canceled, nil)


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.

Examples:

browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
frame = browser.frames[1]
frame.html # <html lang="en"><head><style>body {transition: opacity ease-in 0.2s; }...
frame.content = "<html><head></head><body><p>lol</p></body></html>"
frame.html # => <html><head></head><body><p>lol</p></body></html>

Parameters:

  • html (String)

Returns:

  • (Object)

Raises:



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.

Returns:

  • (Integer, nil)


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.

Parameters:

  • timeout (Numeric, Deadline, nil) (defaults to: nil) —

    How long to wait for the id, defaults to the page's timeout.

Returns:

  • (Integer)

Raises:



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).

Parameters:

  • value (Integer, nil)

Returns:

  • (Integer, nil)


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.

Examples:

browser.go_to("https://example.com")
browser.main_frame.idle? # => true

Returns:

  • (Boolean)


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.

Returns:

  • (String)


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.

Examples:

world = page.main_frame.isolated_world("ferrum")
world.execute("window.secret = 'top'")
world.evaluate("window.secret") # => "top"
page.evaluate("window.secret") # => nil

Parameters:

  • name (String)

Returns:



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.

Returns:



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).

Examples:

browser.go_to("https://www.w3schools.com/tags/tag_frame.asp")
frame = browser.frame_by(id: "C09C4E4404314AAEAE85928EAC109A93")
frame.main? # => false

Returns:

  • (Boolean)


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.

Examples:

browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
frame = browser.frames.last
frame.url # => "https://www.openstreetmap.org/export/embed.html?bbox=-0.004017949104309083%2C51.47612752641776%2C0.00030577182769775396%2C51.478569861898606&layer=mapnik"
frame.parent.main? # => false
frame.parent.parent.main? # => false
frame.parent.parent.parent.main? # => true

Returns:



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.

Examples:

browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
frame = browser.frames[1]
frame.title # => HTML Demo: <iframe>

Returns:

  • (String)

Raises:



135
136
137
# File 'lib/ferrum/frame.rb', line 135

def title
  evaluate("document.title")
end

#url ⇒ String

Returns current frame's location.href.

Examples:

browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
frame = browser.frames[1]
frame.url # => https://interactive-examples.mdn.mozilla.net/pages/tabbed/iframe.html

Returns:

  • (String)

Raises:



119
120
121
# File 'lib/ferrum/frame.rb', line 119

def url
  evaluate("document.location.href")
end