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(browser, contexts, id) ⇒ Context

Returns a new instance of Context.



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

def initialize(browser, contexts, id)
  @browser, @contexts, @id = browser, contexts, id
  @targets = Concurrent::Hash.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) ⇒ Object



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

def add_target(params)
  target = Target.new(@browser, params)
  if target.window?
    @targets[target.id] = target
  else
    @pendings.put(target, @browser.timeout)
  end
end

#create_pageObject



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

def create_page
  create_target.page
end

#create_targetObject

Raises:



44
45
46
47
48
49
50
51
52
# File 'lib/ferrum/context.rb', line 44

def create_target
  target_id = @browser.command("Target.createTarget",
                               browserContextId: @id,
                               url: "about:blank")["targetId"]
  target = @pendings.take(@browser.timeout)
  raise NoSuchTargetError unless target.is_a?(Target)
  @targets[target.id] = target
  target
end

#default_targetObject



17
18
19
# File 'lib/ferrum/context.rb', line 17

def default_target
  @default_target ||= create_target
end

#delete_target(target_id) ⇒ Object



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

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

#disposeObject



71
72
73
# File 'lib/ferrum/context.rb', line 71

def dispose
  @contexts.dispose(@id)
end

#inspectObject



75
76
77
# File 'lib/ferrum/context.rb', line 75

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

#pageObject



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

def page
  default_target.page
end

#pagesObject



25
26
27
# File 'lib/ferrum/context.rb', line 25

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

#update_target(target_id, params) ⇒ Object



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

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)


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

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