forked from brynary/webrat
/
fill_in_spec.rb
230 lines (209 loc) · 7.01 KB
/
fill_in_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
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "fill_in" do
it "should work with textareas" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_text">User Text</label>
<textarea id="user_text" name="user[text]"></textarea>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"text" => "filling text area"})
fill_in "User Text", :with => "filling text area"
click_button
end
it "should support multiline values" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_text">User Text</label>
<textarea id="user_text" name="user[text]"></textarea>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"text" => "One\nTwo"})
fill_in "User Text", :with => "One\nTwo"
click_button
end
[
'password', 'search', 'email', 'url', 'tel', 'color',
'number', 'range', 'date', 'month', 'week', 'time',
'datetime', 'datetime-local'
].each do |type|
it "should work with #{type} fields" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_text" name="user[text]" type="#{type}" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"text" => "some value"})
fill_in "user_text", :with => "some value"
click_button
end
end
it "should fail if input not found" do
with_html <<-HTML
<html>
<form method="get" action="/login">
</form>
</html>
HTML
lambda { fill_in "Email", :with => "foo@example.com" }.should raise_error(Webrat::NotFoundError)
end
it "should fail if input is disabled" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="text" disabled="disabled" />
<input type="submit" />
</form>
</html>
HTML
lambda { fill_in "Email", :with => "foo@example.com" }.should raise_error(Webrat::DisabledFieldError)
end
it "should allow overriding default form values" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in "user[email]", :with => "foo@example.com"
click_button
end
it "should choose the shortest label match" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_mail1">Some other mail</label>
<input id="user_mail1" name="user[mail1]" type="text" />
<label for="user_mail2">Some mail</label>
<input id="user_mail2" name="user[mail2]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "", "mail2" => "value"})
fill_in "Some", :with => "value"
click_button
end
it "should choose the first label match if closest is a tie" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_mail1">Some mail one</label>
<input id="user_mail1" name="user[mail1]" type="text" />
<label for="user_mail2">Some mail two</label>
<input id="user_mail2" name="user[mail2]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "value", "mail2" => ""})
fill_in "Some mail", :with => "value"
click_button
end
it "should anchor label matches to start of label" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_email">Some mail</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
</form>
</html>
HTML
lambda { fill_in "mail", :with => "value" }.should raise_error(Webrat::NotFoundError)
end
it "should anchor label matches to word boundaries" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_email">Emailtastic</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
</form>
</html>
HTML
lambda { fill_in "Email", :with => "value" }.should raise_error(Webrat::NotFoundError)
end
it "should work with inputs nested in labels" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label>
Email
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in "Email", :with => "foo@example.com"
click_button
end
it "should work with full input names" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_email" name="user[email]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in "user[email]", :with => "foo@example.com"
click_button
end
it "should work if the input type is not set" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_email" name="user[email]" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in "user[email]", :with => "foo@example.com"
click_button
end
it "should work with symbols" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in :email, :with => "foo@example.com"
click_button
end
it "should escape field values" do
with_html <<-HTML
<html>
<form method="post" action="/users">
<label for="user_phone">Phone</label>
<input id="user_phone" name="user[phone]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/users", "user" => {"phone" => "+1 22 33"})
fill_in 'Phone', :with => "+1 22 33"
click_button
end
end