Class: Ferrum::Contexts

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ferrum/contexts.rb

Constant Summary collapse

ALLOWED_TARGET_TYPES =
%w[page iframe].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Contexts

Returns a new instance of Contexts.



13
14
15
16
17
18
19
# File 'lib/ferrum/contexts.rb', line 13

def initialize(client)
  @contexts = Concurrent::Map.new
  @client = client
  subscribe
  auto_attach
  discover
end

Instance Attribute Details

#contextsObject (readonly)

Returns the value of attribute contexts.



11
12
13
# File 'lib/ferrum/contexts.rb', line 11

def contexts
  @contexts
end

Instance Method Details

#[](id) ⇒ Object



31
32
33
# File 'lib/ferrum/contexts.rb', line 31

def [](id)
  @contexts[id]
end

#close_connectionsObject



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

def close_connections
  @contexts.each_value(&:close_targets_connection)
end

#create(**options) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/ferrum/contexts.rb', line 41

def create(**options)
  response = @client.command("Target.createBrowserContext", **options)
  context_id = response["browserContextId"]
  context = Context.new(@client, self, context_id)
  @contexts[context_id] = context
  context
end

#default_contextObject



21
22
23
# File 'lib/ferrum/contexts.rb', line 21

def default_context
  @default_context ||= create
end

#dispose(context_id) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/ferrum/contexts.rb', line 49

def dispose(context_id)
  context = @contexts[context_id]
  context.close_targets_connection
  @client.command("Target.disposeBrowserContext", browserContextId: context.id)
  @contexts.delete(context_id)
  true
end

#each(&block) ⇒ Object



25
26
27
28
29
# File 'lib/ferrum/contexts.rb', line 25

def each(&block)
  return enum_for(__method__) unless block_given?

  @contexts.each(&block)
end

#find_by(target_id:) ⇒ Object



35
36
37
38
39
# File 'lib/ferrum/contexts.rb', line 35

def find_by(target_id:)
  context = nil
  @contexts.each_value { |c| context = c if c.target?(target_id) }
  context
end

#resetObject



61
62
63
64
# File 'lib/ferrum/contexts.rb', line 61

def reset
  @default_context = nil
  @contexts.each_key { |id| dispose(id) }
end

#sizeObject



66
67
68
# File 'lib/ferrum/contexts.rb', line 66

def size
  @contexts.size
end