page-object 0.6.3 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. data/ChangeLog +15 -0
  2. data/features/button.feature +1 -0
  3. data/features/element.feature +60 -56
  4. data/features/frames.feature +1 -0
  5. data/features/generic_elements.feature +24 -0
  6. data/features/html/multi_elements.html +4 -0
  7. data/features/html/nested_elements.html +4 -1
  8. data/features/html/static_elements.html +15 -1
  9. data/features/label.feature +44 -0
  10. data/features/link.feature +1 -0
  11. data/features/sample-app/public/prototype.html +1 -0
  12. data/features/step_definitions/element_steps.rb +22 -6
  13. data/features/step_definitions/frames_steps.rb +4 -0
  14. data/features/step_definitions/generic_element_steps.rb +19 -0
  15. data/features/step_definitions/label_steps.rb +19 -0
  16. data/features/support/page.rb +17 -0
  17. data/lib/page-object.rb +7 -0
  18. data/lib/page-object/accessors.rb +73 -1
  19. data/lib/page-object/element_locators.rb +36 -0
  20. data/lib/page-object/elements.rb +2 -1
  21. data/lib/page-object/elements/button.rb +2 -2
  22. data/lib/page-object/elements/element.rb +6 -6
  23. data/lib/page-object/elements/label.rb +19 -0
  24. data/lib/page-object/elements/link.rb +2 -2
  25. data/lib/page-object/elements/text_field.rb +1 -1
  26. data/lib/page-object/nested_elements.rb +8 -0
  27. data/lib/page-object/page_factory.rb +6 -0
  28. data/lib/page-object/platforms/selenium_webdriver/element.rb +17 -17
  29. data/lib/page-object/platforms/selenium_webdriver/form.rb +2 -2
  30. data/lib/page-object/platforms/selenium_webdriver/image.rb +2 -2
  31. data/lib/page-object/platforms/selenium_webdriver/page_object.rb +54 -1
  32. data/lib/page-object/platforms/selenium_webdriver/select_list.rb +2 -2
  33. data/lib/page-object/platforms/selenium_webdriver/table.rb +2 -2
  34. data/lib/page-object/platforms/watir_webdriver/element.rb +14 -14
  35. data/lib/page-object/platforms/watir_webdriver/form.rb +2 -2
  36. data/lib/page-object/platforms/watir_webdriver/image.rb +2 -2
  37. data/lib/page-object/platforms/watir_webdriver/page_object.rb +72 -9
  38. data/lib/page-object/platforms/watir_webdriver/select_list.rb +5 -5
  39. data/lib/page-object/platforms/watir_webdriver/table.rb +2 -2
  40. data/lib/page-object/version.rb +1 -1
  41. data/page-object.gemspec +1 -1
  42. data/spec/page-object/accessors_spec.rb +15 -3
  43. data/spec/page-object/element_locators_spec.rb +24 -0
  44. data/spec/page-object/elements/button_spec.rb +2 -2
  45. data/spec/page-object/elements/label_spec.rb +29 -0
  46. data/spec/page-object/elements/link_spec.rb +2 -2
  47. data/spec/page-object/page-object_spec.rb +7 -1
  48. data/spec/page-object/page_factory_spec.rb +26 -0
  49. data/spec/page-object/platforms/watir_webdriver_spec.rb +1 -0
  50. metadata +26 -15
@@ -7,14 +7,14 @@ module PageObject
7
7
  # Return the width of the image.
8
8
  #
9
9
  def width
10
- @element.width
10
+ element.width
11
11
  end
12
12
 
13
13
  #
14
14
  # Return the height of the image
15
15
  #
16
16
  def height
17
- @element.height
17
+ element.height
18
18
  end
19
19
  end
20
20
  end
@@ -110,7 +110,7 @@ module PageObject
110
110
  # See PageObject#execute_script
111
111
  #
112
112
  def execute_script(script)
113
- @browser.wd.execute_script(script)
113
+ @browser.execute_script(script)
114
114
  end
115
115
 
116
116
  #
@@ -122,6 +122,13 @@ module PageObject
122
122
  @browser.window(win_id).use &block
123
123
  end
124
124
 
