page_object_wrapper 0.0.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.md +180 -340
  2. data/bad_pages/bad_page.rb +34 -0
  3. data/good_pages/another_test_page.rb +14 -0
  4. data/good_pages/some_test_page.rb +53 -0
  5. data/good_pages/test_table_page.rb +12 -0
  6. data/img/scheme.png +0 -0
  7. data/lib/page_object_wrapper.rb +57 -92
  8. data/lib/page_object_wrapper/Action.rb +11 -0
  9. data/lib/page_object_wrapper/Dsl.rb +35 -0
  10. data/lib/page_object_wrapper/Element.rb +15 -0
  11. data/lib/page_object_wrapper/ElementsSet.rb +22 -0
  12. data/lib/page_object_wrapper/Exceptions.rb +7 -21
  13. data/lib/page_object_wrapper/PageObject.rb +371 -0
  14. data/lib/page_object_wrapper/Pagination.rb +6 -57
  15. data/lib/page_object_wrapper/Table.rb +13 -48
  16. data/lib/page_object_wrapper/known_elements.rb +31 -0
  17. data/lib/page_object_wrapper/version.rb +1 -1
  18. data/spec/define_page_object_spec.rb +162 -0
  19. data/spec/defined_elements_spec.rb +77 -0
  20. data/spec/feed_elements_spec.rb +95 -0
  21. data/spec/fire_event_spec.rb +53 -0
  22. data/spec/generate_spec.rb +20 -0
  23. data/spec/load_spec.rb +46 -0
  24. data/spec/open_spec.rb +70 -0
  25. data/spec/select_from_spec.rb +84 -0
  26. data/spec/shared_examples.rb +12 -0
  27. data/spec/spec_helper.rb +2 -15
  28. metadata +35 -27
  29. data/Gemfile +0 -4
  30. data/LICENSE.txt +0 -22
  31. data/Rakefile +0 -1
  32. data/lib/page_object_wrapper/Distribution.rb +0 -43
  33. data/lib/page_object_wrapper/DynamicClass.rb +0 -31
  34. data/lib/page_object_wrapper/Editable.rb +0 -13
  35. data/lib/page_object_wrapper/Form.rb +0 -116
  36. data/lib/page_object_wrapper/Submitter.rb +0 -8
  37. data/page_object_wrapper.gemspec +0 -22
  38. data/spec/example_spec.rb +0 -42
  39. data/spec/form_spec.rb +0 -176
  40. data/spec/page_object_spec.rb +0 -173
  41. data/spec/pagination_spec.rb +0 -14
  42. data/spec/table_spec.rb +0 -52
  43. data/spec/test_data_spec.rb +0 -38
@@ -1,59 +1,8 @@
1
- require 'Exceptions'
1
+ require 'Dsl'
2
+ class Pagination < DslElementWithLocator
2
3
 
3
- class Pagination
4
-
5
- def initialize(page_object,pagination_div_args)
6
- page_numbers=[]
7
- link_pattern=''
8
- @pagination_links=[]
9
- @page_object=page_object
10
- @accessor=@page_object.class.accessor
11
- @base_url=@page_object.class.url
12
- if @accessor.div(pagination_div_args).exists?
13
- raise ArgumentError.new("Cant find ul list inside div #{pagination_div_args.inspect} on page #{@accessor.url}") if not @accessor.div(pagination_div_args).ul.exists?
14
- raise ArgumentError.new("Pagination link not found inside div #{pagination_div_args.inspect} on page #{@accessor.url}") if @accessor.div(pagination_div_args).ul.links.to_a.empty?
15
- @accessor.div(pagination_div_args).ul.links.each{|l|
16
- if l.text=~/\d/
17
- page_numbers << l.text.to_i
18
- @link_pattern=l.href
19
- end
20
- }
21
- page_numbers.collect(&:to_i).sort!
22
- for i in page_numbers.first..page_numbers.last do
23
- @pagination_links << @link_pattern.gsub(/=\d+/,"="+i.to_s)
24
- end
25
- else
26
- # assuming that page has no pagination links
27
- @pagination_links << @base_url
28
- @link_pattern=@base_url
29
- end
30
- end
31
-
32
- def number(n)
33
- raise ArgumentError.new("Cant find pagination page number #{n} on Page #{@accessor.url}") if not @pagination_links.include?(@link_pattern.gsub(/=\d+/,"="+(n).to_s))
34
- change_page(@pagination_links[n-1])
35
- end
36
-
37
- def first
38
- change_page(@pagination_links.first)
39
- end
40
- def last
41
- change_page(@pagination_links.last)
42
- end
43
- def reset
44
- change_page(@base_url)
45
- end
46
- def each
47
- @pagination_links.each{|link|
48
- p=change_page(link)
49
- yield p
50
- }
51
- end
52
-
53
-
54
- private
55
- def change_page(link)
56
- @page_object.class.url=link
57
- @page_object.class.new(true)
58
- end
4
+ def initialize(label)
5
+ super label
6
+ end
59
7
  end
