Class: Ferrum::Contexts
- Inherits:
-
Object
- Object
- Ferrum::Contexts
- Includes:
- Enumerable
- Defined in:
- lib/ferrum/contexts.rb
Instance Attribute Summary collapse
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
Instance Method Summary collapse
- #[](id) ⇒ Object
- #close_connections ⇒ Object
- #create(**options) ⇒ Object
- #default_context ⇒ Object
- #dispose(context_id) ⇒ Object
- #each(&block) ⇒ Object
- #find_by(target_id:) ⇒ Object
-
#initialize(client) ⇒ Contexts
constructor
A new instance of Contexts.
- #reset ⇒ Object
- #size ⇒ Object
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
#contexts ⇒ Object (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_connections ⇒ Object
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(**) response = @client.command("Target.createBrowserContext", **) context_id = response["browserContextId"] context = Context.new(@client, self, context_id) @contexts[context_id] = context context end |
#default_context ⇒ Object
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 |
#reset ⇒ Object
59 60 61 62 |
# File 'lib/ferrum/contexts.rb', line 59 def reset @default_context = nil @contexts.each_key { |id| dispose(id) } end |
#size ⇒ Object
64 65 66 |
# File 'lib/ferrum/contexts.rb', line 64 def size @contexts.size end |