forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_list_spec.rb
341 lines (278 loc) · 13.9 KB
/
select_list_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "SelectList" 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 select list exists" do
expect(browser.select_list(id: 'new_user_country')).to exist
expect(browser.select_list(id: /new_user_country/)).to exist
expect(browser.select_list(name: 'new_user_country')).to exist
expect(browser.select_list(name: /new_user_country/)).to exist
not_compliant_on :webdriver, :watir_classic do
expect(browser.select_list(text: 'Norway')).to exist
expect(browser.select_list(text: /Norway/)).to exist
end
expect(browser.select_list(class: 'country')).to exist
expect(browser.select_list(class: /country/)).to exist
expect(browser.select_list(index: 0)).to exist
expect(browser.select_list(xpath: "//select[@id='new_user_country']")).to exist
end
it "returns the first select if given no args" do
expect(browser.select_list).to exist
end
it "returns false if the select list doesn't exist" do
expect(browser.select_list(id: 'no_such_id')).to_not exist
expect(browser.select_list(id: /no_such_id/)).to_not exist
expect(browser.select_list(name: 'no_such_name')).to_not exist
expect(browser.select_list(name: /no_such_name/)).to_not exist
expect(browser.select_list(value: 'no_such_value')).to_not exist
expect(browser.select_list(value: /no_such_value/)).to_not exist
expect(browser.select_list(text: 'no_such_text')).to_not exist
expect(browser.select_list(text: /no_such_text/)).to_not exist
expect(browser.select_list(class: 'no_such_class')).to_not exist
expect(browser.select_list(class: /no_such_class/)).to_not exist
expect(browser.select_list(index: 1337)).to_not exist
expect(browser.select_list(xpath: "//select[@id='no_such_id']")).to_not exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.select_list(id: 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.select_list(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 of the select list" do
expect(browser.select_list(name: 'new_user_country').class_name).to eq 'country'
end
it "raises UnknownObjectException if the select list doesn't exist" do
expect { browser.select_list(name: 'no_such_name').class_name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#id" do
it "returns the id of the element" do
expect(browser.select_list(index: 0).id).to eq "new_user_country"
end
it "raises UnknownObjectException if the select list doesn't exist" do
expect { browser.select_list(index: 1337).id }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#name" do
it "returns the name of the element" do
expect(browser.select_list(index: 0).name).to eq "new_user_country"
end
it "raises UnknownObjectException if the select list doesn't exist" do
expect { browser.select_list(index: 1337).name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#multiple?" do
it "knows whether the select list allows multiple selections" do
expect(browser.select_list(index: 0)).to_not be_multiple
expect(browser.select_list(index: 1)).to be_multiple
end
it "raises UnknownObjectException if the select list doesn't exist" do
expect { browser.select_list(index: 1337).multiple? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#value" do
it "returns the value of the selected option" do
expect(browser.select_list(index: 0).value).to eq "2"
browser.select_list(index: 0).select(/Sweden/)
expect(browser.select_list(index: 0).value).to eq "3"
end
it "raises UnknownObjectException if the select list doesn't exist" do
expect { browser.select_list(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.select_list(index: 0)).to respond_to(:class_name)
expect(browser.select_list(index: 0)).to respond_to(:id)
expect(browser.select_list(index: 0)).to respond_to(:name)
expect(browser.select_list(index: 0)).to respond_to(:value)
end
end
# Access methods
describe "#enabled?" do
it "returns true if the select list is enabled" do
expect(browser.select_list(name: 'new_user_country')).to be_enabled
end
it "returns false if the select list is disabled" do
expect(browser.select_list(name: 'new_user_role')).to_not be_enabled
end
it "raises UnknownObjectException if the select_list doesn't exist" do
expect { browser.select_list(name: 'no_such_name').enabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#disabled?" do
it "returns true if the select list is disabled" do
expect(browser.select_list(index: 2)).to be_disabled
end
it "returns false if the select list is enabled" do
expect(browser.select_list(index: 0)).to_not be_disabled
end
it "should raise UnknownObjectException when the select list does not exist" do
expect { browser.select_list(index: 1337).disabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
# Other
describe "#option" do
it "returns an instance of Option" do
option = browser.select_list(name: "new_user_country").option(text: "Denmark")
expect(option).to be_instance_of(Option)
expect(option.value).to eq "1"
end
end
describe "#options" do
it "returns all the options" do
options = browser.select_list(name: "new_user_country").options
expect(options.map(&:text)).to eq ["Denmark", "Norway", "Sweden", "United Kingdom", "USA", "Germany"]
end
end
describe "#selected_options" do
not_compliant_on :safariwatir do
it "should raise UnknownObjectException if the select list doesn't exist" do
expect { browser.select_list(name: 'no_such_name').selected_options }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
it "gets the currently selected item(s)" do
expect(browser.select_list(name: "new_user_country").selected_options.map(&:text)).to eq ["Norway"]
expect(browser.select_list(name: "new_user_languages").selected_options.map(&:text)).to eq ["English", "Norwegian"]
end
end
describe "#clear" do
it "clears the selection when possible" do
browser.select_list(name: "new_user_languages").clear
expect(browser.select_list(name: "new_user_languages").selected_options).to be_empty
end
it "does not clear selections if the select list does not allow multiple selections" do
expect {
browser.select_list(name: "new_user_country").clear
}.to raise_error(/you can only clear multi-selects/)
expect(browser.select_list(name: "new_user_country").selected_options.map(&:text)).to eq ["Norway"]
end
it "raises UnknownObjectException if the select list doesn't exist" do
expect { browser.select_list(name: 'no_such_name').clear }.to raise_error(Watir::Exception::UnknownObjectException)
end
it "fires onchange event" do
browser.select_list(name: "new_user_languages").clear
expect(messages.size).to eq 2
end
it "doesn't fire onchange event for already cleared option" do
browser.select_list(name: "new_user_languages").option.clear
expect(messages.size).to eq 0
end
end
describe "#include?" do
it "returns true if the given option exists" do
expect(browser.select_list(name: 'new_user_country')).to include('Denmark')
end
it "returns false if the given option doesn't exist" do
expect(browser.select_list(name: 'new_user_country')).to_not include('Ireland')
end
end
describe "#selected?" do
it "returns true if the given option is selected" do
browser.select_list(name: 'new_user_country').select('Denmark')
expect(browser.select_list(name: 'new_user_country')).to be_selected('Denmark')
end
it "returns false if the given option is not selected" do
expect(browser.select_list(name: 'new_user_country')).to_not be_selected('Sweden')
end
it "raises UnknownObjectException if the option doesn't exist" do
expect { browser.select_list(name: 'new_user_country').selected?('missing_option') }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#select" do
it "selects the given item when given a String" do
browser.select_list(name: "new_user_country").select("Denmark")
expect(browser.select_list(name: "new_user_country").selected_options.map(&:text)).to eq ["Denmark"]
end
it "selects options by label" do
browser.select_list(name: "new_user_country").select("Germany")
expect(browser.select_list(name: "new_user_country").selected_options.map(&:text)).to eq ["Germany"]
end
it "selects the given item when given a Regexp" do
browser.select_list(name: "new_user_country").select(/Denmark/)
expect(browser.select_list(name: "new_user_country").selected_options.map(&:text)).to eq ["Denmark"]
end
it "selects the given item when given an Xpath" do
browser.select_list(xpath: "//select[@name='new_user_country']").select("Denmark")
expect(browser.select_list(xpath: "//select[@name='new_user_country']").selected_options.map(&:text)).to eq ["Denmark"]
end
it "selects multiple items using :name and a String" do
browser.select_list(name: "new_user_languages").clear
browser.select_list(name: "new_user_languages").select("Danish")
browser.select_list(name: "new_user_languages").select("Swedish")
expect(browser.select_list(name: "new_user_languages").selected_options.map(&:text)).to eq ["Danish", "Swedish"]
end
it "selects multiple items using :name and a Regexp" do
browser.select_list(name: "new_user_languages").clear
browser.select_list(name: "new_user_languages").select(/ish/)
expect(browser.select_list(name: "new_user_languages").selected_options.map(&:text)).to eq ["Danish", "English", "Swedish"]
end
it "selects multiple items using :xpath" do
browser.select_list(xpath: "//select[@name='new_user_languages']").clear
browser.select_list(xpath: "//select[@name='new_user_languages']").select(/ish/)
expect(browser.select_list(xpath: "//select[@name='new_user_languages']").selected_options.map(&:text)).to eq ["Danish", "English", "Swedish"]
end
it "selects empty options" do
browser.select_list(id: "delete_user_username").select("")
expect(browser.select_list(id: "delete_user_username").selected_options.map(&:text)).to eq [""]
end
it "returns the value selected" do
expect(browser.select_list(name: "new_user_languages").select("Danish")).to eq "Danish"
end
it "returns the first matching value if there are multiple matches" do
expect(browser.select_list(name: "new_user_languages").select(/ish/)).to eq "Danish"
end
it "fires onchange event when selecting an item" do
browser.select_list(id: "new_user_languages").select("Danish")
expect(messages).to eq ['changed language']
end
it "doesn't fire onchange event when selecting an already selected item" do
browser.select_list(id: "new_user_languages").clear # removes the two pre-selected options
browser.select_list(id: "new_user_languages").select("English")
expect(messages.size).to eq 3
browser.select_list(id: "new_user_languages").select("English")
expect(messages.size).to eq 3
end
it "returns the text of the selected option" do
expect(browser.select_list(id: "new_user_languages").select("English")).to eq "English"
end
it "returns an empty string when selecting an option that disappears when selected" do
expect(browser.select_list(id: 'obsolete').select('sweden')).to eq ''
end
it "selects options with a single-quoted value" do
browser.select_list(id: 'single-quote').select("'foo'")
end
it "raises NoValueFoundException if the option doesn't exist" do
expect { browser.select_list(name: "new_user_country").select("missing_option") }.to raise_error(Watir::Exception::NoValueFoundException)
expect { browser.select_list(name: "new_user_country").select(/missing_option/) }.to raise_error(Watir::Exception::NoValueFoundException)
end
it "raises a TypeError if argument is not a String, Regexp or Numeric" do
expect { browser.select_list(id: "new_user_languages").select([]) }.to raise_error(TypeError)
end
end
# deprecate?
describe "#select_value" do
it "selects the item by value string" do
browser.select_list(name: "new_user_languages").clear
browser.select_list(name: "new_user_languages").select_value("2")
expect(browser.select_list(name: "new_user_languages").selected_options.map(&:text)).to eq %w[English]
end
it "selects the items by value regexp" do
browser.select_list(name: "new_user_languages").clear
browser.select_list(name: "new_user_languages").select_value(/1|3/)
expect(browser.select_list(name: "new_user_languages").selected_options.map(&:text)).to eq %w[Danish Norwegian]
end
it "raises NoValueFoundException if the option doesn't exist" do
expect { browser.select_list(name: "new_user_languages").select_value("no_such_option") }.to raise_error(Watir::Exception::NoValueFoundException)
expect { browser.select_list(name: "new_user_languages").select_value(/no_such_option/) }.to raise_error(Watir::Exception::NoValueFoundException)
end
end
end