Class: Ferrum::Context

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

Constant Summary collapse

POSITION =
%i[first last].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, contexts, id) ⇒ Context

Returns a new instance of Context.



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

def initialize(client, contexts, id)
  @id = id
  @client = client
  @contexts = contexts
  @targets = Concurrent::Map.new
  @pendings = Concurrent::MVar.new
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#targetsObject (readonly)

Returns the value of attribute targets.



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

def targets
  @targets
end

Instance Method Details

#add_target(params:, session_id: nil) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/ferrum/context.rb', line 56

def add_target(params:, session_id: nil)
  new_target = Target.new(@client, session_id, params)
  target = @targets.put_if_absent(new_target.id, new_target)
  target ||= new_target # `put_if_absent` returns nil if added a new value or existing if there was one already
  @pendings.put(target, @client.timeout) if @pendings.empty?
  target
end

#close_targets_connectionObject



72
73
74
75
76
77
78
# File 'lib/ferrum/context.rb', line 72

def close_targets_connection
  @targets.each_value do |target|
    next unless target.connected?

    target.page.close_connection
  end
end

#create_page(**options) ⇒ Object



43
44
45
46
# File 'lib/ferrum/context.rb', line 43

def create_page(**options)
  target = create_target
  target.page = target.build_page(**options)
end

#create_targetObject

Raises:



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

def create_target
  @client.command("Target.createTarget", browserContextId: @id, url: "about:blank")
  target = @pendings.take(@client.timeout)
  raise NoSuchTargetError unless target.is_a?(Target)

  target
end

#default_targetObject



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

def default_target
  @default_target ||= create_target
end

#delete_target(target_id) ⇒ Object



68
69
70
# File 'lib/ferrum/context.rb', line 68

def delete_target(target_id)
  @targets.delete(target_id)
end

#disposeObject



80
81
82
# File 'lib/ferrum/context.rb', line 80

def dispose
  @contexts.dispose(@id)
end

#inspectObject



88
89
90
# File 'lib/ferrum/context.rb', line 88

def inspect
  %(#<#{self.class} @id=#{@id.inspect} @targets=#{@targets.inspect} @default_target=#{@default_target.inspect}>)
end

#pageObject



23
24
25
# File 'lib/ferrum/context.rb', line 23

def page
  default_target.page
end

#pagesObject



27
28
29
# File 'lib/ferrum/context.rb', line 27

def pages
  @targets.values.map(&:page)
end

#target?(target_id) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ferrum/context.rb', line 84

def target?(target_id)
  !!@targets[target_id]
end

#update_target(target_id, params) ⇒ Object



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

def update_target(target_id, params)
  @targets[target_id]&.update(params)
end

#windows(pos = nil, size = 1) ⇒ Object

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.

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
# File 'lib/ferrum/context.rb', line 35

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