page-object 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,11 @@
1
+ === Version 0.5.1 / 2011-11-16
2
+ * Enhancements
3
+ * Added instance level in_frame method
4
+ * Support for nesting all *_element instance methods inside in_frame call
5
+ * Support for nesting alerts inside an in_frame call
6
+ * Support for nesting confirms inside an in_frame call
7
+ * Support for nesting prompts inside an in_frame call
8
+
1
9
  === Version 0.5 / 2011-11-06
2
10
  * Enhancements
3
11
  * Validated support for JRuby
@@ -35,3 +35,23 @@ Feature: Handling frames
35
35
  Scenario: Nested frames
36
36
  Given I am on the nested frame elements page
37
37
  Then I should be able to click the link in the frame
38
+
39
+ Scenario: Identifying items in frames at runtime
40
+ Given I am on the frame elements page
41
+ When I type "page-object" into the text field from frame 1 identified dynamically
42
+ Then I should verify "page-object" in the text field for frame 1 identified dynamically
43
+
44
+ Scenario: Handling alerts inside frames
45
+ Given I am on the frame elements page
46
+ When I trigger an alert within a frame
47
+ Then I should be able to get the alert's message
48
+
49
+ Scenario: Handling confirms inside frames
50
+ Given I am on the frame elements page
51
+ When I trigger a confirm within a frame
52
+ Then I should be able to get the confirm message
53
+
54
+ Scenario: Handling prompts inside frames
55
+ Given I am on the frame elements page
56
+ When I trigger a prompt within a frame
57
+ Then I should be able to get the message and default value
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
+ <head>
4
+ <title>Frame 1</title>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ </head>
7
+ <body>
8
+ <h1>Frame 3</h1>
9
+ <p>Nam accumsan. Donec nisi pede, interdum eget, ultrices ac, vulputate vitae, nunc. Nulla lorem. Duis cursus pharetra dolor. Nulla accumsan hendrerit leo. Vivamus commodo. Nullam dignissim adipiscing est. Aliquam vitae orci in risus lobortis luctus. Ut luctus fermentum ligula. Nullam ipsum. Suspendisse sit amet nisi.</p>
10
+ <input id=alert_button type=button onclick="alert('I am an alert')" value=Alert>
11
+ <input id=confirm_button type=button onclick="this.value = confirm('set the value')" value=Confirm>
12
+ <input id=prompt_button type=button onclick='this.value = prompt("enter your name", "John Doe")' value=Prompt>
13
+ </body>
14
+ </html>
@@ -4,8 +4,9 @@
4
4
  <title>Frames</title>
5
5
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
6
  </head>
7
- <frameset cols="50%, 50%">
8
- <frame src="frame_1.html" id="frame_1" name="frame1" class="half" />
9
- <frame src="frame_2.html" id="frame_2" name="frame2" class="half" />
7
+ <frameset cols="40%, 40%, 20%">
8
+ <frame src="frame_1.html" id="frame_1" name="frame1"/>
9
+ <frame src="frame_2.html" id="frame_2" name="frame2"/>
10
+ <frame src="frame_3.html" id="frame_3" name="frame3"/>
10
11
  </frameset>
11
- </html>
12
+ </html>
@@ -33,20 +33,20 @@ Given /^I am on the iframe elements page$/ do
33
33
  @page.navigate_to(UrlHelper.iframe_elements)
34
34
  end
35
35
 