125
+ def element_with_focus
126
+ element = browser.execute_script("return document.activeElement")
127
+ type = element.type.to_sym if element.tag_name.to_sym == :input
128
+ cls = ::PageObject::Elements.element_class_for(element.tag_name, type)
129
+ cls.new(element, :platform => :watir_webdriver)
130
+ end
131
+
125
132
  #
126
133
  # platform method to switch to a frame and execute a block
127
134
  # See PageObject#in_frame
@@ -130,7 +137,6 @@ module PageObject
130
137
  frame = [] if frame.nil?
131
138
  frame << identifier
132
139
  block.call(frame)
133
-
134
140
  end
135
141
 
136
142
  #
@@ -297,8 +303,8 @@ module PageObject
297
303
  # See PageObject::Accessors#link
298
304
  #
299
305
  def click_link_for(identifier)
300
- process_watir_call("link(identifier).click if identifier", Elements::Link,
301
- identifier)
306
+ call = call_for_watir_element(identifier, "link(identifier)")
307
+ process_watir_call("#{call}.click if identifier", Elements::Link, identifier)
302
308
  end
303
309
 
304
310
  #
@@ -306,14 +312,16 @@ module PageObject
306
312
  # see PageObject::Accessors#link
307
313
  #
308
314
  def link_for(identifier)
309
- find_watir_element("link(identifier)", Elements::Link, identifier)
315
+ call = call_for_watir_element(identifier, "link(identifier)")
316
+ find_watir_element(call, Elements::Link, identifier)
310
317
  end
311
318
 
312
319
  #
313
320
  # platform method to retrieve an array of link elements
314
321
  #
315
322
  def links_for(identifier)
316
- find_watir_elements("links(identifier)", Elements::Link, identifier)
323
+ call = call_for_watir_elements(identifier, "links(identifier)")
324
+ find_watir_elements(call, Elements::Link, identifier)
317
325
  end
318
326
 
319
327
  #
@@ -445,7 +453,8 @@ module PageObject
445
453
  # See PageObject::Accessors#button
446
454
  #
447
455
  def click_button_for(identifier)
448
- process_watir_call("button(identifier).click", Elements::Button, identifier)
456
+ call = call_for_watir_element(identifier, "button(identifier)")
457
+ process_watir_call("#{call}.click", Elements::Button, identifier)
449
458
  end
450
459
 
451
460
  #
@@ -453,14 +462,16 @@ module PageObject
453
462
  # See PageObject::Accessors#button
454
463
  #
455
464
  def button_for(identifier)
456
- find_watir_element("button(identifier)", Elements::Button, identifier)
465
+ call = call_for_watir_element(identifier, "button(identifier)")
466
+ find_watir_element(call, Elements::Button, identifier)
457
467
  end
458
468
 
459
469
  #
460
470
  # platform method to retrieve an array of button elements
461
471
  #
462
472
  def buttons_for(identifier)
463
- find_watir_elements("buttons(identifier)", Elements::Button, identifier)
473
+ call = call_for_watir_elements(identifier, "buttons(identifier)")
474
+ find_watir_elements(call, Elements::Button, identifier)
464
475
  end
465
476
 
466
477
  #
@@ -746,6 +757,30 @@ module PageObject
746
757
  find_watir_elements("ps(identifier)", Elements::Paragraph, identifier, 'p')
747
758
  end
748
759
 
760
+ #
761
+ # platform method to return the text for a label
762
+ # See PageObject::Accessors#label
763
+ #
764
+ def label_text_for(identifier)
765
+ process_watir_call("label(identifier).text", Elements::Label, identifier, nil, 'label')
766
+ end
767
+
768
+ #
769
+ # platform method to return a PageObject::Elements::Label element
770
+ # See PageObject::Accessors#label
771
+ #
772
+ def label_for(identifier)
773
+ find_watir_element("label(identifier)", Elements::Label, identifier, 'label')
774
+ end
775
+
776
+ #
777
+ #
778
+ # platform method to retrieve an array of label elements
779
+ #
780
+ def labels_for(identifier)
781
+ find_watir_elements("labels(identifier)", Elements::Label, identifier, 'label')
782
+ end
783
+
749
784
  #
750
785
  # platform method to set the file on a file_field element
751
786
  # See PageObject::Accessors#file_field
@@ -763,6 +798,18 @@ module PageObject
763
798
  find_watir_element("file_field(identifier)", Elements::FileField, identifier)
764
799
  end
