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.
@@ -1,14 +1,30 @@
1
1
  module PageObject
2
2
  module Elements
3
3
  class Table < 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::TableRow
13
+ #
14
+ # @return [PageObject::Elements::TableRow]
15
+ #
16
+ def each
17
+ for index in 1..self.rows do
18
+ yield self[index-1]
19
+ end
20
+ end
21
+
10
22
  protected
11
23
 
24
+ def child_xpath
25
+ ".//child::tr"
26
+ end
27
+
12
28
  def include_platform_for platform
13
29
  super
14
30
  if platform[:platform] == :watir
@@ -1,14 +1,30 @@
1
1
  module PageObject
2
2
  module Elements
3
3
  class TableRow < 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::TableCell
13
+ #
14
+ # @return [PageObject::Elements::TableCell]
15
+ #
16
+ def each
17
+ for index in 1..self.columns do
18
+ yield self[index-1]
19
+ end
20
+ end
21
+
10
22
  protected
11
23
 
24
+ def child_xpath
25
+ ".//child::td|th"
26
+ end
27
+
12
28
  def include_platform_for platform
13
29
  super
14
30
  if platform[:platform] == :watir
@@ -1,14 +1,31 @@
1
1
  module PageObject
2
2
  module Elements
3
3
  class UnorderedList < 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
+
22
+
10
23
  protected
11
24
 
25
+ def child_xpath
26
+ ".//child::li"
27
+ end
28
+
12
29
  def self.watir_finders
13
30
  [:class, :id, :index, :xpath]
14
31
  end
@@ -0,0 +1,50 @@
1
+ module PageObject
2
+ #
3
+ # Module to facilitate to creating of page objects in step definitions. You
4
+ # can make the methods below available to all of your step definitions by adding
5
+ # this module to World. This idea was first discussed in Alister Scott's blog
6
+ # entry http://watirmelon.com/2011/06/07/removing-local-page-references-from-cucumber-steps/.
7
+ #
8
+ # @example Making the PageFactory available to your step definitions
9
+ # World PageObject::PageFactory
10
+ #
11
+ # @example Visiting a page for the first time in a Scenario
12
+ # visit_page MyPageObject do |page|
13
+ # page.name = 'Cheezy'
14
+ # end
15
+ #
16
+ # @example using a page that has already been visited in a Scenario
17
+ # on_page MyPageObject do |page|
18
+ # page.name.should == 'Cheezy'
19
+ # end
20
+ #
21
+ module PageFactory
22
+
23
+ #
24
+ # Create and navigate to a page object. The navigation will only work if the
25
+ # 'page_url' method was call on the page object.
26
+ #
27
+ # @param [PageObject] a class that has included the PageObject module
28
+ # @param an optional block to be called
29
+ # @return [PageObject] the newly created page object
30
+ #
31
+ def visit_page(page_class, &block)
32
+ on_page page_class, true, &block
33
+ end
34
+
35
+ #
36
+ # Create a page object.
37
+ #
38
+ # @param [PageObject] a class that has included the PageObject module
39
+ # @param [Bool] should the page be visited? default is false.
40
+ # @param an optional block to be called
41
+ # @return [PageObject] the newly created page object
42
+ #
43
+ def on_page(page_class, visit=false, &block)
44
+ page = page_class.new(@browser, visit)
45
+ block.call page if block
46
+ page
47
+ end
48
+
49
+ end
50
+ end
@@ -9,8 +9,21 @@ module PageObject
9
9
  # @return [PageObject::Elements::ListItem]
10
10
  #
11
11
  def [](idx)
12
- element = @element.find_element(:xpath, ".//li[#{idx+1}]")
13
- PageObject::Elements::ListItem.new(element, :platform => :selenium)
12
+ eles = list_items
13
+ PageObject::Elements::ListItem.new(eles[idx], :platform => :selenium)
14
+ end
15
+
16
+ #
17
+ # Return the number of items contained in the ordered list
18
+ #
19
+ def items
20
+ list_items.size
21
+ end
22
+
23
+ private
24
+
25
+ def list_items
26
+ element.find_elements(:xpath, child_xpath)
14
27
  end
