domkey 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,7 +18,7 @@ module Domkey
18
18
  # TODO Example:
19
19
  class PageObjectCollection
20
20
 
21
- include WidgetryPackage
21
+ include Widgetry::Package
22
22
  include Enumerable
23
23
 
24
24
  # @return [PageObject, Hash{Symbol => PageObjectCollection}]
@@ -1,69 +1,20 @@
1
- require 'domkey/view/labeled_group'
1
+ require 'domkey/view/option_selectable_group'
2
+
2
3
  module Domkey
3
4
 
4
5
  module View
5
6
 
6
7
  # RadioGroup allows you to interact with PageObjectCollection of radios as a single PageObject
7
- # Radios collection is constrained by the same name attribute and behaves like one object.
8
- # It behaves like a single Select list.
9
- # When one radio is selected in the collection the others become unselected.
10
- class RadioGroup < PageObjectCollection
11
-
12
- # @param [String] match text in value attribute and set that radio
13
- def set value
14
- validate_scope
15
- return unless value
16
- [*value].each do |v|
17
- e = case v
18
- when String
19
- element.find { |e| e.value == v }
20
- when Regexp
21
- element.find { |e| e.value.match(v) }
22
- end
23
- e ? e.set : fail(Exception::Error, "RadioGroup value not found: #{v.inspect}")
24
- end
25
- end
26
-
27
- # @return [String] text in value attribute of currently set
28
- def value
29
- validate_scope
30
- element.find_all { |r| r.set? }.map { |e| e.value }
31
- end
32
-
33
- def options
34
- validate_scope
35
- element.map { |e| e.value }
36
- end
37
-
38
- # convert to LabeledGroup settable by corresponding label text
39
- def to_labeled
40
- LabeledGroup.new(self)
41
- end
42
-
43
- # @yield [PageObject]
44
- def each(&blk)
45
- validate_scope
46
- super(&blk)
47
- end
48
-
49
- # @return [Array<PageObject>]
50
- def to_a
51
- validate_scope
52
- super
53
- end
8
+ # Acts like OptionSelectable
9
+ # Radios collection is constrained by the same name attribute
10
+ # Behaves like a single Select list.
11
+ # It has one radio selected at all times
12
+ class RadioGroup < OptionSelectableGroup
54
13
 
55
14
  private
56
15
 
57
- # precondition on acting on this collection
58
- # @return [true] when all radios in collection share the same name attribute
59
- # @raise [Exception::Error] when where is more than one unique name attribute
60
- # --
61
- # returns true on subsequent unless magically more radios show up after initial validation
62
- def validate_scope
63
- return if @validated
64
- groups = element.map { |e| e.name }.uniq
65
- fail(Exception::Error, "RadioGroup definition scope too broad: Found #{groups.count} radio groups with names: #{groups}") unless (groups.size == 1)
66
- @validated = true
16
+ def before_set
17
+ validate_scope
67
18
  end
68
19
  end
69
20
  end