8
+
@@ -1,50 +1,15 @@
1
- require 'Exceptions'
1
+ require 'Dsl'
2
+ class Table < DslElementWithLocator
3
+ dsl_attr_accessor :header
4
+ DEFAULT_HEADER_COLLUMNS_NUMBER = 100
5
+ DEFAULT_HEADER_COLLUMNS_PREFIX = 'column_'
6
+
2
7
 
3
- class Table < Watir::Table
4
- def initialize(accessor,*args)
5
- super(accessor, extract_selector(args).merge(:tag_name => "table"))
6
- @cells=[]
7
- self.rows.each{|row|
8
- row.cells.each{|cell|
9
- @cells << cell
10
- }
11
- }
12
- end
13
-
14
- def has_cell?(text)
15
- @cells.collect(&:text).include?(text)
16
- end
17
- def cells
18
- @cells
19
- end
20
- def select(column_name,where_hash)
21
- ######## TABLE ##############
22
- # HEADER r0,c0 r0,c1,...,r0cN
23
- # ROW r1,c0,r1,c1,...,r1cN
24
- return_column,find_by_column_name,find_by_column_value=nil
25
- begin
26
- return_column=Regexp.new(column_name)
27
- find_by_column_name=Regexp.new(where_hash[:where].keys.first)
28
- find_by_column_value=where_hash[:where].values.first
29
- rescue
30
- raise TableError.new('invalid parameters, check column names and parameters (must be Table#select(column_name_regexp,:where=>{column_name_regexp=>value_or_:any}))','select',column_name,where_hash) if not find_attrs_valid?(column_name,where_hash)
31
- end
32
- index=0
33
- return_index=nil
34
- find_by_index=nil
35
- self.rows[0].cells.each{|header_cell|
36
- return_index=index if return_column===header_cell.text
37
- find_by_index=index if find_by_column_name===header_cell.text
38
- index+=1
39
- }
40
- raise TableError.new("column not found",'select',column_name) if return_index.nil?
41
- raise TableError.new("column not found",'select',where_hash) if find_by_index.nil?
42
- return self.rows[1].cells[return_index] if find_by_column_value==:any
43
- found=nil
44
- self.rows.each{|row|
45
- found=row.cells[return_index] if row.cells[find_by_index].text==find_by_column_value
46
- }
47
- raise TableError.new("value #{find_by_column_value} not found in column #{find_by_column_name}",'select',column_name,where_hash) if found.nil?
48
- found
49
- end
8
+ def initialize(label)
9
+ super label
10
+ h = []
11
+ DEFAULT_HEADER_COLLUMNS_NUMBER.times { |i| h << (DEFAULT_HEADER_COLLUMNS_PREFIX+i.to_s).to_sym }
12
+ @header = h
13
+ end
50
14
  end
