Class: AePageObjects::MultipleWindows::WindowHandleManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ae_page_objects/multiple_windows/window_handle_manager.rb

Class Method Summary collapse

Class Method Details

.allObject



4
5
6
# File 'lib/ae_page_objects/multiple_windows/window_handle_manager.rb', line 4

def self.all
  browser.window_handles
end

.browserObject



29
30
31
# File 'lib/ae_page_objects/multiple_windows/window_handle_manager.rb', line 29

def self.browser
  Capybara.current_session.driver.browser
end

.close(handle) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ae_page_objects/multiple_windows/window_handle_manager.rb', line 33

def self.close(handle)
  all_handles_before_close = all()
  more_than_one_window     = (all_handles_before_close.size > 1)

  # We must protect against closing the last window as doing so will quit the entire browser
  # which would mess up subsequent tests.
  # http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#close()
  return false unless more_than_one_window

  current_handle_before_switch = current

  # switch to the window to close
  switch_to(handle)

  browser.close

  # need to switch back to something. Use whatever was switched from originally unless we just
  # closed that window, in which case just pick something else.
  switch_to(current_handle_before_switch == handle ?  all.first : current_handle_before_switch)

  true
end

.currentObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ae_page_objects/multiple_windows/window_handle_manager.rb', line 8

def self.current
  # Accessing browser.window_handle tries to find an existing page, which will blow up
  # if there isn't one. So... we look at the collection first as a guard.
  if all.empty?
    nil
  else
    browser.window_handle
  end
rescue => e
  if Capybara.current_session.driver.is_a?(Capybara::Selenium::Driver) &&
     e.is_a?(Selenium::WebDriver::Error::NoSuchWindowError)
    raise WindowNotFound
  end

  raise
end

.switch_to(handle) ⇒ Object



25
26
27
# File 'lib/ae_page_objects/multiple_windows/window_handle_manager.rb', line 25

def self.switch_to(handle)
  browser.switch_to.window(handle)
end