@@ -0,0 +1,67 @@
1
+ require 'domkey/view/option_selectable'
2
+
3
+ module Domkey
4
+ module View
5
+
6
+ class SelectList < PageObject
7
+
8
+ include OptionSelectable
9
+
10
+ def set_by_string value
11
+ element.select value
12
+ end
13
+
14
+ def set_by_regexp value
15
+ element.select value
16
+ end
17
+
18
+ def set_by_index value
19
+ case value
20
+ when Fixnum
21
+ element.options[value].select
22
+ when Array
23
+ value.each do |i|
24
+ element.options[i].select
25
+ end
26
+ end
27
+ end
28
+
29
+ def set_by_value value
30
+ case value
31
+ when String
32
+ element.select_value value
33
+ when Array
34
+ value.each { |v| element.select_value v }
35
+ end
36
+ end
37
+
38
+ def value_by_options opts
39
+ element.selected_options.map do |o|
40
+ Hash[opts.map { |opt| [opt, o.send(opt)] }]
41
+ end
42
+ end
43
+
44
+ def value_by_default
45
+ element.selected_options.map { |e| e.text }
46
+ end
47
+
48
+ # iffy
49
+ def options
50
+ element.options.map do |o|
51
+ {text: o.text,
52
+ value: o.value,
53
+ index: o.index}
54
+ end
55
+ end
56
+
57
+
58
+ private
59
+
60
+ def before_set
61
+ element.clear if element.multiple?
62
+ end
63
+
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,51 @@
1
+ module Domkey
2
+ module View
3
+ module Widgetry
4
+
5
+ # Dispatcher chooser for a given Element
6
+ # @param object [Element] thing that needs to be interacted with, i.e. Watir::Select, Watir::CheckBox, Selenium::Element etc...
7
+ # @return [Widgetry::Dispatcher] (or subclass of) that handles the strategy for interacting with the element
8
+ def self.dispatcher(object)
9
+ object_class_name = object.class.name.split('::').last
10
+ if const_defined? object_class_name.to_sym
11
+ const_get("#{self}::#{object_class_name}").new(object)
12
+ else
13
+ Dispatcher.new(object)
14
+ end
15
+ end
16
+
17
+ # Widgetry::Dispatcher is a communication object responsible for
18
+ # receiving and transmitting messages to PageObject Element.
19
+ # Client should subclass and provide desired interaction strategy that may differ from provided by default
20
+ class Dispatcher < SimpleDelegator
21
+
22
+ # @param [Element] thing that needs to be set i.e. Watir::Select, Watir::CheckBox etc...
23
+ def initialize(object)
24
+ __setobj__(object)
25
+ end
26
+
27
+ # @return [Element] subclasses use this to interact with original Elment wrapped by Dispacher
28
+ def original
29
+ __getobj__
30
+ end
31
+
32
+ # @return [Array<Option>] defaults to [] if original.options.empty?
33
+ def options
34
+ o = original.options
35
+ o.count == 0 ? [] : o
36
+ end
37
+
38
+ end
39
+
40
+ class Radio < Dispatcher
41
+
42
+ # because radio.set does not take args
43
+ def set value
44
+ original.set
45
+ end
46
+ end
47
+
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,49 @@
1
+ module Domkey
2
+ module View
3
+ module Widgetry
4
+
5
+ module Package
6
+
7
+ attr_accessor :package, :container
8
+
9
+ # initialize PageObject or PageObjectCollection
10
+ # for PageObject expects WebdriverElement a single element definition i.e text_field, checkbox
11
+ # for PageObjectCollection expects WebdriverElement a collection definition i.e. text_fields, checkboxes
12
+ # @param package [Proc(WebdriverElement)]
13
+ # @param package [PageObject]
14
+ # @param package [Hash{Symbol => Proc(WebdriverElement)]
15
+ # @param package [Hash{Symbol => PageObject]
16
+ def initialize package, container=lambda { Domkey.browser }
17
+ @container = container
18
+ @package = initialize_this package
19
+ end
20
+
21
+ # access widgetry of watir elements composing this page object
22
+ # @param [Symbol] (false)
23
+ # @return [Hash{Symbol => WebdriverElement}]
24
+ # @return [Element] raw element, i.e. Watir::Select, Watir::CheckBox (not wrapped with Dispatcher strategy)
25
+ def element(key=false)
26
+ return instantiator unless package.respond_to?(:each_pair)
27
+ return package.fetch(key).element if key
28
+ Hash[package.map { |key, package| [key, package.element] }]
29
+ end
30
+
31
+ private
32
+
33
+ # talks to the browser
34
+ # returns runtime element in a specified container
35
+ # @return [WebdriverElement]
36
+ def instantiator
37
+ container_instantiator.instance_exec(&package)
38
+ end
39
+
40
+ # talks to the browser
41
+ # returns runtime container element in a browser/driver
42
+ # @return [WebdriverElement]
43
+ def container_instantiator
44
+ container.respond_to?(:call) ? container.call : container.send(:instantiator)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -39,16 +39,20 @@ describe Domkey::View::CheckboxGroup do
39
39
 