36
- When /^I type "([^"]*)" into the text field for frame 2 using "([^"]*)"$/ do |text, arg_type|
36
+ When /^I type "([^\"]*)" into the text field for frame 2 using "([^\"]*)"$/ do |text, arg_type|
37
37
  @page.send "text_field_2_#{arg_type}=".to_sym, text
38
38
  end
39
39
 
40
- Then /^I should verify "([^"]*)" is in the text field for frame 2 using "([^"]*)"$/ do |text, arg_type|
40
+ Then /^I should verify "([^\"]*)" is in the text field for frame 2 using "([^\"]*)"$/ do |text, arg_type|
41
41
  result = @page.send "text_field_2_#{arg_type}".to_sym
42
42
  result.should == text
43
43
  end
44
44
 
45
- When /^I type "([^"]*)" into the text field from frame 1 using "([^"]*)"$/ do |text, arg_type|
45
+ When /^I type "([^\"]*)" into the text field from frame 1 using "([^\"]*)"$/ do |text, arg_type|
46
46
  @page.send "text_field_1_#{arg_type}=".to_sym, text
47
47
  end
48
48
 
49
- Then /^I should verify "([^"]*)" is in the text field for frame 1 using "([^"]*)"$/ do |text, arg_type|
49
+ Then /^I should verify "([^\"]*)" is in the text field for frame 1 using "([^\"]*)"$/ do |text, arg_type|
50
50
  result = @page.send "text_field_1_#{arg_type}".to_sym
51
51
  result.should == text
52
52
  end
@@ -71,3 +71,38 @@ Then /^I should be able to click the link in the frame$/ do
71
71
  @page.text.should include "Success"
72
72
  end
73
73
 
74
+ When /^I type "([^\"]*)" into the text field from frame 1 identified dynamically$/ do |value|
75
+ @page.in_frame(:id => 'frame_1') do |frame|
76
+ @page.text_field_element(:name => 'senderElement', :frame => frame).value = value
77
+ end
78
+ end
79
+
80
+ Then /^I should verify "([^\"]*)" in the text field for frame 1 identified dynamically$/ do |value|
81
+ @page.in_frame(:id => 'frame_1') do |frame|
82
+ @page.text_field_element(:name => 'senderElement', :frame => frame).value.should == value
83
+ end
84
+ end
85
+
86
+ When /^I trigger an alert within a frame$/ do
87
+ @page.in_frame(:id => 'frame_3') do |frame|
88
+ @msg = @page.alert(frame) do
89
+ @page.button_element(:id => 'alert_button', :frame => frame).click
90
+ end
91
+ end
92
+ end
93
+
94
+ When /^I trigger a confirm within a frame$/ do
95
+ @page.in_frame(:id => 'frame_3') do |frame|
96
+ @msg = @page.confirm(true, frame) do
97
+ @page.button_element(:id => 'confirm_button', :frame => frame).click
98
+ end
99
+ end
100
+ end
101
+
102
+ When /^I trigger a prompt within a frame$/ do
103
+ @page.in_frame(:id => 'frame_3') do |frame|
104
+ @msg = @page.prompt("Cheezy", frame) do
105
+ @page.button_element(:id => 'prompt_button', :frame => frame).click
106
+ end
107
+ end
108
+ end
@@ -23,7 +23,7 @@ When /^I handle the alert$/ do
23
23
  end
24
24
  end
25
25
 
26
- Then /^I should be able to get the alert's message$/ do
26
+ Then /^I should be able to get the alert\'s message$/ do
27
27
  @msg.should == "I am an alert"
28
28
  end
29
29
 
data/lib/page-object.rb CHANGED
@@ -121,11 +121,12 @@ module PageObject
121
121
  # @page.button_that_causes_alert
122
122
  # end
123
123
  #
124
+ # @param frame optional parameter used when alert is nested within a frame
124
125
  # @param block a block that has the call that will cause the alert to display
125
126
  # @return [String] the message that was contained in the alert
126
127
  #
127
- def alert(&block)
128
- platform.alert(&block)
128
+ def alert(frame=nil, &block)
129
+ platform.alert(frame, &block)
129
130
  end
130
131
 
131
132
  #
@@ -137,11 +138,12 @@ module PageObject
137
138
  # end
138
139
  #
139
140
  # @param [bool] what response you want to return back from the confirm popup
141
+ # @param frame optional parameter used when the confirm is nested within a frame
140
142
  # @param block a block that has the call that will cause the confirm to display
141
143
  # @return [String] the message that was prompted in the confirm
142
144
  #
143
- def confirm(response, &block)
144
- platform.confirm(response, &block)
145
+ def confirm(response, frame=nil, &block)
146
+ platform.confirm(response, frame, &block)
145
147
  end
146
148
 
147
149
  #
@@ -153,14 +155,36 @@ module PageObject
153
155
  # end
154
156
  #
155
157
  # @param [string] the value returned to the caller of the prompt
158
+ # @param frame optional parameter used with the prompt is nested within a frame
156
159
  # @param block a block that has the call that will cause the prompt to display
157
160
  # @return [Hash] A has containing two keys - :message contains the prompt message and
158
161
  # :default_value contains the default value for the prompt if provided
159
162
  #
160
- def prompt(answer, &block)
161
- platform.prompt(answer, &block)
163
+ def prompt(answer, frame=nil, &block)
164
+ platform.prompt(answer, frame, &block)
162
165
  end
163
166
 
167
+ #
168
+ # Identify an element as existing within a frame or iframe. A frame parameter
169
+ # is passed to the block and must be passed to the other calls to PageObject.
170
+ # You can nest calls to in_frame by passing the frame to the next level.
171
+ #
172
+ # @example
173
+ # in_frame(:id => 'frame_id') do |frame|
174
+ # text_field_element(:id => 'fname', :frame => frame)
175
+ # end
176
+ #
177
+ # @param [Hash] identifier how we find the frame. The valid keys are:
178
+ # * :id => Watir and Selenium
179
+ # * :index => Watir and Selenium
180
+ # * :name => Watir and Selenium
181
+ # @param frame passed from a previous call to in_frame. Used to nest calls
182
+ # @param block that contains the calls to elements that exist inside the frame.
183
+ #
184
+ def in_frame(identifier, frame=nil, &block)
185
+ platform.in_frame(identifier, frame, &block)
186
+ end
187
+
164
188
  #
165
189
  # Override the normal showModalDialog call is it opens a window instead
166
190
  # of a dialog. You will need to attach to the new window in order to
@@ -68,7 +68,7 @@ module PageObject
68
68
  # platform method to handle an alert popup
69
69
  # See PageObject#alert
70
70
  #
71
- def alert(&block)
71
+ def alert(frame=nil, &block)
72
72
  @browser.execute_script "window.alert = function(msg) { window.__lastWatirAlert = msg; }"
73
73
  yield
74
74
  @browser.execute_script "return window.__lastWatirAlert"
@@ -78,7 +78,7 @@ module PageObject
78
78
  # platform method to handle a confirm popup
79
79
  # See PageObject#confirm
80
80
  #
81
- def confirm(response, &block)
81
+ def confirm(response, frame=nil, &block)
82
82
  @browser.execute_script "window.confirm = function(msg) { window.__lastWatirConfirm = msg; return #{!!response} }"
83
83
  yield
84
84
  @browser.execute_script "return window.__lastWatirConfirm"
@@ -88,11 +88,10 @@ module PageObject
88
88
  # platform method to handle a prompt popup
89
89
  # See PageObject#prompt
90
90
  #
91
- def prompt(answer, &block)
91
+ def prompt(answer, frame=nil, &block)
92
92
  @browser.execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{answer.to_json}; }"
93
93
  yield
94
94
  result = @browser.execute_script "return window.__lastWatirPrompt"
95
-
96
95
  result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k) }