765
800
 
801
+ #
802
+ # platform method to return a PageObject::Elements::Element element
803
+ # See PageObject::Accessors#elem
804
+ #
805
+ def element_for(tag, identifier)
806
+ find_watir_element("#{tag.to_s}(identifier)", Elements::Element, identifier, tag.to_s)
807
+ end
808
+
809
+ def elements_for(tag, identifier)
810
+ find_watir_elements("#{tag.to_s}s(identifier)", Elements::Element, identifier, tag.to_s)
811
+ end
812
+
766
813
  private
767
814
 
768
815
  def find_watir_elements(the_call, type, identifier, tag_name=nil)
@@ -814,6 +861,22 @@ module PageObject
814
861
  @browser.wd.switch_to.default_content unless frame_identifiers.nil?
815
862
  end
816
863
 
864
+ def css_element
865
+ "element(identifier)"
866
+ end
867
+
868
+ def css_elements
869
+ "elements(identifier)"
870
+ end
871
+
872
+ def call_for_watir_element(identifier, call)
873
+ identifier[:css] ? "#{css_element}" : call
874
+ end
875
+
876
+ def call_for_watir_elements(identifier, call)
877
+ identifier[:css] ? "#{css_elements}" : call
878
+ end
879
+
817
880
  def switch_to_frame(frame_identifiers)
818
881
  unless frame_identifiers.nil?
819
882
  frame_identifiers.each do |frame_id|
@@ -17,7 +17,7 @@ module PageObject
17
17
  # Select a value from the list
18
18
  #
19
19
  def select(value)
20
- @element.select(value)
20
+ element.select(value)
21
21
  end
22
22
 
23
23
  #
@@ -27,7 +27,7 @@ module PageObject
27
27
  #
28
28
  def options
29
29
  elements = []
30
- options = @element.wd.find_elements(:xpath, child_xpath)
30
+ options = element.wd.find_elements(:xpath, child_xpath)
31
31
  options.each do |opt|
32
32
  elements << Object::PageObject::Elements::Option.new(opt, :platform => :watir_webdriver)
33
33
  end
@@ -38,7 +38,7 @@ module PageObject
38
38
  # @return [Array<String>] An array of strings representing the text value of the currently selected options.
39
39
  #
40
40
  def selected_options
41
- @element.selected_options.map { |e| e.text }.compact
41
+ element.selected_options.map { |e| e.text }.compact
42
42
  end
43
43
 
44
44
  #
@@ -47,7 +47,7 @@ module PageObject
47
47
  # @param [String, Regexp] value A value.
48
48
  # @return [Boolean]
49
49
  def include?(value)
50
- @element.include? value
50
+ element.include? value
51
51
  end
52
52
 
53
53
  #
@@ -56,7 +56,7 @@ module PageObject
56
56
  # @param [String, Regexp] value A value.
57
57
  # @return [Boolean]
58
58
  def selected?(value)
59
- @element.selected? value
59
+ element.selected? value
60
60
  end
61
61
  end
62
62
  end
@@ -11,7 +11,7 @@ module PageObject
11
11
  # @return [PageObject::Elements::TableRow]
12
12
  #
13
13
  def [](idx)
14
- Object::PageObject::Elements::TableRow.new(@element[idx], :platform => :watir_webdriver)
14
+ Object::PageObject::Elements::TableRow.new(element[idx], :platform => :watir_webdriver)
15
15
  end
16
16
 
17
17
  #
@@ -24,4 +24,4 @@ module PageObject
24
24
  end
25
25
  end
26
26
  end
27
- end
27
+ end
@@ -1,4 +1,4 @@
1
1
  module PageObject
2
2
  # @private
3
- VERSION = "0.6.3"
3
+ VERSION = "0.6.4"
4
4
  end
data/page-object.gemspec CHANGED
@@ -19,7 +19,7 @@ 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.5.3'
22
+ s.add_dependency 'watir-webdriver', '>= 0.5.4'
23
23
  s.add_dependency 'selenium-webdriver', '>= 2.20.0'
24
24
 
25
25
  s.add_development_dependency 'rspec', '>= 2.6.0'
@@ -130,17 +130,29 @@ describe PageObject::Accessors do
130
130
  describe "goto a page" do
131
131
  it "should navigate to a page when requested" do
132
132
  watir_browser.should_receive(:goto)