40
40
  it 'initial value on test page' do
41
41
  @v.group.value.should eql ['other']
42
+ @v.group.value(:value, :index, :label).should eql [value: 'other', index: 2, label: 'Other']
42
43
  end
43
44
 
44
- it 'set value attribute by default. value returns array of value attribute' do
45
+ it 'set string' do
45
46
  @v.group.set 'tomato'
46
47
  @v.group.value.should eql ['tomato']
48
+ end
49
+
50
+ it 'set regexp' do
47
51
  @v.group.set /^othe/
48
52
  @v.group.value.should eql ['other']
49
53
  end
50
54
 
51
- it 'set array of value attribute. value returns array of value attribute' do
55
+ it 'set array of strings or regexp' do
52
56
  @v.group.set ['tomato']
53
57
  @v.group.value.should eql ['tomato']
54
58
 
@@ -56,12 +60,12 @@ describe Domkey::View::CheckboxGroup do
56
60
  @v.group.value.should eql ['tomato', 'other']
57
61
  end
58
62
 
59
- it 'set false clears all. value is empty array' do
63
+ it 'set false clears all' do
60
64
  @v.group.set false
61
65
  @v.group.value.should eql []
62
66
  end
63
67
 
64
- it 'set empty array clears all. value is empty array' do
68
+ it 'set empty array clears all' do
65
69
  @v.group.set []
66
70
  @v.group.value.should eql []
67
71
  end
@@ -74,8 +78,80 @@ describe Domkey::View::CheckboxGroup do
74
78
  expect { @v.group.set /balaba/ }.to raise_error
75
79
  end
76
80
 
77
- it 'options' do
81
+ it 'set by index single' do
82
+ @v.group.set index: 1
83
+ @v.group.value.should eql ['tomato']
84
+ end
85
+
86
+ it 'set by index array' do
87
+ @v.group.set index: [0, 2, 1]
88
+ @v.group.value.should eql ['cucumber', 'tomato', 'other']
89
+ end
90
+
91
+ it 'set by label string' do
92
+ @v.group.set label: 'Tomatorama'
93
+ @v.group.value.should eql ['tomato']
94
+ end
95
+
96
+ it 'set by label regexp' do
97
+ @v.group.set label: /umberama/
98
+ @v.group.value([:index, :value, :text, :label]).should eql [{:index=>0, :value=>"cucumber", :text=>"Cucumberama", :label=>"Cucumberama"}]
99
+ end
100
+
101
+
102
+ it 'set by index array string, regex' do
103
+ @v.group.set label: ['Cucumberama', /atorama/]
104
+ @v.group.value.should eql ['cucumber', 'tomato']
105
+ end
106
+
107
+
108
+ it 'value options single selected' do
109
+ @v.group.set [/tomat/]
110
+ @v.group.value.should eql ['tomato']
111
+
112
+ @v.group.value(:label).should eql [{:label=>"Tomatorama"}]
113
+ @v.group.value([:label]).should eql [{:label=>"Tomatorama"}]
114
+ @v.group.value(:label, :value, :index).should eql [{:label=>"Tomatorama", :value=>"tomato", :index=>1}]
115
+ end
116
+
117
+ it 'value options many selected' do
118
+ @v.group.set ['other', /tomat/, /cucum/]
119
+ @v.group.value.should eql ['cucumber', 'tomato', 'other']
120
+
121
+ @v.group.value(:label).should eql [{:label=>"Cucumberama"}, {:label=>"Tomatorama"}, {:label=>"Other"}]
122
+ @v.group.value(:label, :index, :value).should eql [{:label=>"Cucumberama", :index=>0, :value=>"cucumber"},
123
+ {:label=>"Tomatorama", :index=>1, :value=>"tomato"},
124
+ {:label=>"Other", :index=>2, :value=>"other"}]
125
+ end
126
+
127
+ it 'value options none selected' do
128
+ @v.group.set []
129
+ @v.group.value.should eql []
130
+ @v.group.value(:label).should eql []
131
+ @v.group.value(:label, :index, :value).should eql []
132
+ end
133
+
134
+ it 'options by default' do
78
135
  @v.group.options.should eql ["cucumber", "tomato", "other"]