15
+
@@ -0,0 +1,31 @@
1
+ KNOWN_ELEMENTS = [
2
+ 'a', 'abbr', 'abbrs', 'address', 'addresses', 'area',
3
+ 'areas', 'article', 'articles', 'as', 'aside', 'asides',
4
+ 'audio', 'audios', 'b', 'base', 'bases', 'bdi', 'bdis',
5
+ 'bdo', 'bdos', 'blockquote', 'blockquotes', 'body', 'bodys',
6
+ 'br', 'brs', 'bs', 'button', 'buttons', 'canvas', 'canvases',
7
+ 'caption', 'captions', 'checkbox', 'checkboxes', 'cite',
8
+ 'cites', 'code', 'codes', 'col', 'colgroup', 'colgroups',
9
+ 'cols', 'command', 'commands', 'datalist', 'datalists',
10
+ 'dd', 'dds', 'del', 'dels', 'details', 'dfn', 'dfns', 'div',
11
+ 'divs', 'dl', 'dls', 'dt', 'dts', 'element', 'elements',
12
+ 'em', 'embed', 'embeds', 'ems', 'fieldset', 'fieldsets', 'figcaption',
13
+ 'figcaptions', 'figure', 'figures', 'file_field', 'file_fields', 'font',
14
+ 'fonts', 'footer', 'footers', 'form', 'forms', 'frame', 'frames',
15
+ 'h1', 'h1s', 'h2', 'h2s', 'h3', 'h3s', 'h4', 'h4s', 'h5', 'h5s', 'h6',
16
+ 'h6s', 'head', 'header', 'headers', 'heads', 'hgroup', 'hgroups',
17
+ 'hidden', 'hiddens', 'hr', 'hrs', 'html', 'htmls', 'i', 'iframe',
18
+ 'iframes', 'img', 'imgs', 'input', 'inputs', 'ins', 'inses',
19
+ 'is', 'kbd', 'kbds', 'keygen', 'keygens', 'label', 'labels', 'legend',
20
+ 'legends', 'li', 'link', 'lis', 'map', 'maps', 'mark', 'marks', 'menu', 'menus',
21
+ 'meta', 'metas', 'meter', 'meters', 'nav', 'navs', 'noscript', 'noscripts',
22
+ 'object', 'objects', 'ol', 'ols', 'optgroup', 'optgroups', 'option', 'options',
23
+ 'output', 'outputs', 'p', 'param', 'params', 'pre', 'pres', 'progress', 'progresses',
24
+ 'ps', 'q', 'qs', 'radio', 'radios', 'rp', 'rps', 'rt', 'rts', 'rubies', 'ruby', 's',
25
+ 'samp', 'samps', 'script', 'scripts', 'section', 'sections', 'select', 'selects', 'small',
26
+ 'smalls', 'source', 'sources', 'span', 'spans', 'ss', 'strong', 'strongs', 'style', 'styles',
27
+ 'sub', 'subs', 'summaries', 'summary', 'sup', 'sups', 'table', 'tables', 'tbody', 'tbodys', 'td',
28
+ 'tds', 'text_field', 'text_fields', 'textarea', 'textareas', 'tfoot', 'tfoots', 'th', 'thead',
29
+ 'theads', 'ths', 'time', 'times', 'title', 'titles', 'tr', 'track', 'tracks', 'trs', 'ul',
30
+ 'uls', 'var', 'vars', 'video', 'videos', 'wbr', 'wbrs'
31
+ ]
@@ -1,3 +1,3 @@
1
1
  module PageObjectWrapper
