ae_page_objects 0.4.1.dt3 → 0.4.1.dt4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module AePageObjects
2
- VERSION = '0.4.1.dt3'.freeze
2
+ VERSION = '0.4.1.dt4'.freeze
3
3
  end
@@ -1,33 +1,109 @@
1
1
  module AePageObjects
2
2
  class Window
3
- class << self
4
- def all
5
- @all ||= {}
3
+
4
+ class Registry < Hash
5
+ def [](window_or_handle)
6
+ if window_or_handle.is_a?(Window)
7
+ super(window_or_handle.handle)
8
+ else
9
+ super(window_or_handle)
10
+ end
6
11
  end
7
12
 
8
- def current
9
- current_handle = Capybara.current_session.driver.browser.window_handle
13
+ def add(window)
14
+ self[window.handle] = window
15
+ end
16
+
17
+ def remove(window)
18
+ self.delete(window.handle)
19
+ end
20
+ end
21
+
22
+ class HandleManager
23
+ def self.all
24
+ browser.window_handles
25
+ end
10
26
 
11
- window = all.keys.find do |window|
12
- window.handle == current_handle
27
+ def self.current
28
+ # Accessing browser.window_handle tries to find an existing page, which will blow up
29
+ # if there isn't one. So... we look at the collection first as a guard.
30
+ if all.empty?
31
+ nil
32
+ else
33
+ browser.window_handle
13
34
  end
35
+ end
36
+
37
+ def self.switch_to(handle)
38
+ browser.switch_to.window(handle)
39
+ end
40
+
41
+ def self.browser
42
+ Capybara.current_session.driver.browser
43
+ end
44
+
45
+ def self.close(handle)
46
+ all_handles_before_close = all()
47
+ more_than_one_window = (all_handles_before_close.size > 1)
48
+
49
+ # We must protect against closing the last window as doing so will quit the entire browser
50
+ # which would mess up subsequent tests.
51
+ # http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#close()
52
+ return false unless more_than_one_window
53
+
54
+ current_handle_before_switch = current
55
+
56
+ # switch to the window to close
57
+ switch_to(handle)
58
+
59
+ browser.close
14
60
 
15
- unless window
16
- window = new(current_handle)
17
- all[window] = window
61
+ # need to switch back to something. Use whatever was switched from originally unless we just
62
+ # closed that window, in which case just pick something else.
63
+ switch_to(current_handle_before_switch == handle ? all.first : current_handle_before_switch)
64
+
65
+ true
66
+ end
67
+ end
68
+
69
+ class << self
70
+ def registry
71
+ @registry ||= Registry.new
72
+ end
73
+
74
+ def close_all
75
+ all.each(&:close)
76
+ end
77
+
78
+ def all
79
+ HandleManager.all.map do |handle|
80
+ find(handle)
18
81
  end
82
+ end
83
+
84
+ def find(handle)
85
+ registry[handle] || create(handle)
86
+ end
87
+
88
+ def current
89
+ current_handle = HandleManager.current
19
90
 
20
- window
91
+ find(current_handle) if current_handle
92
+ end
93
+
94
+ def create(handle)
95
+ new(registry, handle)
21
96
  end
22
97
  end
23
98
 
24
99
  attr_reader :current_document, :handle
25
100
 
26
- def initialize(handle)
27
- @handle = handle
101
+ def initialize(registry, handle)
102
+ @registry = registry
103
+ @handle = handle
28
104
  @current_document = nil
29
105
 
30
- self.class.all[self] = self
106
+ @registry.add(self)
31
107
  end
32
108
 
33
109
  def current_document=(document)
@@ -36,16 +112,15 @@ module AePageObjects
36
112
  end
37
113
 
38
114
  def switch_to
39
- Capybara.current_session.driver.browser.switch_to.window(handle)
115
+ HandleManager.switch_to(handle)
40
116
  current_document
41
117
  end
42
118
 
43
119
  def close
44
- self.current_document = nil
45
-
46
- Capybara.current_session.execute_script("window.close();")
47
-
48
- self.class.all.delete(self)
120
+ if HandleManager.close(@handle)
121
+ self.current_document = nil
122
+ @registry.remove(self)
123
+ end
49
124
  end
50
125
  end
51
- end
126
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ae_page_objects
3
3
  version: !ruby/object:Gem::Version
4
- hash: 4196270005
4
+ hash: -4200723772
5
5
  prerelease: 6
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
9
  - 1
10
10
  - dt
11
- - 3
12
- version: 0.4.1.dt3
11
+ - 4
12
+ version: 0.4.1.dt4
13
13
  platform: ruby
14
14
  authors:
15
15
  - Donnie Tognazzini
@@ -20,7 +20,6 @@ cert_chain: []
20
20
  date: 2013-07-19 00:00:00 Z
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
- type: :runtime
24
23
  name: capybara
25
24
  version_requirements: &id001 !ruby/object:Gem::Requirement
26
25
  none: false
@@ -32,10 +31,10 @@ dependencies:
32
31
  - 1
33
32
  - 1
34
33
  version: "1.1"
35
- requirement: *id001
36
34
  prerelease: false
35
+ type: :runtime
36
+ requirement: *id001
37
37
  - !ruby/object:Gem::Dependency
38
- type: :development
39
38
  name: appraisal
40
39
  version_requirements: &id002 !ruby/object:Gem::Requirement
41
40
  none: false
@@ -48,10 +47,10 @@ dependencies:
48
47
  - 5
49
48
  - 1
50
49
  version: 0.5.1
51
- requirement: *id002
52
50
  prerelease: false
53
- - !ruby/object:Gem::Dependency
54
51
  type: :development
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
55
54
  name: mocha
56
55
  version_requirements: &id003 !ruby/object:Gem::Requirement
57
56
  none: false
@@ -64,10 +63,10 @@ dependencies:
64
63
  - 13
65
64
  - 3
66
65
  version: 0.13.3
67
- requirement: *id003
68
66
  prerelease: false
69
- - !ruby/object:Gem::Dependency
70
67
  type: :development
68
+ requirement: *id003
69
+ - !ruby/object:Gem::Dependency
71
70
  name: selenium-webdriver
72
71
  version_requirements: &id004 !ruby/object:Gem::Requirement
73
72
  none: false
@@ -78,8 +77,9 @@ dependencies:
78
77
  segments:
79
78
  - 0
80
79
  version: "0"
81
- requirement: *id004
82
80
  prerelease: false
81
+ type: :development
82
+ requirement: *id004
83
83
  description: Capybara Page Objects pattern
84
84
  email:
85
85
  - engineering@appfolio.com