Class: Ferrum::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/target.rb,
sig/ferrum/target.rbs

Overview

Represents a CDP target, e.g. a page, iframe, dedicated/shared worker or service worker. Wraps the raw targetInfo params and lazily connects to the target as a Page or Worker over its own Client/SessionClient session, so a Target can exist without being connected to yet.

Constant Summary collapse

NEW_WINDOW_WAIT =

Returns:

  • (::Float)
ENV.fetch("FERRUM_NEW_WINDOW_WAIT", 0.3).to_f

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser_client, session_id = nil, params = nil, implicit: false) ⇒ Target

Returns a new instance of Target.

Parameters:

  • browser_client (Client)
  • session_id (String, nil) (defaults to: nil)
  • params (Hash[String, untyped], nil) (defaults to: nil)
  • implicit: (Boolean) (defaults to: false)


21
22
23
24
25
26
27
28
29
# File 'lib/ferrum/target.rb', line 21

def initialize(browser_client, session_id = nil, params = nil, implicit: false)
  @page = nil
  @worker = nil
  @session_id = session_id
  @params = params
  @implicit = implicit
  @browser_client = browser_client
  @options = browser_client.options
end

Instance Attribute Details

#options ⇒ Browser::Options (readonly)

Returns the value of attribute options.

Returns:



13
14
15
# File 'lib/ferrum/target.rb', line 13

def options
  @options
end

#session_id ⇒ String?

Note:

The writer is a private API, Ferrum sets it.

Returns:

  • (String, nil)


16
17
18
# File 'lib/ferrum/target.rb', line 16

def session_id
  @session_id
end

Instance Method Details

#build_page(**options) ⇒ Page

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.

Builds this target's Page, and passes it to the blocks registered with on_page before returning it. Called lazily by page, or eagerly by Context#create_page.

Parameters:

  • options (Hash)

Returns:



99
100
101
102
103
104
# File 'lib/ferrum/target.rb', line 99

def build_page(**options)
  maybe_sleep_if_new_window
  @page = Page.new(client, context_id: context_id, target_id: id, **options)
  @options.page_handlers.each { |handler| handler.call(@page) }
  @page
end

#build_worker ⇒ Worker

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.

Builds a Worker for this target. Called lazily by #worker.

Returns:



111
112
113
114
115
116
117
# File 'lib/ferrum/target.rb', line 111

def build_worker
  if session_id.nil?
    raise NoSuchTargetError, "No session with #{type} #{id}, attach to it with Context#attach_target first"
  end

  Worker.new(client, target_id: id, url: url, type: type)
end

#client ⇒ SessionClient

The client connected to this target's own session.

Returns:



86
87
88
# File 'lib/ferrum/target.rb', line 86

def client
  @client ||= build_client
end

#command(method, timeout: nil, **params) ⇒ Hash

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.

Sends a CDP command through #client.

Parameters:

  • method (String) —

    The CDP method name.

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

    How long to wait for the response, the browser's timeout by default.

  • params (Hash) —

    The command's parameters.

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

Returns:

  • (Hash) —

    The command's result.

Raises:



236
237
238
# File 'lib/ferrum/target.rb', line 236

def command(method, timeout: nil, **params)
  client.command(method, timeout: timeout || client.timeout, **params)
end

#connected? ⇒ Boolean

Whether this target has already been connected to as a Page or Worker.

Returns:

  • (Boolean)


47
48
49
# File 'lib/ferrum/target.rb', line 47

def connected?
  !!@page || !!@worker
end

#context_id ⇒ String?

The id of the browser context this target belongs to, nil for the browser's implicit context, which Chrome doesn't let us address by id, see Context#implicit?. Commands scoped to a browser context target it by omitting browserContextId.

Returns:

  • (String, nil)


169
170
171
# File 'lib/ferrum/target.rb', line 169

def context_id
  @params["browserContextId"] unless @implicit
end

#id ⇒ String

The target's id.

Returns:

  • (String)


122
123
124
# File 'lib/ferrum/target.rb', line 122

def id
  @params["targetId"]
end

#iframe? ⇒ Boolean

Whether this target is an iframe.

Returns:

  • (Boolean)


176
177
178
# File 'lib/ferrum/target.rb', line 176

def iframe?
  type == "iframe"
end

#maybe_sleep_if_new_window ⇒ 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.

Chrome fires no events for a newly opened window, so we sleep a bit to give it a chance to load before connecting.



214
215
216
217
# File 'lib/ferrum/target.rb', line 214

def maybe_sleep_if_new_window
  # Dirty hack because new window doesn't have events at all
  sleep(NEW_WINDOW_WAIT) if opener_id
end

#opener_id ⇒ String?

The id of the target that opened this one, set only for windows/tabs opened via window.open/links/etc.

Returns:

  • (String, nil)


152
153
154
# File 'lib/ferrum/target.rb', line 152

def opener_id
  @params["openerId"]
end

#page ⇒ Page

Connects to and returns this target's Page.

Returns:

Raises:



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

def page
  @page || build_page
end

#page? ⇒ Boolean

Whether this target is a page.

Returns:

  • (Boolean)


183
184
185
# File 'lib/ferrum/target.rb', line 183

def page?
  type == "page"
end

#parent_id ⇒ String?

The id of the target that spawned this one, set for iframes and workers. Unlike opener_id, which is only set for windows/tabs opened via window.open/links/etc.

Returns:

  • (String, nil)


159
160
161
# File 'lib/ferrum/target.rb', line 159

def parent_id
  @params["parentId"]
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.

Releases what waits on the target's page or worker, Chrome has detached, destroyed or lost it.



57
58
59
60
# File 'lib/ferrum/target.rb', line 57

def release_waiters
  @page&.release_waiters
  @worker&.release_waiters
end

#service_worker? ⇒ Boolean

Whether this target is a service worker.

Returns:

  • (Boolean)


204
205
206
# File 'lib/ferrum/target.rb', line 204

def service_worker?
  type == "service_worker"
end

#shared_worker? ⇒ Boolean

Whether this target is a shared worker.

Returns:

  • (Boolean)


197
198
199
# File 'lib/ferrum/target.rb', line 197

def shared_worker?
  type == "shared_worker"
end

#title ⇒ String

The target's title.

Returns:

  • (String)


137
138
139
# File 'lib/ferrum/target.rb', line 137

def title
  @params["title"]
end

#type ⇒ String

The target's type, e.g. "page", "iframe", "worker", "shared_worker", "service_worker".

Returns:

  • (String)


130
131
132
# File 'lib/ferrum/target.rb', line 130

def type
  @params["type"]
end

#update(params) ⇒ Hash

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.

Merges freshly received CDP target info into this target's params, e.g. on Target.targetInfoChanged.

Parameters:

  • params (Hash)

Returns:

  • (Hash)


39
40
41
# File 'lib/ferrum/target.rb', line 39

def update(params)
  @params.merge!(params)
end

#url ⇒ String

The target's URL.

Returns:

  • (String)


144
145
146
# File 'lib/ferrum/target.rb', line 144

def url
  @params["url"]
end

#worker ⇒ Worker

Connects to and returns this target's Worker.

Returns:

Raises:



79
80
81
# File 'lib/ferrum/target.rb', line 79

def worker
  @worker ||= build_worker
end

#worker? ⇒ Boolean

Whether this target is a dedicated worker.

Returns:

  • (Boolean)


190
191
192
# File 'lib/ferrum/target.rb', line 190

def worker?
  type == "worker"
end