2
- VERSION = "0.0.6"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -0,0 +1,162 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples'
3
+
4
+ describe "define_page_object" do
5
+ let!(:page_object){
6
+ begin
7
+ PageObjectWrapper.load('./good_pages')
8
+ rescue
9
+ end
10
+ PageObjectWrapper.receive_page(:some_test_page)
11
+ }
12
+
13
+ context "page_object" do
14
+ subject { page_object }
15
+ it { should be_a(PageObject)}
16
+ specify {page_object.class.pages.should include(subject)}
17
+
18
+ describe "page_object label" do
19
+ it_should_behave_like "a label"
20
+ end
21
+
22
+ describe "page_object locator" do
23
+ it_should_behave_like "a locator"
24
+ its(:locator_value) { should be_a String }
25
+ end
26
+
27
+ describe "page_object uniq_element" do
28
+ its(:uniq_element_type) { should eq :h1 }
29
+ its(:uniq_element_hash) { should == {:text => 'Testing display of HTML elements'} }
30
+ end
31
+
32
+ specify { subject.esets.collect(&:label_value).should include(:test_elements)}
33
+ it { should respond_to(:test_elements) }
34
+ it { should respond_to(:feed_test_elements) }
35
+
36
+ specify { subject.elements.collect(&:label_value).should include(:tf)}
37
+ it { should respond_to(:tf) }
38
+
39
+ specify { subject.actions.collect(&:label_value).should include(:press_cool_button)}
40
+ it { should respond_to(:fire_press_cool_button) }
41
+
42
+ specify { subject.tables.collect(&:label_value).should include(:table_without_header)}
43
+ it { should respond_to(:select_from_table_without_header) }
44
+ it { should respond_to(:select_from_table_with_header) }
45
+
46
+ specify { subject.paginations.collect(&:label_value).should include(:some_pagination)}
47
+ it { should respond_to(:some_pagination_each) }
48
+ it { should respond_to(:some_pagination_open) }
49
+ end
50
+
51
+
52
+ context "elements_set" do
53
+ subject { page_object.esets[page_object.esets.collect(&:label_value).index(:test_elements)] }
54
+
55
+ it { should be_a(ElementsSet) }
56
+
57
+ describe "elements_set label" do
58
+ it_should_behave_like "a label"
59
+ end
60
+ end
61
+
62
+ context "element" do
63
+ subject { page_object.elements[page_object.elements.collect(&:label_value).index(:tf)] }
64
+
65
+ it { should be_a(Element) }
66
+
67
+ describe "element label" do
68
+ it_should_behave_like "a label"
69
+ end
70
+
71
+ describe "element locator" do
72
+ it_should_behave_like "a locator"
73
+ its(:locator_value) { should be_a Hash }
74
+ end
75
+
76
+ describe "element food" do
77
+ it { should respond_to(:fresh_food) }
78
+ it { should respond_to(:fresh_food_value) }
79
+ it { should respond_to(:missing_food) }
80
+ it { should respond_to(:missing_food_value) }
81
+
82
+ its(:fresh_food_value) { should be_a String}
83
+ its(:missing_food_value) { should be_a String}
84
+
85
+ describe "food default values" do
86
+ its(:fresh_food_value){ should eq('some fresh food') }
87
+ end
88
+
89
+ describe "food user defined values" do
90
+ its(:missing_food_value){ should eq('some missing food') }
91
+ end
92
+ end
93
+ end
94
+
95
+ context "action" do
96
+ subject { page_object.actions[page_object.actions.collect(&:label_value).index(:press_cool_button)] }
97
+
98
+ it { should be_a(Action) }
99
+
100
+ describe "action label" do
101
+ it_should_behave_like "a label"
102
+ end
103
+
104
+ describe "action next_page" do
105
+ it { should respond_to(:next_page_value) }
106
+ it { should respond_to(:fire_block_value) }
107
+
108
+ its(:next_page_value){ should eq(:test_page_with_table) }
109
+ its(:fire_block_value) { should be_a Proc }
110
+ end
111
+ end
112
+
113
+ context "table" do
114
+ subject { page_object.tables[page_object.tables.collect(&:label_value).index(:table_without_header)] }
115
+
116
+ it { should be_a(Table) }
117
+
118
+ describe "table label" do
119
+ it_should_behave_like "a label"
120
+ end
121
+
122
+ describe "element locator" do
123
+ it_should_behave_like "a locator"
124
+ its(:locator_value) { should be_a Hash }
125
+ end
126
+
127
+ describe "table header" do
128
+ it { should respond_to(:header) }
129
+ it { should respond_to(:header_value) }
130
+
131
+ its(:header_value) { should be_a Array}
132
+
133
+ describe "default header" do
134
+ subject { page_object.tables[page_object.tables.collect(&:label_value).index(:table_without_header)] }
135
+ let(:default_header){
136
+ h = []
137
+ 100.times {|i| h << ('column_'+i.to_s).to_sym}
138
+ h
139
+ }
140
+ its(:header_value) { should eq(default_header)}
141
+ end
142
+
143
+ describe "user defined header" do
144
+ end
145
+ end
146
+ end
147
+
148
+ describe "pagination" do
149
+ subject { page_object.paginations[page_object.paginations.collect(&:label_value).index(:some_pagination)] }
150
+
151
+ it { should be_a(Pagination) }
152
+
153
+ describe "pagination label" do
154
+ it_should_behave_like "a label"
155
+ end
156
+
157
+ describe "pagination locator" do
158
+ it_should_behave_like "a locator"
159
+ its(:locator_value) { should be_a Hash }
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples'
3
+
4
+ describe "page_object.xxx" do
5
+ context "page is opened in browser" do
6
+
7
+ before(:all){
8
+ @b = Watir::Browser.new
9
+ PageObjectWrapper.use_browser @b
10
+ begin
11
+ PageObjectWrapper.load('./good_pages')
12
+ rescue
13
+ end
14
+ }
15
+ after(:all){ PageObjectWrapper.browser.close }
16
+
17
+ subject{ PageObjectWrapper.open_page(:some_test_page) }
18
+
19
+ context "xxx is an element" do
20
+ its(:tf){ should be_a(Watir::TextField) }
21
+ its("tf.id"){ should eq 'f1' }
22
+
23
+ its(:ta){ should be_a(Watir::TextArea) }
24
+ its("ta.id"){ should eq 'f2' }
25
+
26
+ its(:s1){ should be_a(Watir::Select) }
27
+ its("s1.id"){ should eq 'f10' }
28
+
29
+ its(:s2){ should be_a(Watir::Select) }
30
+ its("s2.id"){ should eq 'f11' }
31
+
32
+ its(:cb){ should be_a(Watir::CheckBox) }
33
+ its("cb.id"){ should eq 'f5' }
34
+
35
+ its(:rb){ should be_a(Watir::Radio) }
36
+ its("rb.id"){ should eq 'f3' }
37
+ end
38
+
39
+ context "xxx is an elements_set" do
40
+ its(:test_elements){ should be_a Array }
41
+ end
42
+
43
+ context "xxx is not defined in page_object" do
44
+ it "raises NoMethodError" do
45
+ expect{subject.undefined_element}.to raise_error(NoMethodError)
46
+ end
47
+ end
48
+ end
49
+
50
+ context "page is not opened in a browser" do
51
+ before(:all){
52
+ @b = Watir::Browser.new
53
+ PageObjectWrapper.use_browser @b
54
+ begin
55
+ PageObjectWrapper.load('./good_pages')
56
+ rescue
57
+ end
58
+ }
59
+ after(:all){ PageObjectWrapper.browser.close }
60
+
61
+ subject{ PageObjectWrapper.receive_page(:some_test_page) }
62
+
63
+ context "xxx is an element" do
64
+ its("tf.present?"){ should be_false }
65
+
66
+ it "raises Watir::Exception::UnknownObjectException when working with element" do
67
+ expect{ subject.tf.set 'qqq' }.to raise_error(Watir::Exception::UnknownObjectException)
68
+ end
69
+ end
70
+
71
+ context "xxx is an elements_set" do
72
+ its(:test_elements){ should be_a Array }
73
+ end
74
+ end
75
+ end
76
+
77
+
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples'
3
+
4
+ describe "page_object.feed_xxx" do
5
+ context "browser is closed" do
6
+ before(:all){
7
+ begin
8
+ PageObjectWrapper.browser.quit if not PageObjectWrapper.browser.nil?
9
+ PageObjectWrapper.load('./good_pages')
10
+ rescue
11
+ end
12
+ }
13
+
14
+ it "raises PageObjectWrapper::BrowserNotFound" do
15
+ tp = PageObjectWrapper.receive_page(:some_test_page)
16
+ expect{ tp.feed_test_elements }.to raise_error(PageObjectWrapper::BrowserNotFound)
17
+ end
18
+ end
19
+
20
+ context "browser is opened" do
21
+ before(:all){
22
+ @b = Watir::Browser.new
23
+ PageObjectWrapper.use_browser @b
24
+ begin
25
+ PageObjectWrapper.load('./good_pages')
26
+ rescue
27
+ end
28
+ }
29
+ after(:all){PageObjectWrapper.browser.close}
30
+
31
+ it "returns current page object" do
32
+ tp = PageObjectWrapper.open_page(:some_test_page)
33
+ tp.feed_test_elements.should eq(tp)
34
+ end
35
+
36
+ context "xxx not found among current_page element_sets" do
37
+ it "raises NoMethodError" do
38
+ tp = PageObjectWrapper.current_page
39
+ expect{tp.feed_empty_set}.to raise_error(NoMethodError)
40
+ end
41
+ end
42
+
43
+ context "argument = :fresh_food" do
44
+ it "populates all xxx elements with :fresh_food" do
45
+ tp = PageObjectWrapper.current_page
46
+ tp.feed_test_elements(:fresh_food)
47
+ @b.text_field(:id=>'f1').value.should eq 'some fresh food'
48
+ @b.textarea(:id=>'f2').value.should eq 'default fresh food'
49
+ @b.select(:id=>'f10').value.should eq "one\n"
50
+ @b.select(:id=>'f11').value.should eq "one\n"
51
+ @b.checkbox(:id=>'f5').should be_set
52
+ @b.radio(:id=>'f3').should be_set
53
+ end
54
+ end
55
+
56
+ context "argument = nil" do
57
+ it "populates all xxx elements with :fresh_food" do
58
+ tp = PageObjectWrapper.current_page
59
+ tp.feed_test_elements
60
+ @b.text_field(:id=>'f1').value.should eq 'some fresh food'
61
+ @b.textarea(:id=>'f2').value.should eq 'default fresh food'
62
+ @b.select(:id=>'f10').value.should eq "one\n"
63
+ @b.select(:id=>'f11').value.should eq "one\n"
64
+ @b.checkbox(:id=>'f5').should be_set
65
+ @b.radio(:id=>'f3').should be_set
66
+ end
67
+ end
68
+
69
+ context "argument = :missing_food" do
70
+ it "populates all xxx elements with :missing_food" do
71
+ tp = PageObjectWrapper.open_page(:some_test_page)
72
+ tp.feed_test_elements(:missing_food)
73
+ @b.text_field(:id=>'f1').value.should eq 'some missing food'
74
+ @b.textarea(:id=>'f2').value.should eq 'default missing food'
75
+ @b.select(:id=>'f10').value.should eq "three\n"
76
+ @b.select(:id=>'f11').value.should eq "two (default)\n"
77
+ @b.checkbox(:id=>'f5').should be_set
78
+ @b.radio(:id=>'f3').should be_set
79
+ end
80
+ end
81
+
82
+ context "fresh food is not found in select list" do
83
+ it "raises Watir Watir::Exception::NoValueFoundException" do
84
+ atp = PageObjectWrapper.open_page(:another_test_page)
85
+ expect{atp.feed_test_elements}.to raise_error(Watir::Exception::NoValueFoundException)
86
+ end
87
+ end
88
+ context "missing food is not found in select list" do
89
+ it "continues execution" do
90
+ atp = PageObjectWrapper.current_page
91
+ atp.feed_test_elements(:missing_food).should eq(atp)
92
+ end
93
+ end
94
+ end
95
+ end