79
136
  end
137
+
138
+ it 'options by opts single' do
139
+ @v.group.options(:value).should eql [{:value=>"cucumber"}, {:value=>"tomato"}, {:value=>"other"}]
140
+ @v.group.options([:value]).should eql [{:value=>"cucumber"}, {:value=>"tomato"}, {:value=>"other"}]
141
+ end
142
+
143
+ it 'options by label' do
144
+ @v.group.options(:label).should eql [{:label=>"Cucumberama"}, {:label=>"Tomatorama"}, {:label=>"Other"}]
145
+ @v.group.options([:label]).should eql [{:label=>"Cucumberama"}, {:label=>"Tomatorama"}, {:label=>"Other"}]
146
+ end
147
+
148
+ it 'options by opts many' do
149
+ expected = [{:value=>"cucumber", :index=>0, :label=>"Cucumberama", :text=>"Cucumberama"},
150
+ {:value=>"tomato", :index=>1, :label=>"Tomatorama", :text=>"Tomatorama"},
151
+ {:value=>"other", :index=>2, :label=>"Other", :text=>"Other"}]
152
+
153
+ @v.group.options(:value, :index, :label, :text).should eql expected
154
+ @v.group.options([:value, :index, :label, :text]).should eql expected
155
+ end
80
156
  end
81
157
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Domkey::View::OptionSelectable do
4
+
5
+ class OptionSelectableFaker
6
+ include Domkey::View::OptionSelectable
7
+ end
8
+
9
+ before :all do
10
+ @widget = OptionSelectableFaker.new
11
+ end
12
+
13
+ it 'set string' do
14
+ expect { @widget.set('fake') }.to raise_error(Domkey::Exception::NotImplementedError)
15
+ end
16
+
17
+ it 'set regex' do
18
+ expect { @widget.set(/fake/) }.to raise_error(Domkey::Exception::NotImplementedError)
19
+ end
20
+
21
+ it 'set :label' do
22
+ expect { @widget.set(:label => 'fake') }.to raise_error(Domkey::Exception::NotImplementedError)
23
+ end
24
+
25
+ it 'set :text' do
26
+ expect { @widget.set(:text => 'fake') }.to raise_error(Domkey::Exception::NotImplementedError)
27
+ end
28
+
29
+ it 'set :index' do
30
+ expect { @widget.set(:index => 3) }.to raise_error(Domkey::Exception::NotImplementedError)
31
+ end
32
+
33
+ it 'set :value' do
34
+ expect { @widget.set(:value => 'fake') }.to raise_error(Domkey::Exception::NotImplementedError)
35
+ end
36
+
37
+ it 'value default' do
38
+ expect { @widget.value }.to raise_error(Domkey::Exception::NotImplementedError)
39
+ end
40
+
41
+ it 'value option' do
42
+ expect { @widget.value(:foo) }.to raise_error(Domkey::Exception::NotImplementedError)
43
+ end
44
+
45
+ it 'options default' do
46
+ expect { @widget.options }.to raise_error(Domkey::Exception::NotImplementedError)
47
+ end
48
+
49
+ it 'options opt' do
50
+ expect { @widget.options(:foo) }.to raise_error(Domkey::Exception::NotImplementedError)
51
+ end
52
+
53
+ end
54
+
55
+
56
+
57
+
@@ -41,11 +41,19 @@ describe Domkey::View::RadioGroup do
41
41
 
42
42
  it 'initial value on test page' do
43
43
  @v.group.value.should eql ['other']