15
28
 
16
29
  end
@@ -6,11 +6,20 @@ module PageObject
6
6
  # Return the PageObject::Elements::TableRow for the index provided. Index
7
7
  # is zero based.
8
8
  #
9
+ # @return [PageObject::Elements::TableRow]
10
+ #
9
11
  def [](idx)
10
- element = @element.find_element(:xpath, ".//tr[#{idx+1}]")
11
- PageObject::Elements::TableRow.new(element, :platform => :selenium)
12
+ eles = table_rows
13
+ PageObject::Elements::TableRow.new(eles[idx], :platform => :selenium)
12
14
  end
13
15
 
16
+ #
17
+ # Returns the number of rows in the table.
18
+ #
19
+ def rows
20
+ table_rows.size
21
+ end
22
+
14
23
  #
15
24
  # override PageObject::Platforms::SeleniumElement because exists? is not
16
25
  # available on a table element in Selenium.
@@ -19,6 +28,11 @@ module PageObject
19
28
  raise "exists? not available on table element"
20
29
  end
21
30
 
31
+ private
32
+
33
+ def table_rows
34
+ @element.find_elements(:xpath, child_xpath)
35
+ end
22
36
  end
23
37
  end
24
38
  end
@@ -7,8 +7,21 @@ module PageObject
7
7
  # is zero based.
8
8
  #
9
9
  def [](idx)
10
- element = @element.find_element(:xpath, "./th|td[#{idx+1}]")
11
- PageObject::Elements::TableCell.new(element, :platform => :selenium)
10
+ els = table_cells
11
+ PageObject::Elements::TableCell.new(els[idx], :platform => :selenium)
12
+ end
13
+
14
+ #
15
+ # Returns the number of columns in the table.
16
+ #
17
+ def columns
18
+ table_cells.size
19
+ end
20
+
21
+ private
22
+
23
+ def table_cells
24
+ element.find_elements(:xpath, child_xpath)
12
25
  end
13
26
 
14
27
  end
@@ -9,8 +9,21 @@ module PageObject
9
9
  # @return [PageObject::Elements::ListItem]
10
10
  #
11
11
  def [](idx)
12
- element = @element.find_element(:xpath, ".//li[#{idx+1}]")
13
- PageObject::Elements::ListItem.new(element, :platform => :selenium)
12
+ eles = list_items
13
+ PageObject::Elements::ListItem.new(eles[idx], :platform => :selenium)
14
+ end
15
+
16
+ #
17
+ # Return the number of items contained in the unordered list
18
+ #
19
+ def items
20
+ list_items.size
21
+ end
22
+
23
+ private
24
+
25
+ def list_items
26
+ element.find_elements(:xpath, child_xpath)
14
27
  end
15
28
 
16
29
  end
@@ -9,10 +9,22 @@ module PageObject
9
9
  # @return [PageObject::Elements::ListItem]
10
10
  #
11
11
  def [](idx)
12
- element = @element.wd.find_element(:xpath, ".//li[#{idx+1}]")
13
- PageObject::Elements::ListItem.new(element, :platform => :watir)
12
+ eles = list_items
13
+ PageObject::Elements::ListItem.new(eles[idx], :platform => :watir)
14
14
  end
15
15
 
16
+ #
17
+ # Return the number of items contained in the ordered list
18
+ #
19
+ def items
20
+ list_items.size
21
+ end
22
+
23
+ private
24
+
25
+ def list_items
26
+ element.wd.find_elements(:xpath, child_xpath)
27
+ end
16
28
  end
17
29
  end
18
30
  end
@@ -6,10 +6,19 @@ module PageObject
6
6
  # Return the PageObject::Elements::TableRow for the index provided. Index