133
- page = AccessorsTestPageObject.new(watir_browser, true)
133
+ AccessorsTestPageObject.new(watir_browser, true)
134
+ end
135
+
136
+ it "should call a method when page_url called with a symbol" do
137
+ class SymbolPageUrl
138
+ include PageObject
139
+ page_url :custom_url
140
+ def custom_url
141
+ "custom"
142
+ end
143
+ end
144
+ watir_browser.should_receive(:goto).with('custom')
145
+ SymbolPageUrl.new(watir_browser, true)
134
146
  end
135
147
 
136
148
  it "should not navigate to a page when not requested" do
137
149
  watir_browser.should_not_receive(:goto)
138
- page = AccessorsTestPageObject.new(watir_browser)
150
+ AccessorsTestPageObject.new(watir_browser)
139
151
  end
140
152
 
141
153
  it "should not navigate to a page when 'page_url' not specified" do
142
154
  watir_browser.should_not_receive(:goto)
143
- page = BlockPageObject.new(watir_browser, true)
155
+ BlockPageObject.new(watir_browser, true)
144
156
  end
145
157
  end
146
158
 
@@ -299,6 +299,18 @@ describe PageObject::ElementLocators do
299
299
  elements[0].should be_instance_of PageObject::Elements::Paragraph
300
300
  end
301
301
 
302
+ it "should find a label" do
303
+ watir_browser.should_receive(:label).with(:id => 'blah').and_return(watir_browser)
304
+ element = watir_page_object.label_element(:id => 'blah')
305
+ element.should be_instance_of PageObject::Elements::Label
306
+ end
307
+
308
+ it "should find all label elements" do
309
+ watir_browser.should_receive(:labels).with(:id => 'blah').and_return([watir_browser])
310
+ elements = watir_page_object.label_elements(:id => 'blah')
311
+ elements[0].should be_instance_of PageObject::Elements::Label
312
+ end
313
+
302
314
  it "should find a file field element" do
303
315
  watir_browser.should_receive(:file_field).with(:id => 'blah').and_return(watir_browser)
304
316
  element = watir_page_object.file_field_element(:id => 'blah')
@@ -599,6 +611,18 @@ describe PageObject::ElementLocators do
599
611
  elements[0].should be_instance_of PageObject::Elements::Paragraph
600
612
  end
601
613
 
614
+ it "should find a label" do
615
+ selenium_browser.should_receive(:find_element).with(:id, 'blah').and_return(selenium_browser)
616
+ element = selenium_page_object.label_element(:id => 'blah')
617
+ element.should be_instance_of PageObject::Elements::Label
618
+ end
619
+
620
+ it "should find all label elements" do
621
+ selenium_browser.should_receive(:find_elements).with(:id, 'blah').and_return([selenium_browser])
622
+ elements = selenium_page_object.label_elements(:id => 'blah')
623
+ elements[0].should be_instance_of PageObject::Elements::Label
624
+ end
625
+
602
626
  it "should find a file field element" do
603
627
  selenium_browser.should_receive(:find_element).with(:id, 'blah').and_return(selenium_browser)
604
628
  element = selenium_page_object.file_field_element(:id => 'blah')
@@ -6,14 +6,14 @@ describe PageObject::Elements::Button do
6
6
 
7
7
  context "when mapping how to find an element" do
8
8
  it "should map watir types to same" do
9
- [:class, :id, :index, :name, :value, :xpath, :src, :alt].each do |t|
9
+ [:class, :id, :index, :name, :value, :xpath, :src, :alt, :css].each do |t|
10
10
  identifier = button.watir_identifier_for t => 'value'
11
11
  identifier.keys.first.should == t
12
12
  end
13
13
  end
14
14
 
15
15
  it "should map selenium types to same" do
16
- [:class, :id, :index, :name, :value, :xpath, :src, :alt].each do |t|
16
+ [:class, :id, :index, :name, :value, :xpath, :src, :alt, :css].each do |t|
17
17
  key, value = button.selenium_identifier_for t => 'value'
18
18
  key.should == t
