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
/
text_field_spec.rb
303 lines (249 loc) · 12.3 KB
/
text_field_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
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "TextField" 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 element exists" do
expect(browser.text_field(id: 'new_user_email')).to exist
expect(browser.text_field(id: /new_user_email/)).to exist
expect(browser.text_field(name: 'new_user_email')).to exist
expect(browser.text_field(name: /new_user_email/)).to exist
expect(browser.text_field(value: 'Developer')).to exist
expect(browser.text_field(value: /Developer/)).to exist
expect(browser.text_field(text: 'Developer')).to exist
expect(browser.text_field(text: /Developer/)).to exist
expect(browser.text_field(class: 'name')).to exist
expect(browser.text_field(class: /name/)).to exist
expect(browser.text_field(index: 0)).to exist
expect(browser.text_field(xpath: "//input[@id='new_user_email']")).to exist
expect(browser.text_field(label: "First name")).to exist
expect(browser.text_field(label: /(Last|First) name/)).to exist
expect(browser.text_field(label: 'Without for')).to exist
expect(browser.text_field(label: /Without for/)).to exist
end
it "returns the first text field if given no args" do
expect(browser.text_field).to exist
end
it "respects text fields types" do
expect(browser.text_field.type).to eq('text')
end
it "returns true if the element exists (no type attribute)" do
expect(browser.text_field(id: 'new_user_first_name')).to exist
end
it "returns true if the element exists (invalid type attribute)" do
expect(browser.text_field(id: 'new_user_last_name')).to exist
end
it "returns true for element with upper case type" do
expect(browser.text_field(id: "new_user_email_confirm")).to exist
end
it "returns true for element with unknown type attribute" do
expect(browser.text_field(id: "unknown_text_field")).to exist
end
it "returns false if the element does not exist" do
expect(browser.text_field(id: 'no_such_id')).to_not exist
expect(browser.text_field(id: /no_such_id/)).to_not exist
expect(browser.text_field(name: 'no_such_name')).to_not exist
expect(browser.text_field(name: /no_such_name/)).to_not exist
expect(browser.text_field(value: 'no_such_value')).to_not exist
expect(browser.text_field(value: /no_such_value/)).to_not exist
expect(browser.text_field(text: 'no_such_text')).to_not exist
expect(browser.text_field(text: /no_such_text/)).to_not exist
expect(browser.text_field(class: 'no_such_class')).to_not exist
expect(browser.text_field(class: /no_such_class/)).to_not exist
expect(browser.text_field(index: 1337)).to_not exist
expect(browser.text_field(xpath: "//input[@id='no_such_id']")).to_not exist
expect(browser.text_field(label: "bad label")).to_not exist
expect(browser.text_field(label: /bad label/)).to_not exist
# input type='hidden' should not be found by #text_field
expect(browser.text_field(id: "new_user_interests_dolls")).to_not exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.text_field(id: 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.text_field(no_such_how: 'some_value').exists? }.to raise_error(Watir::Exception::MissingWayOfFindingObjectException)
end
end
# Attribute methods
describe "#id" do
it "returns the id attribute if the text field exists" do
expect(browser.text_field(index: 4).id).to eq "new_user_occupation"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(index: 1337).id }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#name" do
it "returns the name attribute if the text field exists" do
expect(browser.text_field(index: 3).name).to eq "new_user_email_confirm"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(index: 1337).name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#title" do
it "returns the title attribute if the text field exists" do
expect(browser.text_field(id: "new_user_code").title).to eq "Your personal code"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(index: 1337).title }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#type" do
it "returns the type attribute if the text field exists" do
expect(browser.text_field(index: 3).type).to eq "text"
end
it "returns 'text' if the type attribute is invalid" do
expect(browser.text_field(id: 'new_user_last_name').type).to eq "text"
end
it "returns 'text' if the type attribute does not exist" do
expect(browser.text_field(id: 'new_user_first_name').type).to eq "text"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(index: 1337).type }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#value" do
it "returns the value attribute if the text field exists" do
expect(browser.text_field(name: "new_user_occupation").value).to eq "Developer"
expect(browser.text_field(index: 4).value).to eq "Developer"
expect(browser.text_field(name: /new_user_occupation/i).value).to eq "Developer"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(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.text_field(index: 0)).to respond_to(:class_name)
expect(browser.text_field(index: 0)).to respond_to(:id)
expect(browser.text_field(index: 0)).to respond_to(:name)
expect(browser.text_field(index: 0)).to respond_to(:title)
expect(browser.text_field(index: 0)).to respond_to(:type)
expect(browser.text_field(index: 0)).to respond_to(:value)
end
end
# Access methods
describe "#enabled?" do
it "returns true for enabled text fields" do
expect(browser.text_field(name: "new_user_occupation")).to be_enabled
expect(browser.text_field(id: "new_user_email")).to be_enabled
end
it "returns false for disabled text fields" do
expect(browser.text_field(name: "new_user_species")).to_not be_enabled
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(id: "no_such_id").enabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#disabled?" do
it "returns true if the text field is disabled" do
expect(browser.text_field(id: 'new_user_species')).to be_disabled
end
it "returns false if the text field is enabled" do
expect(browser.text_field(index: 0)).to_not be_disabled
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(index: 1337).disabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#readonly?" do
bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1200366", :firefox do
it "returns true for read-only text fields" do
expect(browser.text_field(name: "new_user_code")).to be_readonly
expect(browser.text_field(id: "new_user_code")).to be_readonly
end
end
it "returns false for writable text fields" do
expect(browser.text_field(name: "new_user_email")).to_not be_readonly
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(id: 'no_such_id').readonly? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
# Manipulation methods
describe "#append" do
it "appends the text to the text field" do
browser.text_field(name: "new_user_occupation").append(" Append This")
expect(browser.text_field(name: "new_user_occupation").value).to eq "Developer Append This"
end
it "appends multi-byte characters" do
browser.text_field(name: "new_user_occupation").append(" ijij")
expect(browser.text_field(name: "new_user_occupation").value).to eq "Developer ijij"
end
it "raises ObjectReadOnlyException if the object is read only" do
expect { browser.text_field(id: "new_user_code").append("Append This") }.to raise_error(Watir::Exception::ObjectReadOnlyException)
end
it "raises ObjectDisabledException if the object is disabled" do
expect { browser.text_field(name: "new_user_species").append("Append This") }.to raise_error(Watir::Exception::ObjectDisabledException)
end
it "raises UnknownObjectException if the object doesn't exist" do
expect { browser.text_field(name: "no_such_name").append("Append This") }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#clear" do
it "removes all text from the text field" do
browser.text_field(name: "new_user_occupation").clear
expect(browser.text_field(name: "new_user_occupation").value).to be_empty
browser.textarea(id: "delete_user_comment").clear
expect(browser.textarea(id: "delete_user_comment").value).to be_empty
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(id: "no_such_id").clear }.to raise_error(Watir::Exception::UnknownObjectException)
end
bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1200366", :firefox do
it "raises ObjectReadOnlyException if the object is read only" do
expect { browser.text_field(id: "new_user_code").clear }.to raise_error(Watir::Exception::ObjectReadOnlyException)
end
end
end
describe "#value=" do
it "sets the value of the element" do
browser.text_field(id: 'new_user_email').value = 'Hello Cruel World'
expect(browser.text_field(id: "new_user_email").value).to eq 'Hello Cruel World'
end
it "is able to set multi-byte characters" do
browser.text_field(name: "new_user_occupation").value = "ijij"
expect(browser.text_field(name: "new_user_occupation").value).to eq "ijij"
end
it "sets the value of a textarea element" do
browser.textarea(id: 'delete_user_comment').value = 'Hello Cruel World'
expect(browser.textarea(id: "delete_user_comment").value).to eq 'Hello Cruel World'
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(name: "no_such_name").value = 'yo' }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#set" do
it "sets the value of the element" do
browser.text_field(id: 'new_user_email').set('Bye Cruel World')
expect(browser.text_field(id: "new_user_email").value).to eq 'Bye Cruel World'
end
it "sets the value of a textarea element" do
browser.textarea(id: 'delete_user_comment').set('Hello Cruel World')
expect(browser.textarea(id: "delete_user_comment").value).to eq 'Hello Cruel World'
end
it "fires events" do
browser.text_field(id: "new_user_username").set("Hello World")
expect(browser.span(id: "current_length").text).to eq "11"
end
it "sets the value of a password field" do
browser.text_field(name: 'new_user_password').set('secret')
expect(browser.text_field(name: 'new_user_password').value).to eq 'secret'
end
it "sets the value when accessed through the enclosing Form" do
browser.form(id: 'new_user').text_field(name: 'new_user_password').set('secret')
expect(browser.form(id: 'new_user').text_field(name: 'new_user_password').value).to eq 'secret'
end
it "is able to set multi-byte characters" do
browser.text_field(name: "new_user_occupation").set("ijij")
expect(browser.text_field(name: "new_user_occupation").value).to eq "ijij"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.text_field(id: "no_such_id").set('secret') }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
end