97
96
  result
98
97
  end
@@ -113,6 +112,16 @@ module PageObject
113
112
  end
114
113
  end
115
114
  end
115
+
116
+ #
117
+ # platform method to switch to a frame and execute a block
118
+ # See PageObject#in_frame
119
+ #
120
+ def in_frame(identifier, frame=nil, &block)
121
+ switch_to_frame([identifier])
122
+ block.call(nil)
123
+ @browser.switch_to.default_content
124
+ end
116
125
 
117
126
  #
118
127
  # platform method to refresh the page
@@ -69,24 +69,40 @@ module PageObject
69
69
  # platform method to handle an alert popup
70
70
  # See PageObject#alert
71
71
  #
72
- def alert(&block)
73
- @browser.alert(&block)
72
+ def alert(frame=nil, &block)
73
+ switch_to_frame(frame)
74
+ @browser.wd.execute_script "window.alert = function(msg) { window.__lastWatirAlert = msg; }"
75
+ yield
76
+ value = @browser.wd.execute_script "return window.__lastWatirAlert"
77
+ switch_to_default_content(frame)
78
+ value
74
79
  end
75
80
 
76
81
  #
77
82
  # platform method to handle a confirm popup
78
83
  # See PageObject#confirm
79
84
  #
80
- def confirm(response, &block)
81
- @browser.confirm(response, &block)
85
+ def confirm(response, frame=nil, &block)
86
+ switch_to_frame(frame)
87
+ @browser.wd.execute_script "window.confirm = function(msg) { window.__lastWatirConfirm = msg; return #{!!response} }"
88
+ yield
89
+ value = @browser.wd.execute_script "return window.__lastWatirConfirm"
90
+ switch_to_default_content(frame)
91
+ value
82
92
  end
83
93
 
84
94
  #
85
95
  # platform method to handle a prompt popup
86
96
  # See PageObject#prompt
87
97
  #
88
- def prompt(answer, &block)
89
- @browser.prompt(answer, &block)
98
+ def prompt(answer, frame=nil, &block)
99
+ switch_to_frame(frame)
100
+ @browser.wd.execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{answer.to_json}; }"
101
+ yield
102
+ result = @browser.wd.execute_script "return window.__lastWatirPrompt"
103
+ switch_to_default_content(frame)
104
+ result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k) }
105
+ result
90
106
  end
