page-object 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,18 @@
1
+ === Version 0.0.5 / 2011-06-15
2
+ * Enhancements
3
+ * Added rows method to Table to return number or rows
4
+ * Added columns method to TableRow to return the number of columns
5
+ * Table now supports Enumerable to iterate over the TableRows
6
+ * TableRow now supports Enumerable to iterate over TableCells
7
+ * Added items method to UnorderedList to return number of ListItems
8
+ * Added items method to OrderedList to return number of ListItems
9
+ * UnorderedList now supports Enumerable to iterate over the ListItems
10
+ * OrderedList now supports Enumerable to iterate over the ListItems
11
+ * All element methods now take an optional block that can be executed passing a browser
12
+ * Created PageFactory module to add factory methods to your step definitions
13
+ * Thanks Alister Scott for the idea
14
+
15
+
1
16
  === Version 0.0.4 / 2011-06-13
2
17
  * Enhancements
3
18
  * Added support for the following elements
@@ -12,16 +27,13 @@
12
27
  * Added support for the following elements
13
28
  * span
14
29
  * image
15
-
16
30
  * Added the following methods to Element
17
31
  * value
18
32
  * ==
19
33
  * tag_name
20
34
  * attribute
21
35
  * click
22
-
23
36
  * Updated to use selenium-webdriver 0.2.1
24
-
25
37
  * Updated to use watir-webdriver 0.2.4
26
38
 
27
39
  === Version 0.0.2 / 2011-05-30
@@ -32,7 +44,6 @@
32
44
  * table
33
45
  * table row
34
46
  * table cell
35
-
36
47
  * Added text method to element
37
48
 
38
49
  === Version 0.0.1 / 2011-05-22
@@ -43,7 +54,6 @@
43
54
  * radio button
44
55
  * select list
45
56
  * text field
46
-
47
57
  * Support for the following page level functions
48
58
  * text
49
59
  * html
@@ -11,6 +11,8 @@ Feature: Ordered list
11
11
  When I search for the ordered list by "<search_by>"
12
12
  And I get the first item from the list
13
13
  Then the list item's text should be "Number One"
14
+ And the list should contain 3 items
15
+ And each item should contain "Number"
14
16
 
15
17
  Scenarios:
16
18
  | search_by |
@@ -31,8 +31,6 @@ Feature: Select List
31
31
  Scenarios:
32
32
  | search_by |
33
33
  | index |
34
- # | value |
35
- # | text |
36
34
 
37
35
  Scenario: Retrieve a select list
38
36
  When I retrieve a select list
@@ -28,7 +28,7 @@ Feature: Span
28
28
 
29
29
 
30
30
  @selenium_only
31
- Scenario Outline: Locating span on Watir only
31
+ Scenario Outline: Locating span on Selenium only
32
32
  When I search for the span by "<search_by>"
33
33
  Then the text should be "My alert"
34
34
 
@@ -220,3 +220,32 @@ end
220
220
  When /^I search for the ordered list by "([^"]*)"$/ do |how|
221
221
  @list = @page.send "ol_#{how}_ordered_list"
222
222
  end
223
+
224
+ Then /^the table should have "([^"]*)" rows$/ do |rows|
225
+ @element.rows.should == rows.to_i
226
+ end
227
+
228
+ Then /^each row should contain "([^"]*)"$/ do |text|
229
+ @element.each do |row|
230
+ row.text.should include text
231
+ end
232
+ end
233
+
234
+ Then /^row "([^"]*)" should have "([^"]*)" columns$/ do |row, cols|
235
+ @element[row.to_i - 1].columns.should == cols.to_i
236
+ end
237
+
238
+ Then /^each column should contain "([^"]*)"$/ do |text|
239
+ row = @element[0]
240
+ row.each do |column|
241
+ column.text.should include text
242
+ end
243
+ end
244
+
245
+ Then /^the list should contain (\d+) items$/ do |items|
246
+ @list.items.should == items.to_i
247
+ end
248
+
249
+ Then /^each item should contain "([^"]*)"$/ do |text|
250
+ @list.each { |item| item.text.should include text }
251
+ end
@@ -19,7 +19,10 @@ Feature: Table
19
19
  When I retrieve a table element
20
20
  Then the data for row "1" should be "Data1" and "Data2"
21
21
  And the data for row "2" should be "Data3" and "Data4"
22
-
22
+ And the table should have "2" rows
23
+ And each row should contain "Data"
24
+ And row "1" should have "2" columns
25
+ And each column should contain "Data"
23
26
 
24
27
  Scenario Outline: Locating table cells on the Page
