forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkbox_spec.rb
280 lines (227 loc) · 12.1 KB
/
checkbox_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
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "CheckBox" 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 checkbox button exists" do
expect(browser.checkbox(id: "new_user_interests_books")).to exist
expect(browser.checkbox(id: /new_user_interests_books/)).to exist
expect(browser.checkbox(name: "new_user_interests")).to exist
expect(browser.checkbox(name: /new_user_interests/)).to exist
expect(browser.checkbox(value: "books")).to exist
expect(browser.checkbox(value: /books/)).to exist
# not sure what :text is supposed to represent here
# browser.checkbox(text: "books").to exist
# browser.checkbox(text: /books/).to exist
expect(browser.checkbox(class: "fun")).to exist
expect(browser.checkbox(class: /fun/)).to exist
expect(browser.checkbox(index: 0)).to exist
expect(browser.checkbox(xpath: "//input[@id='new_user_interests_books']")).to exist
end
it "returns true if the checkbox button exists (search by name and value)" do
expect(browser.checkbox(name: "new_user_interests", value: 'cars')).to exist
browser.checkbox(xpath: "//input[@name='new_user_interests' and @value='cars']").set
end
it "returns the first checkbox if given no args" do
expect(browser.checkbox).to exist
end
it "returns false if the checkbox button does not exist" do
expect(browser.checkbox(id: "no_such_id")).to_not exist
expect(browser.checkbox(id: /no_such_id/)).to_not exist
expect(browser.checkbox(name: "no_such_name")).to_not exist
expect(browser.checkbox(name: /no_such_name/)).to_not exist
expect(browser.checkbox(value: "no_such_value")).to_not exist
expect(browser.checkbox(value: /no_such_value/)).to_not exist
expect(browser.checkbox(text: "no_such_text")).to_not exist
expect(browser.checkbox(text: /no_such_text/)).to_not exist
expect(browser.checkbox(class: "no_such_class")).to_not exist
expect(browser.checkbox(class: /no_such_class/)).to_not exist
expect(browser.checkbox(index: 1337)).to_not exist
expect(browser.checkbox(xpath: "//input[@id='no_such_id']")).to_not exist
end
it "returns false if the checkbox button does not exist (search by name and value)" do
expect(browser.checkbox(name: "new_user_interests", value: 'no_such_value')).to_not exist
expect(browser.checkbox(xpath: "//input[@name='new_user_interests' and @value='no_such_value']")).to_not exist
expect(browser.checkbox(name: "no_such_name", value: 'cars')).to_not exist
expect(browser.checkbox(xpath: "//input[@name='no_such_name' and @value='cars']")).to_not exist
end
it "returns true for checkboxes with a string value" do
expect(browser.checkbox(name: 'new_user_interests', value: 'books')).to exist
expect(browser.checkbox(name: 'new_user_interests', value: 'cars')).to exist
end
it "returns true for checkbox with upper case type" do
expect(browser.checkbox(id: "new_user_interests_draw")).to exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.checkbox(id: 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.checkbox(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 checkbox exists and has an attribute" do
expect(browser.checkbox(id: "new_user_interests_dancing").class_name).to eq "fun"
end
it "returns an empty string if the checkbox exists and the attribute doesn't" do
expect(browser.checkbox(id: "new_user_interests_books").class_name).to eq ""
end
it "raises UnknownObjectException if the checkbox doesn't exist" do
expect { browser.checkbox(id: "no_such_id").class_name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#id" do
it "returns the id attribute if the checkbox exists and has an attribute" do
expect(browser.checkbox(index: 0).id).to eq "new_user_interests_books"
end
it "returns an empty string if the checkbox exists and the attribute doesn't" do
expect(browser.checkbox(index: 1).id).to eq ""
end
it "raises UnknownObjectException if the checkbox doesn't exist" do
expect { browser.checkbox(index: 1337).id }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#name" do
it "returns the name attribute if the checkbox exists" do
expect(browser.checkbox(id: 'new_user_interests_books').name).to eq "new_user_interests"
end
it "returns an empty string if the checkbox exists and the attribute doesn't" do
expect(browser.checkbox(id: 'new_user_interests_food').name).to eq ""
end
it "raises UnknownObjectException if the checkbox doesn't exist" do
expect { browser.checkbox(index: 1337).name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#title" do
it "returns the title attribute if the checkbox exists" do
expect(browser.checkbox(id: "new_user_interests_dancing").title).to eq "Dancing is fun!"
end
it "returns an empty string if the checkbox exists and the attribute doesn't" do
expect(browser.checkbox(id: "new_user_interests_books").title).to eq ""
end
it "raises UnknownObjectException if the checkbox doesn't exist" do
expect { browser.checkbox(index: 1337).title }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#type" do
it "returns the type attribute if the checkbox exists" do
expect(browser.checkbox(index: 0).type).to eq "checkbox"
end
it "raises UnknownObjectException if the checkbox doesn't exist" do
expect { browser.checkbox(index: 1337).type }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#value" do
it "returns the value attribute if the checkbox exists" do
expect(browser.checkbox(id: 'new_user_interests_books').value).to eq 'books'
end
it "raises UnknownObjectException if the checkbox doesn't exist" do
expect { browser.checkbox(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.checkbox(index: 0)).to respond_to(:class_name)
expect(browser.checkbox(index: 0)).to respond_to(:id)
expect(browser.checkbox(index: 0)).to respond_to(:name)
expect(browser.checkbox(index: 0)).to respond_to(:title)
expect(browser.checkbox(index: 0)).to respond_to(:type)
expect(browser.checkbox(index: 0)).to respond_to(:value)
end
end
# Access methods
describe "#enabled?" do
it "returns true if the checkbox button is enabled" do
expect(browser.checkbox(id: "new_user_interests_books")).to be_enabled
expect(browser.checkbox(xpath: "//input[@id='new_user_interests_books']")).to be_enabled
end
it "returns false if the checkbox button is disabled" do
expect(browser.checkbox(id: "new_user_interests_dentistry")).to_not be_enabled
expect(browser.checkbox(:xpath,"//input[@id='new_user_interests_dentistry']")).to_not be_enabled
end
it "raises UnknownObjectException if the checkbox button doesn't exist" do
expect { browser.checkbox(id: "no_such_id").enabled? }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.checkbox(xpath: "//input[@id='no_such_id']").enabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#disabled?" do
it "returns true if the checkbox is disabled" do
expect(browser.checkbox(id: 'new_user_interests_dentistry')).to be_disabled
end
it "returns false if the checkbox is enabled" do
expect(browser.checkbox(id: 'new_user_interests_books')).to_not be_disabled
end
it "raises UnknownObjectException if the checkbox doesn't exist" do
expect { browser.checkbox(index: 1337).disabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
# Manipulation methods
describe "#clear" do
it "raises ObjectDisabledException if the checkbox is disabled" do
expect(browser.checkbox(id: "new_user_interests_dentistry")).to_not be_set
expect { browser.checkbox(id: "new_user_interests_dentistry").clear }.to raise_error(Watir::Exception::ObjectDisabledException)
expect { browser.checkbox(xpath: "//input[@id='new_user_interests_dentistry']").clear }.to raise_error(Watir::Exception::ObjectDisabledException)
end
it "clears the checkbox button if it is set" do
browser.checkbox(id: "new_user_interests_books").clear
expect(browser.checkbox(id: "new_user_interests_books")).to_not be_set
end
it "clears the checkbox button when found by :xpath" do
browser.checkbox(xpath: "//input[@id='new_user_interests_books']").clear
expect(browser.checkbox(xpath: "//input[@id='new_user_interests_books']")).to_not be_set
end
it "raises UnknownObjectException if the checkbox button doesn't exist" do
expect { browser.checkbox(name: "no_such_id").clear }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.checkbox(xpath: "//input[@id='no_such_id']").clear }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#set" do
it "sets the checkbox button" do
browser.checkbox(id: "new_user_interests_cars").set
expect(browser.checkbox(id: "new_user_interests_cars")).to be_set
end
it "sets the checkbox button when found by :xpath" do
browser.checkbox(xpath: "//input[@id='new_user_interests_cars']").set
expect(browser.checkbox(xpath: "//input[@id='new_user_interests_cars']")).to be_set
end
it "fires the onclick event" do
expect(browser.button(id: "disabled_button")).to be_disabled
browser.checkbox(id: "toggle_button_checkbox").set
expect(browser.button(id: "disabled_button")).to_not be_disabled
browser.checkbox(id: "toggle_button_checkbox").clear
expect(browser.button(id: "disabled_button")).to be_disabled
end
it "raises UnknownObjectException if the checkbox button doesn't exist" do
expect { browser.checkbox(name: "no_such_name").set }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.checkbox(xpath: "//input[@name='no_such_name']").set }.to raise_error(Watir::Exception::UnknownObjectException)
end
it "raises ObjectDisabledException if the checkbox is disabled" do
expect { browser.checkbox(id: "new_user_interests_dentistry").set }.to raise_error(Watir::Exception::ObjectDisabledException)
expect { browser.checkbox(xpath: "//input[@id='new_user_interests_dentistry']").set }.to raise_error(Watir::Exception::ObjectDisabledException )
end
end
# Other
describe '#set?' do
it "returns true if the checkbox button is set" do
expect(browser.checkbox(id: "new_user_interests_books")).to be_set
end
it "returns false if the checkbox button unset" do
expect(browser.checkbox(id: "new_user_interests_cars")).to_not be_set
end
it "returns the state for checkboxes with string values" do
expect(browser.checkbox(name: "new_user_interests", value: 'cars')).to_not be_set
browser.checkbox(name: "new_user_interests", value: 'cars').set
expect(browser.checkbox(name: "new_user_interests", value: 'cars')).to be_set
browser.checkbox(name: "new_user_interests", value: 'cars').clear
expect(browser.checkbox(name: "new_user_interests", value: 'cars')).to_not be_set
end
it "raises UnknownObjectException if the checkbox button doesn't exist" do
expect { browser.checkbox(id: "no_such_id").set? }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.checkbox(xpath: "//input[@id='no_such_id']").set? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
end