Class: Ferrum::Contexts

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ Contexts

Returns a new instance of Contexts.



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

def initialize(browser)
  @contexts = Concurrent::Hash.new
  @browser = browser
  subscribe
  discover
end

Instance Attribute Details

#contextsObject (readonly)

Returns the value of attribute contexts.



7
8
9
# File 'lib/ferrum/contexts.rb', line 7

def contexts
  @contexts
end

Instance Method Details

#createObject



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

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

#default_contextObject



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

def default_context
  @default_context ||= create
end

#dispose(context_id) ⇒ Object



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

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

#find_by(target_id:) ⇒ Object



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

def find_by(target_id:)
  @contexts.find { |_, c| c.targets.keys.include?(target_id) }&.last
end

#resetObject



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

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