Class: Ferrum::Contexts

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Contexts

Returns a new instance of Contexts.



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

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.



9
10
11
# File 'lib/ferrum/contexts.rb', line 9

def contexts
  @contexts
end

Instance Method Details

#[](id) ⇒ Object



29
30
31
# File 'lib/ferrum/contexts.rb', line 29

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

#close_connectionsObject



55
56
57
# File 'lib/ferrum/contexts.rb', line 55

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

#create(**options) ⇒ Object



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

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



19
20
21
# File 'lib/ferrum/contexts.rb', line 19

def default_context
  @default_context ||= create
end

#dispose(context_id) ⇒ Object



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

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



23
24
25
26
27
# File 'lib/ferrum/contexts.rb', line 23

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

  @contexts.each(&block)
end

#find_by(target_id:) ⇒ Object



33
34
35
36
37
# File 'lib/ferrum/contexts.rb', line 33

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

#resetObject



59
60
61
62
# File 'lib/ferrum/contexts.rb', line 59

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

#sizeObject



64
65
66
# File 'lib/ferrum/contexts.rb', line 64

def size
  @contexts.size
end