forked from brynary/webrat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clicks_link_test.rb
211 lines (189 loc) · 6.7 KB
/
clicks_link_test.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
require File.dirname(__FILE__) + "/helper"
class ClicksLinkTest < Test::Unit::TestCase
def setup
@session = ActionController::Integration::Session.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
end
def test_should_use_get_by_default
@response.stubs(:body).returns(<<-EOS)
<a href="/page">Link text</a>
EOS
@session.expects(:get_via_redirect).with("/page", {})
@session.clicks_link "Link text"
end
def test_should_click_get_links
@response.stubs(:body).returns(<<-EOS)
<a href="/page">Link text</a>
EOS
@session.expects(:get_via_redirect).with("/page", {})
@session.clicks_get_link "Link text"
end
def test_should_click_delete_links
@response.stubs(:body).returns(<<-EOS)
<a href="/page">Link text</a>
EOS
@session.expects(:delete_via_redirect).with("/page", {})
@session.clicks_delete_link "Link text"
end
def test_should_click_post_links
@response.stubs(:body).returns(<<-EOS)
<a href="/page">Link text</a>
EOS
@session.expects(:post_via_redirect).with("/page", {})
@session.clicks_post_link "Link text"
end
def test_should_click_put_links
@response.stubs(:body).returns(<<-EOS)
<a href="/page">Link text</a>
EOS
@session.expects(:put_via_redirect).with("/page", {})
@session.clicks_put_link "Link text"
end
def test_should_click_rails_javascript_links_with_authenticity_tokens
@response.stubs(:body).returns(<<-EOS)
<a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = this.href;
var s = document.createElement('input');
s.setAttribute('type', 'hidden');
s.setAttribute('name', 'authenticity_token');
s.setAttribute('value', 'aa79cb354597a60a3786e7e291ed4f74d77d3a62');
f.appendChild(s);
f.submit();
return false;">Posts</a>
EOS
@session.expects(:post_via_redirect).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
@session.clicks_link "Posts"
end
def test_should_click_rails_javascript_delete_links
@response.stubs(:body).returns(<<-EOS)
<a href="/posts/1" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = this.href;
var m = document.createElement('input');
m.setAttribute('type', 'hidden');
m.setAttribute('name', '_method');
m.setAttribute('value', 'delete');
f.appendChild(m);
f.submit();
return false;">Delete</a>
EOS
@session.expects(:delete_via_redirect).with("/posts/1", {})
@session.clicks_link "Delete"
end
def test_should_click_rails_javascript_post_links
@response.stubs(:body).returns(<<-EOS)
<a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = this.href;
f.submit();
return false;">Posts</a>
EOS
@session.expects(:post_via_redirect).with("/posts", {})
@session.clicks_link "Posts"
end
def test_should_click_rails_javascript_put_links
@response.stubs(:body).returns(<<-EOS)
<a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = this.href;
var m = document.createElement('input');
m.setAttribute('type', 'hidden');
m.setAttribute('name', '_method');
m.setAttribute('value', 'put');
f.appendChild(m);
f.submit();
return false;">Put</a></h2>
EOS
@session.expects(:put_via_redirect).with("/posts", {})
@session.clicks_link "Put"
end
def test_should_assert_valid_response
@response.stubs(:body).returns(<<-EOS)
<a href="/page">Link text</a>
EOS
@session.expects(:assert_response).with(:success)
@session.clicks_link "Link text"
end
def test_should_not_be_case_sensitive
@response.stubs(:body).returns(<<-EOS)
<a href="/page">Link text</a>
EOS
@session.expects(:get_via_redirect).with("/page", {})
@session.clicks_link "LINK TEXT"
end
def test_should_match_link_substrings
@response.stubs(:body).returns(<<-EOS)
<a href="/page">This is some cool link text, isn't it?</a>
EOS
@session.expects(:get_via_redirect).with("/page", {})
@session.clicks_link "Link text"
end
def test_should_work_with_elements_in_the_link
@response.stubs(:body).returns(<<-EOS)
<a href="/page"><span>Link text</span></a>
EOS
@session.expects(:get_via_redirect).with("/page", {})
@session.clicks_link "Link text"
end
def test_should_match_the_first_matching_link
@response.stubs(:body).returns(<<-EOS)
<a href="/page1">Link text</a>
<a href="/page2">Link text</a>
EOS
@session.expects(:get_via_redirect).with("/page1", {})
@session.clicks_link "Link text"
end
def test_should_choose_the_shortest_link_text_match
@response.stubs(:body).returns(<<-EOS)
<a href="/page1">Linkerama</a>
<a href="/page2">Link</a>
EOS
@session.expects(:get_via_redirect).with("/page2", {})
@session.clicks_link "Link"
end
def test_should_click_link_within_a_selector
@response.stubs(:body).returns(<<-EOS)
<a href="/page1">Link</a>
<div id="container">
<a href="/page2">Link</a>
</div>
EOS
@session.expects(:get_via_redirect).with("/page2", {})
@session.clicks_link_within "#container", "Link"
end
def test_should_not_make_request_when_link_is_local_anchor
@response.stubs(:body).returns(<<-EOS)
<a href="#section-1">Jump to Section 1</a>
EOS
# Don't know why @session.expects(:get_via_redirect).never doesn't work here
@session.expects(:send).with('get_via_redirect', '#section-1', {}).never
@session.clicks_link "Jump to Section 1"
end
def test_should_follow_relative_links
@session.current_page.stubs(:url).returns("/page")
@response.stubs(:body).returns(<<-EOS)
<a href="sub">Jump to sub page</a>
EOS
@session.expects(:get_via_redirect).with("/page/sub", {})
@session.clicks_link "Jump to sub page"
end
def test_should_follow_query_parameters
@session.current_page.stubs(:url).returns("/page")
@response.stubs(:body).returns(<<-EOS)
<a href="?foo=bar">Jump to foo bar</a>
EOS
@session.expects(:get_via_redirect).with("/page?foo=bar", {})
@session.clicks_link "Jump to foo bar"
end
end