91
107
 
92
108
  #
@@ -97,6 +113,17 @@ module PageObject
97
113
  win_id = {identifier.keys.first => /#{Regexp.escape(identifier.values.first)}/}
98
114
  @browser.window(win_id).use &block
99
115
  end
116
+
117
+ #
118
+ # platform method to switch to a frame and execute a block
119
+ # See PageObject#in_frame
120
+ #
121
+ def in_frame(identifier, frame=nil, &block)
122
+ frame = [] if frame.nil?
123
+ frame << identifier
124
+ block.call(frame)
125
+
126
+ end
100
127
 
101
128
  #
102
129
  # platform method to refresh the page
@@ -708,6 +735,15 @@ module PageObject
708
735
  def switch_to_default_content(frame_identifiers)
709
736
  @browser.wd.switch_to.default_content unless frame_identifiers.nil?
710
737
  end
738
+
739
+ def switch_to_frame(frame_identifiers)
740
+ unless frame_identifiers.nil?
741
+ frame_identifiers.each do |frame_id|
742
+ value = frame_id.values.first
743
+ @browser.wd.switch_to.frame(value)
744
+ end
745
+ end
746
+ end
711
747
  end
712
748
  end
713
749
  end
@@ -1,4 +1,4 @@
1
1
  module PageObject
2
2
  # @private
3
- VERSION = "0.5.0"
3
+ VERSION = "0.5.1"
4
4
  end
data/page-object.gemspec CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency 'watir-webdriver', '>= 0.3.8'
23
- s.add_dependency 'selenium-webdriver', '>= 2.10.0'
22
+ s.add_dependency 'watir-webdriver', '>= 0.3.9'
23
+ s.add_dependency 'selenium-webdriver', '>= 2.12.2'
24
24
 
25
25
  s.add_development_dependency 'rspec', '>= 2.6.0'
26
26
  s.add_development_dependency 'cucumber', '>= 1.1.0'
@@ -92,19 +92,22 @@ describe PageObject do
92
92
  end
93
93
 
94
94
  it "should override alert popup behavior" do
95
- watir_browser.should_receive(:alert)
95
+ watir_browser.should_receive(:wd).twice.and_return(watir_browser)
96
+ watir_browser.should_receive(:execute_script).twice
96
97
  watir_page_object.alert do
97
98
  end
98
99
  end
99
100
 
100
101
  it "should override confirm popup behavior" do
101
- watir_browser.should_receive(:confirm)
102
+ watir_browser.should_receive(:wd).twice.and_return(watir_browser)
103
+ watir_browser.should_receive(:execute_script).twice
102
104
  watir_page_object.confirm(true) do
103
105
  end
104
106
  end
105
107
 
106
108
  it "should override prompt popup behavior" do
107
- watir_browser.should_receive(:prompt)
109
+ watir_browser.should_receive(:wd).twice.and_return(watir_browser)
110
+ watir_browser.should_receive(:execute_script).twice
108
111
  watir_page_object.prompt("blah") do
109
112
  end
110
113
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: page-object
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.0
5
+ version: 0.5.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeff Morgan
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-11-06 00:00:00 Z
13
+ date: 2011-11-18 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: watir-webdriver
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.3.8
23
+ version: 0.3.9
24
24
  type: :runtime
25
25
  version_requirements: *id001
26
26
  - !ruby/object:Gem::Dependency
@@ -31,7 +31,7 @@ dependencies:
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 2.10.0
34
+ version: 2.12.2
35
35
  type: :runtime
36
36
  version_requirements: *id002
37
37
  - !ruby/object:Gem::Dependency
@@ -98,6 +98,7 @@ files:
98
98
  - features/hidden_field.feature
99
99
  - features/html/frame_1.html
100
100
  - features/html/frame_2.html
101
+ - features/html/frame_3.html
101
102
  - features/html/frames.html
102
103
  - features/html/iframes.html
103
104
  - features/html/images/circle.png
@@ -290,6 +291,7 @@ test_files:
290
291
  - features/hidden_field.feature
291
292
  - features/html/frame_1.html
292
293
  - features/html/frame_2.html
294
+ - features/html/frame_3.html
293
295
  - features/html/frames.html
294
296
  - features/html/iframes.html
295
297
  - features/html/images/circle.png