44
+ @v.group.value(:index, :value, :label, :text).should eql [{:index=>2, :value=>"other", :label=>"Other", :text=>"Other"}]
45
+ @v.group.value([:index, :value, :label, :text]).should eql [{:index=>2, :value=>"other", :label=>"Other", :text=>"Other"}]
46
+ @v.group.value([:index]).should eql [{:index=>2}]
47
+ @v.group.value([:value]).should eql [{:value=>'other'}]
44
48
  end
45
49
 
46
- it 'set value attribute by default. value returns that value attribute' do
50
+ it 'set string' do
47
51
  @v.group.set 'tomato'
48
52
  @v.group.value.should eql ['tomato']
53
+ @v.group.value(:value).should eql [{value: 'tomato'}]
54
+ end
55
+
56
+ it 'set regexp' do
49
57
  @v.group.set /^oth/
50
58
  @v.group.value.should eql ['other']
51
59
  end
@@ -56,14 +64,15 @@ describe Domkey::View::RadioGroup do
56
64
 
57
65
  @v.group.set ['other', 'tomato', /cucu/]
58
66
  @v.group.value.should eql ['cucumber']
67
+ @v.group.value([:index, :label]).should eql [{:index=>0, :label=>"Cucumber"}]
59
68
  end
60
69
 
61
- it 'set false has no effect. value is initial value on the page' do
70
+ it 'set false has no effect' do
62
71
  @v.group.set false
63
72
  @v.group.value.should eql ['other']
64
73
  end
65
74
 
66
- it 'set empty array clears all. value is empty array' do
75
+ it 'set empty array has no effect' do
67
76
  @v.group.set []
68
77
  @v.group.value.should eql ['other']
69
78
  end
@@ -76,10 +85,59 @@ describe Domkey::View::RadioGroup do
76
85
  expect { @v.group.set /balaba/ }.to raise_error
77
86
  end
78
87
 
79
- it 'options' do
88
+
89
+ it 'set by index single' do
90
+ @v.group.set index: 1
91
+ @v.group.value.should eql ['tomato']
92
+ end
93
+
94
+ it 'set by index array' do
95
+ @v.group.set index: [0, 2, 1]
96
+ @v.group.value.should eql ['tomato']
97
+ end
98
+
99
+ it 'set by label string' do
100
+ @v.group.set label: 'Tomato'
101
+ @v.group.value.should eql ['tomato']
102
+ @v.group.value(:text).should eql [{text: 'Tomato'}]
103
+ end
104
+
105
+ it 'set by label regexp' do
106
+ @v.group.set label: /umber/
107
+ @v.group.value.should eql ['cucumber']
108
+ end
109
+
110
+
111
+ it 'set by index array string, regex' do
112
+ @v.group.set label: ['Cucumber', /mato/]
113
+ @v.group.value.should eql ['tomato']
114
+ end
115
+
116
+ it 'options by default' do
80
117
  @v.group.options.should eql ["cucumber", "tomato", "other"]
81
118
  end
82
119
 
120
+ it 'options by opts single' do
121
+ @v.group.options(:value).should eql [{:value=>"cucumber"}, {:value=>"tomato"}, {:value=>"other"}]
122
+ @v.group.options([:value]).should eql [{:value=>"cucumber"}, {:value=>"tomato"}, {:value=>"other"}]
123
+ end
124
+
125
+ it 'options by label' do
126
+ expected = [{:label=>"Cucumber"}, {:label=>"Tomato"}, {:label=>"Other"}]
127
+ @v.group.options(:label).should eql expected
128
+ @v.group.options([:label]).should eql expected
129
+ end
130
+
131
+ it 'options by opts many' do
132
+ expected = [{:value=>"cucumber", :index=>0, :label=>"Cucumber", :text=>"Cucumber"},
133
+ {:value=>"tomato", :index=>1, :label=>"Tomato", :text=>"Tomato"},
134
+ {:value=>"other", :index=>2, :label=>"Other", :text=>"Other"}]
135
+
136
+ @v.group.options(:value, :index, :label, :text).should eql expected
137
+ @v.group.options([:value, :index, :label, :text]).should eql expected
138
+ end
139
+
140
+
83
141
  end
84
142
 
85
143
  end