class Ferrum::Context

Constants

POSITION

Attributes

id[R]
targets[R]

Public Class Methods

new(browser, contexts, id) click to toggle source
# File lib/ferrum/context.rb, line 11
def initialize(browser, contexts, id)
  @browser, @contexts, @id = browser, contexts, id
  @targets = Concurrent::Map.new
  @pendings = Concurrent::MVar.new
end

Public Instance Methods

add_target(params) click to toggle source
# File lib/ferrum/context.rb, line 54
def add_target(params)
  target = Target.new(@browser, params)
  if target.window?
    @targets.put_if_absent(target.id, target)
  else
    @pendings.put(target, @browser.timeout)
  end
end
create_page() click to toggle source
# File lib/ferrum/context.rb, line 40
def create_page
  create_target.page
end
create_target() click to toggle source
# File lib/ferrum/context.rb, line 44
def create_target
  @browser.command("Target.createTarget",
                   browserContextId: @id,
                   url: "about:blank")
  target = @pendings.take(@browser.timeout)
  raise NoSuchTargetError unless target.is_a?(Target)
  @targets.put_if_absent(target.id, target)
  target
end
default_target() click to toggle source
# File lib/ferrum/context.rb, line 17
def default_target
  @default_target ||= create_target
end
delete_target(target_id) click to toggle source
# File lib/ferrum/context.rb, line 67
def delete_target(target_id)
  @targets.delete(target_id)
end
dispose() click to toggle source
# File lib/ferrum/context.rb, line 71
def dispose
  @contexts.dispose(@id)
end
has_target?(target_id) click to toggle source
# File lib/ferrum/context.rb, line 75
def has_target?(target_id)
  !!@targets[target_id]
end
inspect() click to toggle source
# File lib/ferrum/context.rb, line 79
def inspect
  %(#<#{self.class} @id=#{@id.inspect} @targets=#{@targets.inspect} @default_target=#{@default_target.inspect}>)
end
page() click to toggle source
# File lib/ferrum/context.rb, line 21
def page
  default_target.page
end
pages() click to toggle source
# File lib/ferrum/context.rb, line 25
def pages
  @targets.values.map(&:page)
end
update_target(target_id, params) click to toggle source
# File lib/ferrum/context.rb, line 63
def update_target(target_id, params)
  @targets[target_id].update(params)
end
windows(pos = nil, size = 1) click to toggle source

When we call `page` method on target it triggers ruby to connect to given page by WebSocket, if there are many opened windows but we need only one it makes more sense to get and connect to the needed one only which usually is the last one.

# File lib/ferrum/context.rb, line 33
def windows(pos = nil, size = 1)
  raise ArgumentError if pos && !POSITION.include?(pos)
  windows = @targets.values.select(&:window?)
  windows = windows.send(pos, size) if pos
  windows.map(&:page)
end