25
28
  When I retrieve a table element by "<search_by>"
@@ -11,6 +11,8 @@ Feature: Unordered list
11
11
  When I search for the unordered list by "<search_by>"
12
12
  And I get the first item from the list
13
13
  Then the list item's text should be "Item One"
14
+ And the list should contain 3 items
15
+ And each item should contain "Item"
14
16
 
15
17
  Scenarios:
16
18
  | search_by |
@@ -41,9 +41,10 @@ module PageObject
41
41
  #
42
42
  # @param [Watir::Browser or Selenium::WebDriver::Driver] the platform browser to use
43
43
  #
44
- def initialize(browser)
44
+ def initialize(browser, visit=false)
45
45
  @browser = browser
46
46
  include_platform_driver(browser)
47
+ goto if visit && respond_to?(:goto)
47
48
  end
48
49
 
49
50
  # @private
@@ -9,6 +9,19 @@ module PageObject
9
9
  # @see PageObject::SeleniumPageObject for the selenium implementation of the platform delegate
10
10
  #
11
11
  module Accessors
12
+
13
+ #
14
+ # Specify the url for the page. A call to this method will generate a
15
+ # 'goto' method to take you to the page.
16
+ #
17
+ # @param [String] the url for the page.
18
+ #
19
+ def page_url(url)
20
+ define_method("goto") do
21
+ platform.navigate_to url
22
+ end
23
+ end
24
+
12
25
  #
13
26
  # adds three methods to the page object - one to set text in a text field,
14
27
  # another to retrieve text from a text field and another to return the text
@@ -29,8 +42,9 @@ module PageObject
29
42
  # * :text => Watir only
30
43
  # * :value => Watir only
31
44
  # * :xpath => Watir and Selenium
45
+ # @param optional block to be invoked when element method is called
32
46
  #
33
- def text_field(name, identifier)
47
+ def text_field(name, identifier=nil, &block)
34
48
  define_method(name) do
35
49
  platform.text_field_value_for identifier
36
50
  end
@@ -38,7 +52,7 @@ module PageObject
38
52
  platform.text_field_value_set(identifier, value)
39
53
  end
40
54
  define_method("#{name}_text_field") do
41
- platform.text_field_for identifier
55
+ block ? block.call(browser) : platform.text_field_for(identifier)
42
56
  end
43
57
  end
44
58
 
@@ -60,13 +74,14 @@ module PageObject
60
74
  # * :tag_name => Watir and Selenium
61
75
  # * :text => Watir only
62
76
  # * :xpath => Watir and Selenium
77
+ # @param optional block to be invoked when element method is called
63
78
  #
64
- def hidden_field(name, identifier)
79
+ def hidden_field(name, identifier=nil, &block)
65
80
  define_method(name) do
66
81
  platform.hidden_field_value_for identifier
67
82
  end
68
83
  define_method("#{name}_hidden_field") do
69
- platform.hidden_field_for identifier
84
+ block ? block.call(browser) : platform.hidden_field_for(identifier)
70
85
  end
71
86
  end
72
87
 
@@ -88,8 +103,9 @@ module PageObject
88
103
  # * :name => Watir and Selenium
89
104
  # * :tag_name => Watir and Selenium
90
105
  # * :xpath => Watir and Selenium
106
+ # @param optional block to be invoked when element method is called
91
107
  #
92
- def text_area(name, identifier)
108
+ def text_area(name, identifier=nil, &block)
93
109
  define_method(name) do
94
110
  platform.text_area_value_for identifier
95
111
  end
@@ -97,7 +113,7 @@ module PageObject
97
113
  platform.text_area_value_set(identifier, value)
98
114
  end
99
115
  define_method("#{name}_text_area") do
100
- platform.text_area_for identifier
116
+ block ? block.call(browser) : platform.text_area_for(identifier)
101
117
  end
102
118
  end
103
119
 
@@ -119,8 +135,9 @@ module PageObject
119
135
  # * :text => Watir only
120
136
  # * :value => Watir only
121
137
  # * :xpath => Watir and Selenium
138
+ # @param optional block to be invoked when element method is called
122
139
  #
123
- def select_list(name, identifier)
140
+ def select_list(name, identifier=nil, &block)
124
141
  define_method(name) do
125
142
  platform.select_list_value_for identifier
126
143
  end
@@ -128,7 +145,7 @@ module PageObject
128
145
  platform.select_list_value_set(identifier, value)
129
146
  end
130
147
  define_method("#{name}_select_list") do
131
- platform.select_list_for identifier
148
+ block ? block.call(browser) : platform.select_list_for(identifier)
132
149
  end
