Class: Capybara::Cuprite::Browser::Targets

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/cuprite/browser/targets.rb

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ Targets

Returns a new instance of Targets.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/capybara/cuprite/browser/targets.rb', line 6

def initialize(browser)
  @mutex = Mutex.new
  @browser = browser
  @_default = targets.first["targetId"]

  @browser.subscribe("Target.detachedFromTarget") do |params|
    page = remove_page(params["targetId"])
    page&.close_connection
  end

  reset
end

Instance Method Details

#close_window(target_id) ⇒ Object



53
54
55
# File 'lib/capybara/cuprite/browser/targets.rb', line 53

def close_window(target_id)
  remove_page(target_id)&.close
end

#open_new_windowObject



46
47
48
49
50
51
# File 'lib/capybara/cuprite/browser/targets.rb', line 46

def open_new_window
  target_id = @browser.command("Target.createTarget", url: "about:blank", browserContextId: @_context_id)["targetId"]
  page = Page.new(target_id, @browser)
  push(target_id, page)
  target_id
end

#pageObject

Raises:



29
30
31
32
# File 'lib/capybara/cuprite/browser/targets.rb', line 29

def page
  raise NoSuchWindowError unless @page
  @page
end

#push(target_id, page = nil) ⇒ Object



19
20
21
# File 'lib/capybara/cuprite/browser/targets.rb', line 19

def push(target_id, page = nil)
  @targets[target_id] = page
end

#refreshObject



23
24
25
26
27
# File 'lib/capybara/cuprite/browser/targets.rb', line 23

def refresh
  @mutex.synchronize do
    targets.each { |t| push(t["targetId"]) if !default?(t) && !has?(t) }
  end
end

#resetObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/capybara/cuprite/browser/targets.rb', line 78

def reset
  if @page
    @page.close
    @browser.command("Target.disposeBrowserContext", browserContextId: @_context_id)
  end

  @page = nil
  @targets = {}
  @_context_id = nil

  @_context_id = @browser.command("Target.createBrowserContext")["browserContextId"]
  target_id = @browser.command("Target.createTarget", url: "about:blank", browserContextId: @_context_id)["targetId"]
  @page = Page.new(target_id, @browser)
  push(target_id, @page)
end

#switch_to_window(target_id) ⇒ Object



42
43
44
# File 'lib/capybara/cuprite/browser/targets.rb', line 42

def switch_to_window(target_id)
  @page = find_or_create_page(target_id)
end

#window_handleObject



34
35
36
# File 'lib/capybara/cuprite/browser/targets.rb', line 34

def window_handle
  page.target_id
end

#window_handlesObject



38
39
40
# File 'lib/capybara/cuprite/browser/targets.rb', line 38

def window_handles
  @mutex.synchronize { @targets.keys }
end

#within_window(locator) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/capybara/cuprite/browser/targets.rb', line 57

def within_window(locator)
  original = window_handle

  if Capybara::VERSION.to_f < 3.0
    target_id = window_handles.find do |target_id|
      page = find_or_create_page(target_id)
      locator == page.frame_name
    end
    locator = target_id if target_id
  end

  if window_handles.include?(locator)
    switch_to_window(locator)
    yield
  else
    raise NoSuchWindowError
  end
ensure
  switch_to_window(original)
end