7
7
  # is zero based.
8
8
  #
9
+ # @return [PageObject::Elements::TableRow]
10
+ #
9
11
  def [](idx)
10
12
  PageObject::Elements::TableRow.new(@element[idx], :platform => :watir)
11
13
  end
12
14
 
15
+ #
16
+ # Returns the number of rows in the table.
17
+ #
18
+ def rows
19
+ element.wd.find_elements(:xpath, child_xpath).size
20
+ end
21
+
13
22
  end
14
23
  end
15
24
  end
@@ -7,7 +7,14 @@ module PageObject
7
7
  # is zero based.
8
8
  #
9
9
  def [](idx)
10
- PageObject::Elements::TableCell.new(@element[idx], :platform => :watir)
10
+ PageObject::Elements::TableCell.new(element[idx], :platform => :watir)
11
+ end
12
+
13
+ #
14
+ # Returns the number of columns in the table.
15
+ #
16
+ def columns
17
+ element.wd.find_elements(:xpath, child_xpath).size
11
18
  end
12
19
 
13
20
  end
@@ -9,8 +9,21 @@ module PageObject
9
9
  # @return [PageObject::Elements::ListItem]
10
10
  #
11
11
  def [](idx)
12
- element = @element.wd.find_element(:xpath, ".//li[#{idx+1}]")
13
- PageObject::Elements::ListItem.new(element, :platform => :watir)
12
+ eles = list_items
13
+ PageObject::Elements::ListItem.new(eles[idx], :platform => :watir)
14
+ end
15
+
16
+ #
17
+ # Return the number of items contained in the unordered list
18
+ #
19
+ def items
20
+ list_items.size
21
+ end
22
+
23
+ private
24
+
25
+ def list_items
26
+ element.wd.find_elements(:xpath, child_xpath)
14
27
  end
15
28
 
16
29
  end
@@ -1,4 +1,4 @@
1
1
  module PageObject
2
2
  # @private
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  class TestPageObject
4
4
  include PageObject
5
5
 
6
+ page_url "http://apple.com"
6
7
  link(:google_search, :link => 'Google Search')
7
8
  text_field(:first_name, :id => 'first_name')
8
9
  hidden_field(:social_security_number, :id => 'ssn')
@@ -22,11 +23,53 @@ class TestPageObject
22
23
  ordered_list(:top_five, :id => 'top')
23
24
  end
24
25
 
26
+ class BlockPageObject
27
+ include PageObject
28
+
29
+ attr_reader :value
30
+
31
+ text_field :first_name do |browser| "text_field" end
32
+ hidden_field :secret do |browser| "hidden_field" end
33
+ text_area :address do |browser| "text_area" end
34
+ select_list :state do |browser| "select_list" end
35
+ link :continue do |browser| "link" end
36
+ checkbox :active do |browser| "checkbox" end
37
+ radio_button :first do |browser| "radio_button" end
38
+ button :click_me do |browser| "button" end
39
+ div :footer do |browser| "div" end
40
+ span :alert do |browser| "span" end
41
+ table :cart do |browser| "table" end
42
+ cell :total do |browser| "cell" end
43
+ image :logo do |browser| "image" end
44
+ form :login do |browser| "form" end
45
+ list_item :item_one do |browser| "list_item" end
46
+ unordered_list :menu do |browser| "unordered_list" end
47
+ ordered_list :top_five do |browser| "ordered_list" end
48
+ end
49
+
25
50
  describe PageObject::Accessors do
26
51
  let(:watir_browser) { mock_watir_browser }
27
52
  let(:selenium_browser) { mock_selenium_browser }
28
53
  let(:watir_page_object) { TestPageObject.new(watir_browser) }
29
54
  let(:selenium_page_object) { TestPageObject.new(selenium_browser) }
