/
check_spec.rb
191 lines (169 loc) · 5.34 KB
/
check_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
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "check" do
it "should fail if no checkbox found" do
with_html <<-HTML
<html>
<form method="post" action="/login">
</form>
</html>
HTML
lambda { check "remember_me" }.should raise_error(Webrat::NotFoundError)
end
it "should fail if input is not a checkbox" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="text" name="remember_me" />
</form>
</html>
HTML
lambda { check "remember_me" }.should raise_error(Webrat::NotFoundError)
end
it "should check rails style checkboxes" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" />
<input name="user[tos]" type="hidden" value="0" />
<label for="user_tos">TOS</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "1"})
check "TOS"
click_button
end
it "should result in the value on being posted if not specified" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("http://www.example.com/login", "remember_me" => "on")
check "remember_me"
click_button
end
it "should fail if the checkbox is disabled" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" disabled="disabled" />
<input type="submit" />
</form>
</html>
HTML
lambda { check "remember_me" }.should raise_error(Webrat::DisabledFieldError)
end
it "should result in a custom value being posted" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" value="yes" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("http://www.example.com/login", "remember_me" => "yes")
check "remember_me"
click_button
end
end
describe "uncheck" do
it "should fail if no checkbox found" do
with_html <<-HTML
<html>
<form method="post" action="/login">
</form>
</html>
HTML
lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError)
end
it "should fail if input is not a checkbox" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="text" name="remember_me" />
</form>
</html>
HTML
lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError)
end
it "should fail if the checkbox is disabled" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" checked="checked" disabled="disabled" />
<input type="submit" />
</form>
</html>
HTML
lambda { uncheck "remember_me" }.should raise_error(Webrat::DisabledFieldError)
end
it "should uncheck rails style checkboxes" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
<input name="user[tos]" type="hidden" value="0" />
<label for="user_tos">TOS</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "0"})
check "TOS"
uncheck "TOS"
click_button
end
it "should result in value not being posted" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" value="yes" checked="checked" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("http://www.example.com/login", {})
uncheck "remember_me"
click_button
end
it "should work with checkboxes with the same name" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="option_1" name="options[]" type="checkbox" value="1" />
<label for="option_1">Option 1</label>
<input id="option_2" name="options[]" type="checkbox" value="2" />
<label for="option_2">Option 2</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("http://www.example.com/login", {"options" => ["1", "2"]})
check 'Option 1'
check 'Option 2'
click_button
end
it "should uncheck rails style checkboxes nested inside a label" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<label>
TOS
<input name="user[tos]" type="hidden" value="0" />
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("http://www.example.com/login", "user" => {"tos" => "0"})
uncheck "TOS"
click_button
end
end