133
150
  end
134
151
 
@@ -152,13 +169,14 @@ module PageObject
152
169
  # * :name => Watir and Selenium
153
170
  # * :text => Watir and Selenium
154
171
  # * :xpath => Watir and Selenium
172
+ # @param optional block to be invoked when element method is called
155
173
  #
156
- def link(name, identifier)
174
+ def link(name, identifier=nil, &block)
157
175
  define_method(name) do
158
176
  platform.click_link_for identifier
159
177
  end
160
178
  define_method("#{name}_link") do
161
- platform.link_for identifier
179
+ block ? block.call(browser) : platform.link_for(identifier)
162
180
  end
163
181
  end
164
182
 
@@ -178,8 +196,9 @@ module PageObject
178
196
  # * :index => Watir only
179
197
  # * :name => Watir and Selenium
180
198
  # * :xpath => Watir and Selenium
199
+ # @param optional block to be invoked when element method is called
181
200
  #
182
- def checkbox(name, identifier)
201
+ def checkbox(name, identifier=nil, &block)
183
202
  define_method("check_#{name}") do
184
203
  platform.check_checkbox(identifier)
185
204
  end
@@ -190,7 +209,7 @@ module PageObject
190
209
  platform.checkbox_checked?(identifier)
191
210
  end
192
211
  define_method("#{name}_checkbox") do
193
- platform.checkbox_for identifier
212
+ block ? block.call(browser) : platform.checkbox_for(identifier)
194
213
  end
195
214
  end
196
215
 
@@ -211,8 +230,9 @@ module PageObject
211
230
  # * :index => Watir only
212
231
  # * :name => Watir and Selenium
213
232
  # * :xpath => Watir and Selenium
233
+ # @param optional block to be invoked when element method is called
214
234
  #
215
- def radio_button(name, identifier)
235
+ def radio_button(name, identifier=nil, &block)
216
236
  define_method("select_#{name}") do
217
237
  platform.select_radio(identifier)
218
238
  end
@@ -223,7 +243,7 @@ module PageObject
223
243
  platform.radio_selected?(identifier)
224
244
  end
225
245
  define_method("#{name}_radio_button") do
226
- platform.radio_button_for identifier
246
+ block ? block.call(browser) : platform.radio_button_for(identifier)
227
247
  end
228
248
  end
229
249
 
@@ -243,13 +263,14 @@ module PageObject
243
263
  # * :name => Watir and Selenium
244
264
  # * :text => Watir only
245
265
  # * :xpath => Watir and Selenium
266
+ # @param optional block to be invoked when element method is called
246
267
  #
247
- def button(name, identifier)
268
+ def button(name, identifier=nil, &block)
248
269
  define_method(name) do
249
270
  platform.click_button_for identifier
250
271
  end
251
272
  define_method("#{name}_button") do
252
- platform.button_for identifier
273
+ block ? block.call(browser) : platform.button_for(identifier)
253
274
  end
254
275
  end
255
276
 
@@ -268,13 +289,14 @@ module PageObject
268
289
  # * :index => Watir only
269
290
  # * :name => Selenium only
270
291
  # * :xpath => Watir and Selenium
292
+ # @param optional block to be invoked when element method is called
271
293
  #
272
- def div(name, identifier)
294
+ def div(name, identifier=nil, &block)
273
295
  define_method(name) do
274
296
  platform.div_text_for identifier
275
297
  end
276
298
  define_method("#{name}_div") do
277
- platform.div_for identifier
299
+ block ? block.call(browser) : platform.div_for(identifier)
278
300
  end
279
301
  end
280
302
 
@@ -293,13 +315,14 @@ module PageObject
293
315
  # * :index => Watir only
294
316
  # * :name => Selenium only
295
317
  # * :xpath => Watir and Selenium
318
+ # @param optional block to be invoked when element method is called
296
319
  #
297
- def span(name, identifier)
320
+ def span(name, identifier=nil, &block)
298
321
  define_method(name) do
299
322
  platform.span_text_for identifier
300
323
  end
301
324
  define_method("#{name}_span") do
302
- platform.span_for identifier
325
+ block ? block.call(browser) : platform.span_for(identifier)
303
326
  end
304
327
  end
305
328
 
@@ -317,10 +340,11 @@ module PageObject
317
340
  # * :index => Watir only
318
341
  # * :name => Selenium only
319
342
  # * :xpath => Watir and Selenium
343
+ # @param optional block to be invoked when element method is called
320
344
  #
321
- def table(name, identifier)
345
+ def table(name, identifier=nil, &block)
322
346
  define_method("#{name}_table") do