55
+ let(:block_page_object) { BlockPageObject.new(watir_browser) }
56
+
57
+ describe "goto a page" do
58
+ it "should navigate to a page when requested" do
59
+ watir_browser.should_receive(:goto)
60
+ page = TestPageObject.new(watir_browser, true)
61
+ end
62
+
63
+ it "should not navigate to a page when not requested" do
64
+ watir_browser.should_not_receive(:goto)
65
+ page = TestPageObject.new(watir_browser)
66
+ end
67
+
68
+ it "should not navigate to a page when 'page_url' not specified" do
69
+ watir_browser.should_not_receive(:goto)
70
+ page = BlockPageObject.new(watir_browser, true)
71
+ end
72
+ end
30
73
 
31
74
  describe "link accessors" do
32
75
  context "when called on a page object" do
@@ -34,6 +77,10 @@ describe PageObject::Accessors do
34
77
  watir_page_object.should respond_to(:google_search)
35
78
  watir_page_object.should respond_to(:google_search_link)
36
79
  end
80
+
81
+ it "should call a block on the element method when present" do
82
+ block_page_object.continue_link.should == "link"
83
+ end
37
84
  end
38
85
 
39
86
  context "Watir implementation" do
@@ -71,6 +118,10 @@ describe PageObject::Accessors do
71
118
  watir_page_object.should respond_to(:first_name=)
72
119
  watir_page_object.should respond_to(:first_name_text_field)
73
120
  end
121
+
122
+ it "should call a block on the element method when present" do
123
+ block_page_object.first_name_text_field.should == "text_field"
124
+ end
74
125
  end
75
126
 
76
127
  context "Watir implementation" do
@@ -121,6 +172,10 @@ describe PageObject::Accessors do
121
172
  watir_page_object.should respond_to(:social_security_number)
122
173
  watir_page_object.should respond_to(:social_security_number_hidden_field)
123
174
  end
175
+
176
+ it "should call a block on the element method when present" do
177
+ block_page_object.secret_hidden_field.should == "hidden_field"
178
+ end
124
179
  end
125
180
 
126
181
  context "watir implementation" do
@@ -159,6 +214,10 @@ describe PageObject::Accessors do
159
214
  watir_page_object.should respond_to(:address=)
160
215
  watir_page_object.should respond_to(:address_text_area)
161
216
  end
217
+
218
+ it "should call a block on the element method when present" do
219
+ block_page_object.address_text_area.should == "text_area"
220
+ end
162
221
  end
163
222
 
164
223
  context "watir implementation" do
@@ -209,6 +268,10 @@ describe PageObject::Accessors do
209
268
  watir_page_object.should respond_to :state=
210
269
  watir_page_object.should respond_to(:state_select_list)
211
270
  end
271
+
272
+ it "should call a block on the element method when present" do
273
+ block_page_object.state_select_list.should == "select_list"
274
+ end
212
275
  end
213
276
 
214
277
  context "Watir implementation" do
@@ -261,6 +324,10 @@ describe PageObject::Accessors do
261
324
  watir_page_object.should respond_to :active_checked?
262
325
  watir_page_object.should respond_to(:active_checkbox)
263
326
  end
327
+
328
+ it "should call a block on the element method when present" do
329
+ block_page_object.active_checkbox.should == "checkbox"
330
+ end
264
331
  end
265
332
 
266
333
  context "Watir implementation" do
@@ -327,6 +394,10 @@ describe PageObject::Accessors do
327
394
  watir_page_object.should respond_to :first_selected?
328
395
  watir_page_object.should respond_to(:first_radio_button)
329
396
  end
397
+
398
+ it "should call a block on the element method when present" do
399
+ block_page_object.first_radio_button.should == "radio_button"
400
+ end
330
401
  end
331
402
 
332
403
  context "Watir implementation" do
@@ -390,6 +461,10 @@ describe PageObject::Accessors do
390
461
  watir_page_object.should respond_to :click_me
391
462
  watir_page_object.should respond_to :click_me_button
