forked from brynary/webrat
/
click_link_spec.rb
520 lines (465 loc) · 14.3 KB
/
click_link_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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "click_link" do
it "should click links with ampertands" do
with_html <<-HTML
<html>
<a href="/page">Save & go back</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Save & go back"
end
it "should use get by default" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text"
end
it "should click get links" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text", :method => :get
end
it "should click link on substring" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "ink tex", :method => :get
end
it "should click delete links" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:delete).with("/page", {})
click_link "Link text", :method => :delete
end
it "should click post links" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:post).with("/page", {})
click_link "Link text", :method => :post
end
it "should click put links" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:put).with("/page", {})
click_link "Link text", :method => :put
end
it "should click links by regexp" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link /link [a-z]/i
end
it "should click links by id" do
with_html <<-HTML
<html>
<a id="link_text_link" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "link_text_link"
end
it "should click links by id regexp" do
with_html <<-HTML
<html>
<a id="link_text_link" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link /_text_/
end
it "should click links by title" do
with_html <<-HTML
<html>
<a title="piddle" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link 'piddle'
end
it "should click links by title regex" do
with_html <<-HTML
<html>
<a title="piddlediddle" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link /iddle/
end
it "should click links by class" do
with_html <<-HTML
<html>
<a class="piddle" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link 'piddle'
end
it "should click links by class when there are many classes" do
with_html <<-HTML
<html>
<a class="one two three" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link 'two'
end
it "should click links by class regex" do
with_html <<-HTML
<html>
<a class="piddlediddle" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link /iddle/
end
it "should click rails javascript links with authenticity tokens" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
click_link "Posts"
end
it "should click rails javascript delete links" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:delete).with("/posts/1", {})
click_link "Delete"
end
it "should click rails javascript post links" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:post).with("/posts", {})
click_link "Posts"
end
it "should click rails javascript post links without javascript" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:get).with("/posts", {})
click_link "Posts", :javascript => false
end
it "should click rails javascript put links" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:put).with("/posts", {})
click_link "Put"
end
it "should fail if the javascript link doesn't have a value for the _method input" do
with_html <<-HTML
<html>
<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');
f.appendChild(m);
f.submit();
return false;">Link</a>
</html>
HTML
lambda {
click_link "Link"
}.should raise_error(Webrat::WebratError)
end
it "should assert valid response" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.response_code = 501
lambda { click_link "Link text" }.should raise_error(Webrat::PageLoadError)
end
[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status
lambda { click_link "Link text" }.should_not raise_error
end
end
it "should fail is the link doesn't exist" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
lambda {
click_link "Missing link"
}.should raise_error(Webrat::NotFoundError)
end
it "should not be case sensitive" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "LINK TEXT"
end
it "should match link substrings" do
with_html <<-HTML
<html>
<a href="/page">This is some cool link text, isn't it?</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text"
end
it "should work with elements in the link" do
with_html <<-HTML
<html>
<a href="/page"><span>Link text</span></a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text"
end
it "should match the first matching link" do
with_html <<-HTML
<html>
<a href="/page1">Link text</a>
<a href="/page2">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page1", {})
click_link "Link text"
end
it "should choose the shortest link text match" do
with_html <<-HTML
<html>
<a href="/page1">Linkerama</a>
<a href="/page2">Link</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page2", {})
click_link "Link"
end
it "should treat non-breaking spaces as spaces" do
with_html <<-HTML
<html>
<a href="/page1">This is a link</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page1", {})
click_link "This is a link"
end
it "should not match on non-text contents" do
pending "needs fix" do
with_html <<-HTML
<html>
<a href="/page1"><span class="location">My house</span></a>
<a href="/page2">Location</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page2", {})
click_link "Location"
end
end
it "should click link within a selector" do
with_html <<-HTML
<html>
<a href="/page1">Link</a>
<div id="container">
<a href="/page2">Link</a>
</div>
</html>
HTML
webrat_session.should_receive(:get).with("/page2", {})
click_link_within "#container", "Link"
end
it "should not make request when link is local anchor" do
with_html <<-HTML
<html>
<a href="#section-1">Jump to Section 1</a>
</html>
HTML
# Don't know why webrat_session.should_receive(:get).never doesn't work here
webrat_session.should_receive(:send).with('get_via_redirect', '#section-1', {}).never
click_link "Jump to Section 1"
end
it "should follow relative links" do
webrat_session.stub!(:current_url => "/page")
with_html <<-HTML
<html>
<a href="sub">Jump to sub page</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page/sub", {})
click_link "Jump to sub page"
end
it "should follow fully qualified local links" do
webrat_session.stub!(:current_url => "/page")
with_html <<-HTML
<html>
<a href="http://subdomain.example.com/page/sub">Jump to sub page</a>
</html>
HTML
webrat_session.should_receive(:get).with("http://subdomain.example.com/page/sub", {})
click_link "Jump to sub page"
end
it "should follow fully qualified local links to example.com" do
with_html <<-HTML
<html>
<a href="http://www.example.com/page/sub">Jump to sub page</a>
</html>
HTML
webrat_session.should_receive(:get).with("http://www.example.com/page/sub", {})
click_link "Jump to sub page"
end
it "should follow query parameters" do
webrat_session.stub!(:current_url => "/page")
with_html <<-HTML
<html>
<a href="?foo=bar">Jump to foo bar</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page?foo=bar", {})
click_link "Jump to foo bar"
end
it "should matches_text? on regexp" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?(/link/i).should == 0
end
it "should matches_text? on link_text" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("Link Text").should == 0
end
it "should matches_text? on substring" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.should_receive(:inner_html).and_return('Link Text')
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end
it "should match text not include " do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('LinkText')
link.matches_text?("LinkText").should == 0
end
it "should not matches_text? on wrong text" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
nbsp = [0xA0].pack("U")
link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link")
link.should_receive(:inner_html).and_return("Some Other Link")
link.should_receive(:title).and_return(nil)
link.matches_text?("Link Text").should == false
end
it "should match text including character reference" do
pending "need to update these"
no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA]
nokogiri_ja_kana = no_ko_gi_ri.pack("U*")
nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("")
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(nokogiri_ja_kana)
link.matches_text?(nokogiri_ja_kana).should == 0
end
it "should match img link" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('')
link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
end