19
19
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'page-object/elements'
3
+
4
+ describe PageObject::Elements::Label do
5
+ let(:label) { PageObject::Elements::Label }
6
+
7
+ describe "when mapping how to find an element" do
8
+ it "should map watir types to same" do
9
+ [:class, :id, :text, :index, :xpath].each do |t|
10
+ identifier = label.watir_identifier_for t => 'value'
11
+ identifier.keys.first.should == t
12
+ end
13
+ end
14
+
15
+ it "should map selenium types to same" do
16
+ [:class, :id, :text, :index, :xpath].each do |t|
17
+ key, value = label.selenium_identifier_for t => 'value'
18
+ key.should == t
19
+ end
20
+ end
21
+
22
+ describe "interface" do
23
+ it "should register with tag :label" do
24
+ ::PageObject::Elements.element_class_for(:label).should == ::PageObject::Elements::Label
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -6,7 +6,7 @@ describe PageObject::Elements::Link do
6
6
 
7
7
  describe "when mapping how to find an element" do
8
8
  it "should map watir types to same" do
9
- [:class, :href, :id, :index, :name, :text, :xpath].each do |t|
9
+ [:class, :href, :id, :index, :name, :text, :xpath, :css].each do |t|
10
10
  identifier = link.watir_identifier_for t => 'value'
11
11
  identifier.keys.first.should == t
12
12
  end
@@ -20,7 +20,7 @@ describe PageObject::Elements::Link do
20
20
  end
21
21
 
22
22
  it "should map selenium types to same" do
23
- [:class, :id, :link, :link_text, :name, :xpath, :index].each do |t|
23
+ [:class, :id, :link, :link_text, :name, :xpath, :index, :css].each do |t|
24
24
  key, value = link.selenium_identifier_for t => 'value'
25
25
  key.should == t
26
26
  end
@@ -75,6 +75,13 @@ describe PageObject do
75
75
  @page = CallbackPage.new(watir_browser)
76
76
  @page.initialize_page.should be_true
77
77
  end
78
+
79
+ it "should know which element has focus" do
80
+ watir_browser.should_receive(:execute_script).and_return(watir_browser)
81
+ watir_browser.should_receive(:tag_name).twice.and_return(:input)
82
+ watir_browser.should_receive(:type).and_return(:submit)
83
+ watir_page_object.element_with_focus.class.should == PageObject::Elements::Button
84
+ end
78
85
  end
79
86
 
80
87
  context "when using WatirPageObject" do
@@ -131,7 +138,6 @@ describe PageObject do
131
138
  end
132
139
 
133
140
  it "should execute javascript on the browser" do
134
- watir_browser.should_receive(:wd).and_return(watir_browser)
135
141
  watir_browser.should_receive(:execute_script).and_return("abc")
136
142
  watir_page_object.execute_script("333").should == "abc"
137
143
  end
@@ -6,6 +6,11 @@ class FactoryTestPage
6
6
  page_url "http://google.com"
7
7
  end
8
8
 
9
+ class TestPageWithDirectUrl
10
+ include PageObject
11
+ direct_url "http//google.com"
12
+ end
13
+
9
14
  class AnotherPage
10
15
  include PageObject
11
16
  end
@@ -33,12 +38,33 @@ describe PageObject::PageFactory do
33
38
  end
34
39
  end
35
40
 
41
+ it "should create a new page object and execute a block using 'on'" do
42
+ @world.browser.should_not_receive(:goto)
43
+ @world.on FactoryTestPage do |page|
44
+ page.should be_instance_of FactoryTestPage
45
+ end
46
+ end
47
+
36
48
  it "should create and visit a new page" do
37
49
  @world.browser.should_receive(:goto)
38
50
  @world.visit_page FactoryTestPage do |page|
39
51
  page.should be_instance_of FactoryTestPage
40
52
  end
41
53
  end
54
+
55
+ it "should create and visit a new page using 'visit'" do
56
+ @world.browser.should_receive(:goto)
57
+ @world.visit FactoryTestPage do |page|
58
+ page.should be_instance_of FactoryTestPage
59
+ end
60
+ end
61
+
62
+ it "should create and visit a new page when url is defined as 'direct_url'" do
63
+ @world.browser.should_receive(:goto)
64
+ @world.visit TestPageWithDirectUrl do |page|
65
+ page.should be_instance_of TestPageWithDirectUrl
66
+ end
67
+ end
42
68
 
43
69
  it "should set an instance variable that can be used outside of the block" do
44
70
  page = @world.on_page FactoryTestPage