392
463
  end
464
+
465
+ it "should call a block on the element method when present" do
466
+ block_page_object.click_me_button.should == "button"
467
+ end
393
468
  end
394
469
 
395
470
  context "watir implementation" do
@@ -428,6 +503,10 @@ describe PageObject::Accessors do
428
503
  watir_page_object.should respond_to(:message)
429
504
  watir_page_object.should respond_to(:message_div)
430
505
  end
506
+
507
+ it "should call a block on the element method when present" do
508
+ block_page_object.footer_div.should == "div"
509
+ end
431
510
  end
432
511
 
433
512
  context "watir implementation" do
@@ -467,6 +546,10 @@ describe PageObject::Accessors do
467
546
  watir_page_object.should respond_to(:alert)
468
547
  watir_page_object.should respond_to(:alert_span)
469
548
  end
549
+
550
+ it "should call a block on the element method when present" do
551
+ block_page_object.alert_span.should == "span"
552
+ end
470
553
  end
471
554
 
472
555
  context "watir implementation" do
@@ -504,6 +587,10 @@ describe PageObject::Accessors do
504
587
  it "should generate accessor methods" do
505
588
  watir_page_object.should respond_to(:cart_table)
506
589
  end
590
+
591
+ it "should call a block on the element method when present" do
592
+ block_page_object.cart_table.should == "table"
593
+ end
507
594
  end
508
595
 
509
596
  context "watir implementation" do
@@ -529,6 +616,10 @@ describe PageObject::Accessors do
529
616
  watir_page_object.should respond_to(:total)
530
617
  watir_page_object.should respond_to(:total_cell)
531
618
  end
619
+
620
+ it "should call a block on the element method when present" do
621
+ block_page_object.total_cell.should == "cell"
622
+ end
532
623
  end
533
624
 
534
625
  context "watir implementation" do
@@ -559,6 +650,10 @@ describe PageObject::Accessors do
559
650
  it "should generate accessor methods" do
560
651
  watir_page_object.should respond_to(:logo_image)
561
652
  end
653
+
654
+ it "should call a block on the element method when present" do
655
+ block_page_object.logo_image.should == "image"
656
+ end
562
657
  end
563
658
 
564
659
  context "watir implementation" do
@@ -583,6 +678,10 @@ describe PageObject::Accessors do
583
678
  it "should generate accessor methods" do
584
679
  watir_page_object.should respond_to(:login_form)
585
680
  end
681
+
682
+ it "should call a block on the element method when present" do
683
+ block_page_object.login_form.should == "form"
684
+ end
586
685
  end
587
686
 
588
687
  context "watir implementation" do
@@ -608,6 +707,10 @@ describe PageObject::Accessors do
608
707
  watir_page_object.should respond_to(:item_one)
609
708
  watir_page_object.should respond_to(:item_one_list_item)
610
709
  end
710
+
711
+ it "should call a block on the element method when present" do
712
+ block_page_object.item_one_list_item.should == "list_item"
713
+ end
611
714
  end
612
715
 
613
716
  context "watir implementation" do
@@ -644,6 +747,10 @@ describe PageObject::Accessors do
644
747
  it "should generate accessor methods" do
645
748
  watir_page_object.should respond_to(:menu_unordered_list)
646
749
  end
750
+
751
+ it "should call a block on the element method when present" do
752
+ block_page_object.menu_unordered_list.should == "unordered_list"
753
+ end
647
754
  end
648
755
 
649
756
  context "watir implementation" do
@@ -668,6 +775,10 @@ describe PageObject::Accessors do
668
775
  it "should generate accessor methods" do
669
776
  watir_page_object.should respond_to(:top_five_ordered_list)
670
777
  end
778
+
779
+ it "should call a block on the element method when present" do
780
+ block_page_object.top_five_ordered_list.should == "ordered_list"
781
+ end
671
782
  end
672
783
 
673
784
  context "watir implementation" do