This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
radio_spec.rb
264 lines (217 loc) · 11 KB
/
radio_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "Radio" do
before :each do
browser.goto(WatirSpec.url_for("forms_with_input_elements.html"))
end
# Exists method
describe "#exists?" do
it "returns true if the radio button exists" do
expect(browser.radio(id: "new_user_newsletter_yes")).to exist
expect(browser.radio(id: /new_user_newsletter_yes/)).to exist
expect(browser.radio(name: "new_user_newsletter")).to exist
expect(browser.radio(name: /new_user_newsletter/)).to exist
expect(browser.radio(value: "yes")).to exist
expect(browser.radio(value: /yes/)).to exist
# TODO: figure out what :text means for a radio button
# browser.radio(text: "yes").to exist
# browser.radio(text: /yes/).to exist
expect(browser.radio(class: "huge")).to exist
expect(browser.radio(class: /huge/)).to exist
expect(browser.radio(index: 0)).to exist
expect(browser.radio(xpath: "//input[@id='new_user_newsletter_yes']")).to exist
end
it "returns the first radio if given no args" do
expect(browser.radio).to exist
end
it "returns true if the radio button exists (search by name and value)" do
expect(browser.radio(name: "new_user_newsletter", value: 'yes')).to exist
browser.radio(xpath: "//input[@name='new_user_newsletter' and @value='yes']").set
end
it "returns true for element with upper case type" do
expect(browser.radio(id: "new_user_newsletter_probably")).to exist
end
it "returns false if the radio button does not exist" do
expect(browser.radio(id: "no_such_id")).to_not exist
expect(browser.radio(id: /no_such_id/)).to_not exist
expect(browser.radio(name: "no_such_name")).to_not exist
expect(browser.radio(name: /no_such_name/)).to_not exist
expect(browser.radio(value: "no_such_value")).to_not exist
expect(browser.radio(value: /no_such_value/)).to_not exist
expect(browser.radio(text: "no_such_text")).to_not exist
expect(browser.radio(text: /no_such_text/)).to_not exist
expect(browser.radio(class: "no_such_class")).to_not exist
expect(browser.radio(class: /no_such_class/)).to_not exist
expect(browser.radio(index: 1337)).to_not exist
expect(browser.radio(xpath: "input[@id='no_such_id']")).to_not exist
end
it "returns false if the radio button does not exist (search by name and value)" do
expect(browser.radio(name: "new_user_newsletter", value: 'no_such_value')).to_not exist
expect(browser.radio(xpath: "//input[@name='new_user_newsletter' and @value='no_such_value']")).to_not exist
expect(browser.radio(name: "no_such_name", value: 'yes')).to_not exist
expect(browser.radio(xpath: "//input[@name='no_such_name' and @value='yes']")).to_not exist
end
it "returns true for radios with a string value" do
expect(browser.radio(name: 'new_user_newsletter', value: 'yes')).to exist
expect(browser.radio(name: 'new_user_newsletter', value: 'no')).to exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.radio(id: 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.radio(no_such_how: 'some_value').exists? }.to raise_error(Watir::Exception::MissingWayOfFindingObjectException)
end
end
# Attribute methods
describe "#class_name" do
it "returns the class name if the radio exists and has an attribute" do
expect(browser.radio(id: "new_user_newsletter_yes").class_name).to eq "huge"
end
it "returns an empty string if the radio exists and the attribute doesn't" do
expect(browser.radio(id: "new_user_newsletter_no").class_name).to eq ""
end
it "raises UnknownObjectException if the radio doesn't exist" do
expect { browser.radio(id: "no_such_id").class_name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#id" do
it "returns the id attribute if the radio exists and has an attribute" do
expect(browser.radio(index: 0).id).to eq "new_user_newsletter_yes"
end
it "returns an empty string if the radio exists and the attribute doesn't" do
expect(browser.radio(index: 2).id).to eq ""
end
it "raises UnknownObjectException if the radio doesn't exist" do
expect { browser.radio(index: 1337).id }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#name" do
it "returns the name attribute if the radio exists" do
expect(browser.radio(id: 'new_user_newsletter_yes').name).to eq "new_user_newsletter"
end
it "returns an empty string if the radio exists and the attribute doesn't" do
expect(browser.radio(id: 'new_user_newsletter_absolutely').name).to eq ""
end
it "raises UnknownObjectException if the radio doesn't exist" do
expect { browser.radio(index: 1337).name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#title" do
it "returns the title attribute if the radio exists" do
expect(browser.radio(id: "new_user_newsletter_no").title).to eq "Traitor!"
end
it "returns an empty string if the radio exists and the attribute doesn't" do
expect(browser.radio(id: "new_user_newsletter_yes").title).to eq ""
end
it "raises UnknownObjectException if the radio doesn't exist" do
expect { browser.radio(index: 1337).title }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#type" do
it "returns the type attribute if the radio exists" do
expect(browser.radio(index: 0).type).to eq "radio"
end
it "raises UnknownObjectException if the radio doesn't exist" do
expect { browser.radio(index: 1337).type }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#value" do
it "returns the value attribute if the radio exists" do
expect(browser.radio(id: 'new_user_newsletter_yes').value).to eq 'yes'
end
it "raises UnknownObjectException if the radio doesn't exist" do
expect { browser.radio(index: 1337).value}.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#respond_to?" do
it "returns true for all attribute methods" do
expect(browser.radio(index: 0)).to respond_to(:class_name)
expect(browser.radio(index: 0)).to respond_to(:id)
expect(browser.radio(index: 0)).to respond_to(:name)
expect(browser.radio(index: 0)).to respond_to(:title)
expect(browser.radio(index: 0)).to respond_to(:type)
expect(browser.radio(index: 0)).to respond_to(:value)
end
end
# Access methods
describe "#enabled?" do
it "returns true if the radio button is enabled" do
expect(browser.radio(id: "new_user_newsletter_yes")).to be_enabled
expect(browser.radio(xpath: "//input[@id='new_user_newsletter_yes']")).to be_enabled
end
it "returns false if the radio button is disabled" do
expect(browser.radio(id: "new_user_newsletter_nah")).to_not be_enabled
expect(browser.radio(xpath: "//input[@id='new_user_newsletter_nah']")).to_not be_enabled
end
it "raises UnknownObjectException if the radio button doesn't exist" do
expect { browser.radio(id: "no_such_id").enabled? }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.radio(xpath: "//input[@id='no_such_id']").enabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#disabled?" do
it "returns true if the radio is disabled" do
expect(browser.radio(id: 'new_user_newsletter_nah')).to be_disabled
end
it "returns false if the radio is enabled" do
expect(browser.radio(id: 'new_user_newsletter_yes')).to_not be_disabled
end
it "raises UnknownObjectException if the radio doesn't exist" do
expect { browser.radio(index: 1337).disabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#set" do
it "sets the radio button" do
browser.radio(id: "new_user_newsletter_no").set
expect(browser.radio(id: "new_user_newsletter_no")).to be_set
end
it "sets the radio button when found by :xpath" do
browser.radio(xpath: "//input[@id='new_user_newsletter_no']").set
expect(browser.radio(xpath: "//input[@id='new_user_newsletter_no']")).to be_set
end
it "fires the onclick event" do
browser.radio(id: "new_user_newsletter_no").set
browser.radio(id: "new_user_newsletter_yes").set
expect(messages).to eq ["clicked: new_user_newsletter_no", "clicked: new_user_newsletter_yes"]
end
# http://webbugtrack.blogspot.com/2007/11/bug-193-onchange-does-not-fire-properly.html
not_compliant_on :internet_explorer do
it "fires the onchange event" do
browser.radio(value: 'certainly').set
expect(messages).to eq ["changed: new_user_newsletter"]
browser.radio(value: 'certainly').set
expect(messages).to eq ["changed: new_user_newsletter"] # no event fired here - didn't change
browser.radio(value: 'yes').set
browser.radio(value: 'certainly').set
expect(messages).to eq ["changed: new_user_newsletter", "clicked: new_user_newsletter_yes", "changed: new_user_newsletter"]
end
end
it "raises UnknownObjectException if the radio button doesn't exist" do
expect { browser.radio(name: "no_such_name").set }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.radio(xpath: "//input[@name='no_such_name']").set }.to raise_error(Watir::Exception::UnknownObjectException)
end
it "raises ObjectDisabledException if the radio is disabled" do
expect { browser.radio(id: "new_user_newsletter_nah").set }.to raise_error(Watir::Exception::ObjectDisabledException)
expect { browser.radio(xpath: "//input[@id='new_user_newsletter_nah']").set }.to raise_error(Watir::Exception::ObjectDisabledException )
end
end
# Other
describe '#set?' do
it "returns true if the radio button is set" do
expect(browser.radio(id: "new_user_newsletter_yes")).to be_set
end
it "returns false if the radio button unset" do
expect(browser.radio(id: "new_user_newsletter_no")).to_not be_set
end
it "returns the state for radios with string values" do
expect(browser.radio(name: "new_user_newsletter", value: 'no')).to_not be_set
browser.radio(name: "new_user_newsletter", value: 'no').set
expect(browser.radio(name: "new_user_newsletter", value: 'no')).to be_set
browser.radio(name: "new_user_newsletter", value: 'yes').set
expect(browser.radio(name: "new_user_newsletter", value: 'no')).to_not be_set
end
it "raises UnknownObjectException if the radio button doesn't exist" do
expect { browser.radio(id: "no_such_id").set? }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.radio(xpath: "//input[@id='no_such_id']").set? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
end