page_object_wrapper 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/README.md +352 -1
- data/lib/page_object_wrapper.rb +96 -3
- data/lib/page_object_wrapper/Distribution.rb +43 -0
- data/lib/page_object_wrapper/DynamicClass.rb +31 -0
- data/lib/page_object_wrapper/Editable.rb +13 -0
- data/lib/page_object_wrapper/Exceptions.rb +22 -0
- data/lib/page_object_wrapper/Form.rb +116 -0
- data/lib/page_object_wrapper/Pagination.rb +59 -0
- data/lib/page_object_wrapper/Submitter.rb +8 -0
- data/lib/page_object_wrapper/Table.rb +50 -0
- data/lib/page_object_wrapper/version.rb +1 -1
- data/page_object_wrapper.gemspec +2 -1
- data/spec/example_spec.rb +42 -0
- data/spec/form_spec.rb +176 -0
- data/spec/page_object_spec.rb +173 -0
- data/spec/pagination_spec.rb +14 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/table_spec.rb +52 -0
- data/spec/test_data_spec.rb +38 -0
- metadata +39 -5
@@ -0,0 +1,43 @@
|
|
1
|
+
class Distribution
|
2
|
+
attr_accessor :total_object_number
|
3
|
+
# distribution types
|
4
|
+
RANDOM=0 #(default)
|
5
|
+
SEQUENTAL=1
|
6
|
+
def initialize(dist_hash) # {status_name => status_probability (in percents)}
|
7
|
+
@distribution=dist_hash
|
8
|
+
@type=RANDOM
|
9
|
+
@sequental_increment=1
|
10
|
+
@sequental_index=@sequental_increment
|
11
|
+
# check for consistency
|
12
|
+
raise "Distribution init error > summ of percents must be 100" if @distribution.values.inject{|sum,x| sum + x }!=100
|
13
|
+
end
|
14
|
+
|
15
|
+
def type(type,total_object_number=nil)
|
16
|
+
raise "Unknown type: #{type}. Possible types are Distribution::RANDOM, Distribution::SEQUENTAL" if not (type==RANDOM or type==SEQUENTAL)
|
17
|
+
@type=type
|
18
|
+
if @type==SEQUENTAL
|
19
|
+
raise "SEQUENTAL type requires the total number of objects that will be generated (>0)" if (total_object_number==nil or total_object_number==0)
|
20
|
+
@sequental_increment=(100/total_object_number).floor
|
21
|
+
@sequental_index=@sequental_increment
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def generate
|
26
|
+
r=(@type==RANDOM)? rand(100) : @sequental_index
|
27
|
+
@sequental_index=(@sequental_index<100)? @sequental_index+@sequental_increment : @sequental_increment
|
28
|
+
dist_ary=@distribution.values
|
29
|
+
dist_ary_normalized=[]
|
30
|
+
dist_ary_normalized << dist_ary.first
|
31
|
+
for i in 1..dist_ary.length-1
|
32
|
+
dist_ary_normalized[i]=dist_ary_normalized[i-1]+dist_ary[i]
|
33
|
+
end
|
34
|
+
ind=(dist_ary_normalized << r).sort!.index(r)
|
35
|
+
return @distribution.keys[ind]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
#d=Distribution.new('new'=>10,'old'=>30,'very_old'=>60)
|
40
|
+
#d.type(Distribution::SEQUENTAL,10)
|
41
|
+
#10.times do
|
42
|
+
# puts d.generate
|
43
|
+
#end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'Exceptions'
|
3
|
+
class DynamicClass
|
4
|
+
|
5
|
+
def metaclass
|
6
|
+
class << self
|
7
|
+
self
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
@@objects=[]
|
12
|
+
def initialize(xml_hash)
|
13
|
+
raise DynamicClassError.new('parameter is not a hash','new',xml_hash) if not xml_hash.is_a?(Hash)
|
14
|
+
xml_hash.each_pair {|attr, value|
|
15
|
+
metaclass.send :attr_accessor, attr
|
16
|
+
send "#{attr}=".to_sym, value
|
17
|
+
}
|
18
|
+
@@objects << self
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.find(attr,value)
|
22
|
+
ind=@@objects.collect(&attr.to_sym).index(value)
|
23
|
+
@@objects[ind]
|
24
|
+
end
|
25
|
+
def self.each
|
26
|
+
@@objects.each{|object|
|
27
|
+
yield object
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Editable
|
2
|
+
attr_accessor :watir_element, :label, :default, :required
|
3
|
+
def initialize(watir_element,label,default,required)
|
4
|
+
@watir_element = watir_element
|
5
|
+
@label=label.to_sym
|
6
|
+
@default=default
|
7
|
+
@required=required
|
8
|
+
end
|
9
|
+
|
10
|
+
def required?
|
11
|
+
required
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class PageError < StandardError
|
2
|
+
attr_accessor :page_url, :page_name
|
3
|
+
def initialize(msg,page_name,page_url)
|
4
|
+
@page_url=page_url
|
5
|
+
@page_name=page_name
|
6
|
+
super "PROBLEM: #{(msg.nil?)? '??' : msg}, PAGE: #{page_name}, URL: #{page_url}"
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
class ParameterError < StandardError
|
12
|
+
attr_accessor :method, :params
|
13
|
+
def initialize(msg,method,*params)
|
14
|
+
@method=method
|
15
|
+
@params=params
|
16
|
+
super "PROBLEM: #{(msg.nil?)? '??' : msg}, METHOD: #{method}, PARAMS: #{params.join('; ').to_s}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class TableError < ParameterError; end
|
21
|
+
class FormError < ParameterError; end
|
22
|
+
class DynamicClassError < ParameterError; end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'Editable'
|
2
|
+
require 'Exceptions'
|
3
|
+
require 'Submitter'
|
4
|
+
class Form < Watir::Form
|
5
|
+
def initialize(current_page,target_page,accessor,*args)
|
6
|
+
super(accessor,extract_selector(args).merge(:tag_name => "form"))
|
7
|
+
@elements=[]
|
8
|
+
@accessor=accessor
|
9
|
+
@current_page=current_page
|
10
|
+
@target_page=target_page
|
11
|
+
@submit_element = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def editable(type,how_find_hash,label=type,default='Field data',required=false)
|
15
|
+
watir_element = @accessor.send(type.to_sym,how_find_hash)
|
16
|
+
raise FormError.new('Element not found', 'editable', "TYPE=#{type.inspect}, HOW_FIND=#{how_find_hash.inspect}, PAGE=#{@current_page.class.name}") if not element.exists?
|
17
|
+
Form.send :define_method, label do
|
18
|
+
watir_element
|
19
|
+
end
|
20
|
+
@elements << Editable.new(watir_element,label,default,required)
|
21
|
+
end
|
22
|
+
|
23
|
+
def submitter(type,how_find_hash,how_click_method = :click,*click_params)
|
24
|
+
# find submitter
|
25
|
+
@submit_element = Submitter.new(how_click_method,click_params)
|
26
|
+
@submit_element.watir_element = @accessor.send(type.to_sym,how_find_hash)
|
27
|
+
raise FormError.new('Element not found', 'submitter', "TYPE=#{type.inspect}, HOW_FIND=#{how_find_hash.inspect}") if not @submit_element.watir_element.exists?
|
28
|
+
end
|
29
|
+
|
30
|
+
def submit(correct_submission_flag)
|
31
|
+
return_page = (correct_submission_flag==true)? @target_page : @current_page
|
32
|
+
if @submit_element.nil?
|
33
|
+
# submitter is not defined, applying regular form submission
|
34
|
+
super()
|
35
|
+
return return_page.new
|
36
|
+
else
|
37
|
+
# submitter defined, applying its submission method
|
38
|
+
begin
|
39
|
+
@submit_element.watir_element.send(@submit_element.how_click_method.to_sym,*@submit_element.click_params)
|
40
|
+
rescue Exception => e
|
41
|
+
puts "ORIGINAL ERROR: #{e.message}"
|
42
|
+
raise FormError.new('Submitter not clickable', "SUBMITTER: TYPE=#{@submit_element.type}, HOW_FIND=#{@submit_element.how_find_hash}", "CLICK_METHOD: #{@submit_element.how_click_mehtod}, CLICK_PARAMS: #{@submit_element.click_params}")
|
43
|
+
end
|
44
|
+
return return_page.new
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def default(label)
|
49
|
+
find_input(label).default
|
50
|
+
end
|
51
|
+
|
52
|
+
def each
|
53
|
+
@elements.each{|e|
|
54
|
+
yield e
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def each_required
|
59
|
+
@elements.select{|f| f.required}.each{|e|
|
60
|
+
yield e
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def fill_only(data_hash)
|
65
|
+
if data_hash.nil?
|
66
|
+
raise FormError.new('Invalid parameter','fill_only',data_hash)
|
67
|
+
else
|
68
|
+
data_hash.each_key{|label|
|
69
|
+
f=find_input(label)
|
70
|
+
f.watir_element.set(data_hash[label])
|
71
|
+
}
|
72
|
+
end
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
def fill_all(except_hash=nil)
|
77
|
+
fill_elements(@elements,except_hash)
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
def fill_required(except_hash=nil)
|
82
|
+
fill_elements(@elements.select{|f| f.required},except_hash)
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
private
|
88
|
+
def find_input(label)
|
89
|
+
label = label.to_sym
|
90
|
+
index=@elements.collect(&:label).index(label)
|
91
|
+
raise FormError.new('label not found','find_input',label) if index.nil?
|
92
|
+
@elements[index]
|
93
|
+
end
|
94
|
+
def fill_elements(elements,except_hash)
|
95
|
+
elements = @elements.clone
|
96
|
+
if not except_hash.nil?
|
97
|
+
if not except_hash.has_key?(:except)
|
98
|
+
raise FormError.new('Invalid parameter, must be :except=>"label or [label1,label2,...]"',"fill_elements",elements.inspect,except_hash.inspect)
|
99
|
+
else
|
100
|
+
if except_hash[:except].is_a?(String) or except_hash[:except].is_a?(Symbol)
|
101
|
+
elements.delete(find_input(except_hash[:except]))
|
102
|
+
elsif except_hash[:except].is_a?(Array)
|
103
|
+
except_hash[:except].each{|label|
|
104
|
+
elements.delete(find_input(label))
|
105
|
+
}
|
106
|
+
else
|
107
|
+
raise FormError.new('Invalid parameter',"fill_elements",elements.inspect,except_hash.inspect)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
elements.each{|field|
|
112
|
+
field.watir_element.when_present.set(field.default)
|
113
|
+
}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'Exceptions'
|
2
|
+
|
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
|
59
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'Exceptions'
|
2
|
+
|
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
|
50
|
+
end
|
data/page_object_wrapper.gemspec
CHANGED
@@ -12,10 +12,11 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.summary = %q{Wraps watir-webdriver with convenient testing interface.}
|
13
13
|
gem.homepage = "https://github.com/evgeniy-khatko/page_object_wrapper"
|
14
14
|
gem.add_dependency "watir-webdriver"
|
15
|
+
gem.add_development_dependency "rspec", ">= 2.0.0"
|
15
16
|
|
16
17
|
|
17
18
|
gem.files = `git ls-files`.split($/)
|
18
19
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
20
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
-
gem.require_paths = ["lib"]
|
21
|
+
gem.require_paths = ["lib","lib/page_object_wrapper"]
|
21
22
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Overall usage" do
|
4
|
+
describe "Test Google for long search query" do
|
5
|
+
let(:precondition){"Open google.com page"}
|
6
|
+
let(:step1){"Enter long search query into the search field"}
|
7
|
+
let(:step2){"Press search button"}
|
8
|
+
let(:expected_result){"The requested URL /... is too large to process"}
|
9
|
+
|
10
|
+
describe "Defining two page objects: GoogleSearchPage, GoogleErrorPage and performing test" do
|
11
|
+
let(:pages_definition){
|
12
|
+
class GoogleSearchPage < PageObjectWrapper::Page
|
13
|
+
attr_accessor :find_form
|
14
|
+
@url="/"
|
15
|
+
expected_element :text_field, :name => 'q'
|
16
|
+
def initialize visit=false
|
17
|
+
super visit
|
18
|
+
@find_form = form(GoogleErrorPage, {:action => '/search'})
|
19
|
+
@find_form.editable(:text_field, {:name => 'q'}, :seach_what, '@find_form default value', true)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class GoogleErrorPage < PageObjectWrapper::Page
|
24
|
+
@url="really does not matter, because navigating to this page done through search query submission"
|
25
|
+
#expected_element :ins, {:text => "That\’s an error."}
|
26
|
+
end
|
27
|
+
{:gsearch_page_class => GoogleSearchPage, :gerror_page => GoogleErrorPage}
|
28
|
+
}
|
29
|
+
|
30
|
+
it "performing test" do
|
31
|
+
puts precondition
|
32
|
+
google_page = pages_definition[:gsearch_page_class].new(true)
|
33
|
+
puts step1
|
34
|
+
long_query = 'q '*1000
|
35
|
+
google_page.find_form.fill_only(:seach_what => long_query)
|
36
|
+
puts step2
|
37
|
+
google_error_page = google_page.find_form.submit(true)
|
38
|
+
google_error_page.should have_text(expected_result)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/form_spec.rb
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe "Form wrapper" do
|
5
|
+
describe "form definition", :code => 6..30 do
|
6
|
+
let(:page_objects){
|
7
|
+
class GoogleAdvancedSearchPage < PageObjectWrapper::Page
|
8
|
+
attr_accessor :as_form
|
9
|
+
@url="/advanced_search"
|
10
|
+
expected_element :text_field, :name => 'as_q'
|
11
|
+
def initialize visit=false
|
12
|
+
super visit
|
13
|
+
@as_form = form(GoogleResultsPage, {:name => 'f'})
|
14
|
+
@as_form.editable(:text_field, {:name => 'as_q'}, :with_words, 'with_words default value', true)
|
15
|
+
@as_form.editable(:text_field, {:name => 'as_epq'}, :with_phrase, 'with_phrase default value', true)
|
16
|
+
@as_form.editable(:text_field, {:name => 'as_oq'}, :with_any_word, 'with_any_word default value', false)
|
17
|
+
@as_form.submitter(:input, {:type => 'submit'})
|
18
|
+
end
|
19
|
+
end
|
20
|
+
class GoogleResultsPage < PageObjectWrapper::Page
|
21
|
+
@url="/"
|
22
|
+
expected_element :button, :name => 'btnG'
|
23
|
+
def initialize visit=false
|
24
|
+
super visit
|
25
|
+
@find_form = form(GoogleResultsPage, {:action => '/search'})
|
26
|
+
@find_form.editable(:text_field, {:name => 'q'}, :seach_what, '@find_form default value', true)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
{:gresults_page_class => GoogleResultsPage, :gadv_page_class => GoogleAdvancedSearchPage}
|
30
|
+
}
|
31
|
+
|
32
|
+
|
33
|
+
it "is defined as #form(TargetPage, how_find_hash)", :code => [13,25] do
|
34
|
+
page_objects[:gadv_page_class].new(true).as_form.class.should eq(Form)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "has #editable(:field_type, how_find_hash, :label, default_value, required?) method to set form's fields
|
38
|
+
:field_type sets corresponding watir element type
|
39
|
+
how_find_hash is used to locate corresponding watir element on the page
|
40
|
+
:label is used to reffer to the form field after definition
|
41
|
+
default_value is used to populate fields with default test data
|
42
|
+
required = true | false and indicates if the field is required
|
43
|
+
", :code => 14..16 do
|
44
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
45
|
+
gadv_page.as_form.with_words.tag_name.should eq('input')
|
46
|
+
gadv_page.as_form.with_words.name.should eq('as_q')
|
47
|
+
gadv_page.as_form.with_words.should be_a(Watir::TextField)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "has #submitter(:field_type, how_find_hash, how_click_mehtod, click_params) method to set form's submitter
|
51
|
+
:field_type sets corresponding watir element type
|
52
|
+
how_find_hash is used to locate corresponding watir element on the page
|
53
|
+
how_click_mehtod is used to tell Waitr the method which should be applied to click the submitter
|
54
|
+
click_params set parameters for the how_click_mehtod
|
55
|
+
", :code => 17 do
|
56
|
+
end
|
57
|
+
end
|
58
|
+
describe "form usage", :code => 59..84 do
|
59
|
+
let(:page_objects){
|
60
|
+
class GoogleAdvancedSearchPage < PageObjectWrapper::Page
|
61
|
+
attr_accessor :as_form
|
62
|
+
@url="/advanced_search"
|
63
|
+
expected_element :text_field, :name => 'as_q'
|
64
|
+
def initialize visit=false
|
65
|
+
super visit
|
66
|
+
@as_form = form(GoogleSearchPage, {:name => 'f'})
|
67
|
+
@as_form.editable(:text_field, {:name => 'as_q'}, :with_words, 'with_words default value', true)
|
68
|
+
@as_form.editable(:text_field, {:name => 'as_epq'}, :with_phrase, 'with_phrase default value', true)
|
69
|
+
@as_form.editable(:text_field, {:name => 'as_oq'}, :with_any_word, 'with_any_word default value', false)
|
70
|
+
@as_form.submitter(:input, {:type => 'submit'})
|
71
|
+
end
|
72
|
+
end
|
73
|
+
class GoogleSearchPage < PageObjectWrapper::Page
|
74
|
+
attr_accessor :find_form
|
75
|
+
@url="/"
|
76
|
+
expected_element :text_field, :name => 'q' # tells Watir that we expect this element which identifies the page in a uniq way
|
77
|
+
def initialize visit=false
|
78
|
+
super visit
|
79
|
+
@find_form = form(GoogleSearchPage, {:action => '/search'})
|
80
|
+
@find_form.editable(:text_field, {:name => 'q'}, :search_what, '@find_form default value', true)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
{:gsearch_page_class => GoogleSearchPage, :gadv_page_class => GoogleAdvancedSearchPage}
|
84
|
+
}
|
85
|
+
|
86
|
+
it "has #submit(flag) method which submits form
|
87
|
+
if flag = true than TargetPage instance is returned
|
88
|
+
if flag = false than CurrentPage instance is returned
|
89
|
+
if submitter is defined, than it's used to submit the form, otherwise standart Watir::Form submit is used
|
90
|
+
", :code => 91..95 do
|
91
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
92
|
+
gsearch_page = gadv_page.as_form.submit(true)
|
93
|
+
gsearch_page.should be_a(GoogleSearchPage)
|
94
|
+
gsearch_page_reloaded = gsearch_page.find_form.submit(false)
|
95
|
+
gsearch_page_reloaded.should be_a(GoogleSearchPage)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "field's watir elements can be accessed by labels", :code => 99..100 do
|
99
|
+
gsearch_page = page_objects[:gsearch_page_class].new(true)
|
100
|
+
gsearch_page.find_form.search_what.should be_a(Watir::TextField)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "fields default values set during form definition can be retrieved with #default(:label) method", :code => 104..105 do
|
104
|
+
gsearch_page = page_objects[:gsearch_page_class].new(true)
|
105
|
+
gsearch_page.find_form.default(:search_what).should eq('@find_form default value')
|
106
|
+
end
|
107
|
+
|
108
|
+
it "has #each method which allows navigating between form fields", :code => 110..112 do
|
109
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
110
|
+
gadv_page.as_form.each{|form_field|
|
111
|
+
form_field.should be_a(Editable)
|
112
|
+
form_field.watir_element.should be_a(Watir::TextField)
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
it "has #each_required method which allows navigating between form required fields", :code => 118..122 do
|
117
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
118
|
+
gadv_page.as_form.each_required{|form_field|
|
119
|
+
form_field.should be_a(Editable)
|
120
|
+
form_field.watir_element.should be_a(Watir::TextField)
|
121
|
+
form_field.should be_required
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
125
|
+
it "has #fill_only(:label => value) method which populated form's :label field with \'value\'
|
126
|
+
method returns form object
|
127
|
+
", :code => 129..130 do
|
128
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
129
|
+
form = gadv_page.as_form.fill_only(:with_words => 'some value')
|
130
|
+
gadv_page.as_form.with_words.value.should eq('some value')
|
131
|
+
form.should be_a(Form)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "has #fill_all(:except => labels_array | label) method which populated all form's fields with default values
|
135
|
+
if except_hash is not nil than provided fields are not populated
|
136
|
+
method returns form object
|
137
|
+
", :code => 139..142 do
|
138
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
139
|
+
form = gadv_page.as_form.fill_all
|
140
|
+
gadv_page.as_form.each{|field|
|
141
|
+
field.watir_element.value.should eq(gadv_page.as_form.default(field.label))
|
142
|
+
}
|
143
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
144
|
+
gadv_page.as_form.fill_all(:except => :with_words)
|
145
|
+
gadv_page.as_form.each{|field|
|
146
|
+
if field.label==:with_words
|
147
|
+
field.watir_element.value.should eq ''
|
148
|
+
else
|
149
|
+
field.watir_element.value.should eq(gadv_page.as_form.default(field.label))
|
150
|
+
end
|
151
|
+
}
|
152
|
+
form.should be_a(Form)
|
153
|
+
end
|
154
|
+
|
155
|
+
it "has #fill_required(:except => labels_array | label) method which populated all required form's fields with default values
|
156
|
+
if except_hash is not nil than provided fields are not populated
|
157
|
+
method returns form object
|
158
|
+
", :code => 165..172 do
|
159
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
160
|
+
form = gadv_page.as_form.fill_required
|
161
|
+
gadv_page.as_form.each_required{|field|
|
162
|
+
field.watir_element.value.should eq(gadv_page.as_form.default(field.label))
|
163
|
+
}
|
164
|
+
gadv_page = page_objects[:gadv_page_class].new(true)
|
165
|
+
gadv_page.as_form.fill_required(:except => :with_words)
|
166
|
+
gadv_page.as_form.each_required{|field|
|
167
|
+
if field.label==:with_words
|
168
|
+
field.watir_element.value.should eq ''
|
169
|
+
else
|
170
|
+
field.watir_element.value.should eq(gadv_page.as_form.default(field.label))
|
171
|
+
end
|
172
|
+
}
|
173
|
+
form.should be_a(Form)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|