323
- platform.table_for identifier
347
+ block ? block.call(browser) : platform.table_for(identifier)
324
348
  end
325
349
  end
326
350
 
@@ -339,13 +363,14 @@ module PageObject
339
363
  # * :index => Watir only
340
364
  # * :name => Selenium only
341
365
  # * :xpath => Watir and Selenium
366
+ # @param optional block to be invoked when element method is called
342
367
  #
343
- def cell(name, identifier)
368
+ def cell(name, identifier=nil, &block)
344
369
  define_method("#{name}") do
345
370
  platform.cell_text_for identifier
346
371
  end
347
372
  define_method("#{name}_cell") do
348
- platform.cell_for identifier
373
+ block ? block.call(browser) : platform.cell_for(identifier)
349
374
  end
350
375
  end
351
376
 
@@ -363,10 +388,11 @@ module PageObject
363
388
  # * :index => Watir only
364
389
  # * :name => Watir and Selenium
365
390
  # * :xpath => Watir and Selenium
391
+ # @param optional block to be invoked when element method is called
366
392
  #
367
- def image(name, identifier)
393
+ def image(name, identifier=nil, &block)
368
394
  define_method("#{name}_image") do
369
- platform.image_for identifier
395
+ block ? block.call(browser) : platform.image_for(identifier)
370
396
  end
371
397
  end
372
398
 
@@ -383,10 +409,11 @@ module PageObject
383
409
  # * :id => Watir and Selenium
384
410
  # * :index => Watir only
385
411
  # * :xpath => Watir and Selenium
412
+ # @param optional block to be invoked when element method is called
386
413
  #
387
- def form(name, identifier)
414
+ def form(name, identifier=nil, &block)
388
415
  define_method("#{name}_form") do
389
- platform.form_for identifier
416
+ block ? block.call(browser) : platform.form_for(identifier)
390
417
  end
391
418
  end
392
419
 
@@ -405,13 +432,14 @@ module PageObject
405
432
  # * :index => Watir only
406
433
  # * :name => Selenium only
407
434
  # * :xpath => Watir and Selenium
435
+ # @param optional block to be invoked when element method is called
408
436
  #
409
- def list_item(name, identifier)
437
+ def list_item(name, identifier=nil, &block)
410
438
  define_method(name) do
411
439
  platform.list_item_text_for identifier
412
440
  end
413
441
  define_method("#{name}_list_item") do
414
- platform.list_item_for identifier
442
+ block ? block.call(browser) : platform.list_item_for(identifier)
415
443
  end
416
444
  end
417
445
 
@@ -429,10 +457,11 @@ module PageObject
429
457
  # * :index => Watir only
430
458
  # * :name => Selenium only
431
459
  # * :xpath => Watir and Selenium
460
+ # @param optional block to be invoked when element method is called
432
461
  #
433
- def unordered_list(name, identifier)
462
+ def unordered_list(name, identifier=nil, &block)
434
463
  define_method("#{name}_unordered_list") do
435
- platform.unordered_list_for identifier
464
+ block ? block.call(browser) : platform.unordered_list_for(identifier)
436
465
  end
437
466
  end
438
467
 
@@ -450,10 +479,11 @@ module PageObject
450
479
  # * :index => Watir only
451
480
  # * :name => Selenium only
452
481
  # * :xpath => Watir and Selenium
482
+ # @param optional block to be invoked when element method is called
453
483
  #
454
- def ordered_list(name, identifier)
484
+ def ordered_list(name, identifier=nil, &block)
455
485
  define_method("#{name}_ordered_list") do
456
- platform.ordered_list_for identifier
486
+ block ? block.call(browser) : platform.ordered_list_for(identifier)
457
487
  end
458
488
  end
459
489
  end
@@ -1,14 +1,30 @@
1
1
  module PageObject
2
2
  module Elements
3
3
  class OrderedList < Element
4
+ include Enumerable
4
5
 
5
6
  def initialize(element, platform)
6
7
  @element = element
7
8
  include_platform_for platform
8
9
  end
9
10
 
11
+ #
12
+ # iterator that yields with a PageObject::Elements::ListItem
13
+ #
14
+ # @return [PageObject::Elements::ListItem]
15
+ #
16
+ def each
17
+ for index in 1..self.items do
18
+ yield self[index-1]
19
+ end
20
+ end
21
+
10
22
  protected
11
23
 
24
+ def child_xpath
25
+ ".//child::li"
26
+ end
27
+
12
28
  def self.watir_finders
13
29
  [:class, :id, :index, :xpath]
14
30
  end