File: matchers_spec.rb

package info (click to toggle)
ruby-capybara 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 948 kB
  • ctags: 516
  • sloc: ruby: 7,998; makefile: 4
file content (687 lines) | stat: -rw-r--r-- 21,861 bytes parent folder | download | duplicates (2)
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
require 'spec_helper'
require 'capybara/dsl'
require 'capybara/rspec/matchers'

describe Capybara::RSpecMatchers do
  include Capybara::DSL
  include Capybara::RSpecMatchers

  describe "have_css matcher" do
    it "gives proper description" do
      have_css('h1').description.should == "have css \"h1\""
    end

    context "on a string" do
      context "with should" do
        it "passes if has_css? returns true" do
          "<h1>Text</h1>".should have_css('h1')
        end

        it "fails if has_css? returns false" do
          expect do
            "<h1>Text</h1>".should have_css('h2')
          end.to raise_error(/expected to find css "h2" but there were no matches/)
        end

        it "passes if matched node count equals expected count" do
          "<h1>Text</h1>".should have_css('h1', :count => 1)
        end

        it "fails if matched node count does not equal expected count" do
          expect do
            "<h1>Text</h1>".should have_css('h1', count: 2)
          end.to raise_error("expected to find css \"h1\" 2 times, found 1 match: \"Text\"")
        end

        it "fails if matched node count is less than expected minimum count" do
          expect do
            "<h1>Text</h1>".should have_css('p', minimum: 1)
          end.to raise_error("expected to find css \"p\" at least 1 time but there were no matches")
        end

        it "fails if matched node count is more than expected maximum count" do
          expect do
            "<h1>Text</h1><h1>Text</h1><h1>Text</h1>".should have_css('h1', maximum: 2)
          end.to raise_error('expected to find css "h1" at most 2 times, found 3 matches: "Text", "Text", "Text"')
        end

        it "fails if matched node count does not belong to expected range" do
          expect do
            "<h1>Text</h1>".should have_css('h1', between: 2..3)
          end.to raise_error("expected to find css \"h1\" between 2 and 3 times, found 1 match: \"Text\"")
        end

      end

      context "with should_not" do
        it "passes if has_no_css? returns true" do
          "<h1>Text</h1>".should_not have_css('h2')
        end

        it "fails if has_no_css? returns false" do
          expect do
            "<h1>Text</h1>".should_not have_css('h1')
          end.to raise_error(/expected not to find css "h1"/)
        end

        it "passes if matched node count does not equal expected count" do
          "<h1>Text</h1>".should_not have_css('h1', :count => 2)
        end

        it "fails if matched node count equals expected count" do
          expect do
            "<h1>Text</h1>".should_not have_css('h1', :count => 1)
          end.to raise_error(/expected not to find css "h1"/)
        end
      end
    end

    context "on a page or node" do
      before do
        visit('/with_html')
      end

      context "with should" do
        it "passes if has_css? returns true" do
          page.should have_css('h1')
        end

        it "fails if has_css? returns false" do
          expect do
            page.should have_css('h1#doesnotexist')
          end.to raise_error(/expected to find css "h1#doesnotexist" but there were no matches/)
        end
      end

      context "with should_not" do
        it "passes if has_no_css? returns true" do
          page.should_not have_css('h1#doesnotexist')
        end

        it "fails if has_no_css? returns false" do
          expect do
            page.should_not have_css('h1')
          end.to raise_error(/expected not to find css "h1"/)
        end
      end
    end
  end

  describe "have_xpath matcher" do
    it "gives proper description" do
      have_xpath('//h1').description.should == "have xpath \"\/\/h1\""
    end

    context "on a string" do
      context "with should" do
        it "passes if has_xpath? returns true" do
          "<h1>Text</h1>".should have_xpath('//h1')
        end

        it "fails if has_xpath? returns false" do
          expect do
            "<h1>Text</h1>".should have_xpath('//h2')
          end.to raise_error(%r(expected to find xpath "//h2" but there were no matches))
        end
      end

      context "with should_not" do
        it "passes if has_no_xpath? returns true" do
          "<h1>Text</h1>".should_not have_xpath('//h2')
        end

        it "fails if has_no_xpath? returns false" do
          expect do
            "<h1>Text</h1>".should_not have_xpath('//h1')
          end.to raise_error(%r(expected not to find xpath "//h1"))
        end
      end
    end

    context "on a page or node" do
      before do
        visit('/with_html')
      end

      context "with should" do
        it "passes if has_xpath? returns true" do
          page.should have_xpath('//h1')
        end

        it "fails if has_xpath? returns false" do
          expect do
            page.should have_xpath("//h1[@id='doesnotexist']")
          end.to raise_error(%r(expected to find xpath "//h1\[@id='doesnotexist'\]" but there were no matches))
        end
      end

      context "with should_not" do
        it "passes if has_no_xpath? returns true" do
          page.should_not have_xpath('//h1[@id="doesnotexist"]')
        end

        it "fails if has_no_xpath? returns false" do
          expect do
            page.should_not have_xpath('//h1')
          end.to raise_error(%r(expected not to find xpath "//h1"))
        end
      end
    end
  end

  describe "have_selector matcher" do
    it "gives proper description" do
      matcher = have_selector('//h1')
      "<h1>Text</h1>".should matcher
      matcher.description.should == "have xpath \"//h1\""
    end

    context "on a string" do
      context "with should" do
        it "passes if has_selector? returns true" do
          "<h1>Text</h1>".should have_selector('//h1')
        end

        it "fails if has_selector? returns false" do
          expect do
            "<h1>Text</h1>".should have_selector('//h2')
          end.to raise_error(%r(expected to find xpath "//h2" but there were no matches))
        end
      end

      context "with should_not" do
        it "passes if has_no_selector? returns true" do
          "<h1>Text</h1>".should_not have_selector(:css, 'h2')
        end

        it "fails if has_no_selector? returns false" do
          expect do
            "<h1>Text</h1>".should_not have_selector(:css, 'h1')
          end.to raise_error(%r(expected not to find css "h1"))
        end
      end
    end

    context "on a page or node" do
      before do
        visit('/with_html')
      end

      context "with should" do
        it "passes if has_selector? returns true" do
          page.should have_selector('//h1', :text => 'test')
        end

        it "fails if has_selector? returns false" do
          expect do
            page.should have_selector("//h1[@id='doesnotexist']")
          end.to raise_error(%r(expected to find xpath "//h1\[@id='doesnotexist'\]" but there were no matches))
        end

        it "includes text in error message" do
          expect do
            page.should have_selector("//h1", :text => 'wrong text')
          end.to raise_error(%r(expected to find xpath "//h1" with text "wrong text" but there were no matches))
        end
      end

      context "with should_not" do
        it "passes if has_no_css? returns true" do
          page.should_not have_selector(:css, 'h1#doesnotexist')
        end

        it "fails if has_no_selector? returns false" do
          expect do
            page.should_not have_selector(:css, 'h1', :text => 'test')
          end.to raise_error(%r(expected not to find css "h1" with text "test"))
        end
      end
    end
  end

  describe "have_content matcher" do
    it "gives proper description" do
      have_content('Text').description.should == "text \"Text\""
    end

    context "on a string" do
      context "with should" do
        it "passes if has_content? returns true" do
          "<h1>Text</h1>".should have_content('Text')
        end

        it "passes if has_content? returns true using regexp" do
          "<h1>Text</h1>".should have_content(/ext/)
        end

        it "fails if has_content? returns false" do
          expect do
            "<h1>Text</h1>".should have_content('No such Text')
          end.to raise_error(/expected to find text "No such Text" in "Text"/)
        end
      end

      context "with should_not" do
        it "passes if has_no_content? returns true" do
          "<h1>Text</h1>".should_not have_content('No such Text')
        end

        it "passes because escapes any characters that would have special meaning in a regexp" do
          "<h1>Text</h1>".should_not have_content('.')
        end

        it "fails if has_no_content? returns false" do
          expect do
            "<h1>Text</h1>".should_not have_content('Text')
          end.to raise_error(/expected not to find text "Text" in "Text"/)
        end
      end
    end

    context "on a page or node" do
      before do
        visit('/with_html')
      end

      context "with should" do
        it "passes if has_content? returns true" do
          page.should have_content('This is a test')
        end

        it "passes if has_content? returns true using regexp" do
          page.should have_content(/test/)
        end

        it "fails if has_content? returns false" do
          expect do
            page.should have_content('No such Text')
          end.to raise_error(/expected to find text "No such Text" in "(.*)This is a test(.*)"/)
        end

        context "with default selector CSS" do
          before { Capybara.default_selector = :css }
          it "fails if has_content? returns false" do
            expect do
              page.should have_content('No such Text')
            end.to raise_error(/expected to find text "No such Text" in "(.*)This is a test(.*)"/)
          end
          after { Capybara.default_selector = :xpath }
        end
      end

      context "with should_not" do
        it "passes if has_no_content? returns true" do
          page.should_not have_content('No such Text')
        end

        it "fails if has_no_content? returns false" do
          expect do
            page.should_not have_content('This is a test')
          end.to raise_error(/expected not to find text "This is a test"/)
        end
      end
    end
  end

  describe "have_text matcher" do
    it "gives proper description" do
      have_text('Text').description.should == "text \"Text\""
    end

    context "on a string" do
      context "with should" do
        it "passes if has_text? returns true" do
          "<h1>Text</h1>".should have_text('Text')
        end

        it "passes if has_text? returns true using regexp" do
          "<h1>Text</h1>".should have_text(/ext/)
        end

        it "fails if has_text? returns false" do
          expect do
            "<h1>Text</h1>".should have_text('No such Text')
          end.to raise_error(/expected to find text "No such Text" in "Text"/)
        end

        it "casts Fixnum to string" do
          expect do
            "<h1>Text</h1>".should have_text(3)
          end.to raise_error(/expected to find text "3" in "Text"/)
        end

        it "fails if matched text count does not equal to expected count" do
          expect do
            "<h1>Text</h1>".should have_text('Text', count: 2)
          end.to raise_error(/expected to find text "Text" 2 times in "Text"/)
        end

        it "fails if matched text count is less than expected minimum count" do
          expect do
            "<h1>Text</h1>".should have_text('Lorem', minimum: 1)
          end.to raise_error(/expected to find text "Lorem" at least 1 time in "Text"/)
        end

        it "fails if matched text count is more than expected maximum count" do
          expect do
            "<h1>Text TextText</h1>".should have_text('Text', maximum: 2)
          end.to raise_error(/expected to find text "Text" at most 2 times in "Text TextText"/)
        end

        it "fails if matched text count does not belong to expected range" do
          expect do
            "<h1>Text</h1>".should have_text('Text', between: 2..3)
          end.to raise_error(/expected to find text "Text" between 2 and 3 times in "Text"/)
        end
      end

      context "with should_not" do
        it "passes if has_no_text? returns true" do
          "<h1>Text</h1>".should_not have_text('No such Text')
        end

        it "passes because escapes any characters that would have special meaning in a regexp" do
          "<h1>Text</h1>".should_not have_text('.')
        end

        it "fails if has_no_text? returns false" do
          expect do
            "<h1>Text</h1>".should_not have_text('Text')
          end.to raise_error(/expected not to find text "Text" in "Text"/)
        end
      end
    end

    context "on a page or node" do
      before do
        visit('/with_html')
      end

      context "with should" do
        it "passes if has_text? returns true" do
          page.should have_text('This is a test')
        end

        it "passes if has_text? returns true using regexp" do
          page.should have_text(/test/)
        end

        it "can check for all text" do
          page.should have_text(:all, 'Some of this text is hidden!')
        end

        it "can check for visible text" do
          page.should have_text(:visible, 'Some of this text is')
          page.should_not have_text(:visible, 'Some of this text is hidden!')
        end

        it "fails if has_text? returns false" do
          expect do
            page.should have_text('No such Text')
          end.to raise_error(/expected to find text "No such Text" in "(.*)This is a test(.*)"/)
        end

        context "with default selector CSS" do
          before { Capybara.default_selector = :css }
          it "fails if has_text? returns false" do
            expect do
              page.should have_text('No such Text')
            end.to raise_error(/expected to find text "No such Text" in "(.*)This is a test(.*)"/)
          end
          after { Capybara.default_selector = :xpath }
        end
      end

      context "with should_not" do
        it "passes if has_no_text? returns true" do
          page.should_not have_text('No such Text')
        end

        it "fails if has_no_text? returns false" do
          expect do
            page.should_not have_text('This is a test')
          end.to raise_error(/expected not to find text "This is a test"/)
        end
      end
    end
  end

  describe "have_link matcher" do
    let(:html) { '<a href="#">Just a link</a>' }

    it "gives proper description" do
      have_link('Just a link').description.should == "have link \"Just a link\""
    end

    it "passes if there is such a button" do
      html.should have_link('Just a link')
    end

    it "fails if there is no such button" do
      expect do
        html.should have_link('No such Link')
      end.to raise_error(/expected to find link "No such Link"/)
    end
  end

  describe "have_title matcher" do
    it "gives proper description" do
      have_title('Just a title').description.should == "have title \"Just a title\""
    end

    context "on a string" do
      let(:html) { '<title>Just a title</title>' }

      it "passes if there is such a title" do
        html.should have_title('Just a title')
      end

      it "fails if there is no such title" do
        expect do
          html.should have_title('No such title')
        end.to raise_error(/expected there to be title "No such title"/)
      end
    end

    context "on a page or node" do
      before do
        visit('/with_js')
      end

      it "passes if there is such a title" do
        page.should have_title('with_js')
      end

      it "fails if there is no such title" do
        expect do
          page.should have_title('No such title')
        end.to raise_error(/expected there to be title "No such title"/)
      end
    end
  end

  describe "have_button matcher" do
    let(:html) { '<button>A button</button><input type="submit" value="Another button"/>' }

    it "gives proper description" do
      have_button('A button').description.should == "have button \"A button\""
    end

    it "passes if there is such a button" do
      html.should have_button('A button')
    end

    it "fails if there is no such button" do
      expect do
        html.should have_button('No such Button')
      end.to raise_error(/expected to find button "No such Button"/)
    end
  end

  describe "have_field matcher" do
    let(:html) { '<p><label>Text field<input type="text" value="some value"/></label></p>' }

    it "gives proper description" do
      have_field('Text field').description.should == "have field \"Text field\""
    end

    it "gives proper description for a given value" do
      have_field('Text field', with: 'some value').description.should == "have field \"Text field\" with value \"some value\""
    end

    it "passes if there is such a field" do
      html.should have_field('Text field')
    end

    it "passes if there is such a field with value" do
      html.should have_field('Text field', with: 'some value')
    end

    it "fails if there is no such field" do
      expect do
        html.should have_field('No such Field')
      end.to raise_error(/expected to find field "No such Field"/)
    end

    it "fails if there is such field but with false value" do
      expect do
        html.should have_field('Text field', with: 'false value')
      end.to raise_error(/expected to find field "Text field"/)
    end

    it "treats a given value as a string" do
      class Foo
        def to_s
          "some value"
        end
      end
      html.should have_field('Text field', with: Foo.new)
    end
  end

  describe "have_checked_field matcher" do
    let(:html) do
      '<label>it is checked<input type="checkbox" checked="checked"/></label>
      <label>unchecked field<input type="checkbox"/></label>'
    end

    it "gives proper description" do
      have_checked_field('it is checked').description.should == "have field \"it is checked\""
    end

    context "with should" do
      it "passes if there is such a field and it is checked" do
        html.should have_checked_field('it is checked')
      end

      it "fails if there is such a field but it is not checked" do
        expect do
          html.should have_checked_field('unchecked field')
        end.to raise_error(/expected to find field "unchecked field"/)
      end

      it "fails if there is no such field" do
        expect do
          html.should have_checked_field('no such field')
        end.to raise_error(/expected to find field "no such field"/)
      end
    end

    context "with should not" do
      it "fails if there is such a field and it is checked" do
        expect do
          html.should_not have_checked_field('it is checked')
        end.to raise_error(/expected not to find field "it is checked"/)
      end

      it "passes if there is such a field but it is not checked" do
        html.should_not have_checked_field('unchecked field')
      end

      it "passes if there is no such field" do
        html.should_not have_checked_field('no such field')
      end
    end
  end

  describe "have_unchecked_field matcher" do
    let(:html) do
      '<label>it is checked<input type="checkbox" checked="checked"/></label>
      <label>unchecked field<input type="checkbox"/></label>'
    end

    it "gives proper description" do
      have_unchecked_field('unchecked field').description.should == "have field \"unchecked field\""
    end

    context "with should" do
      it "passes if there is such a field and it is not checked" do
        html.should have_unchecked_field('unchecked field')
      end

      it "fails if there is such a field but it is checked" do
        expect do
          html.should have_unchecked_field('it is checked')
        end.to raise_error(/expected to find field "it is checked"/)
      end

      it "fails if there is no such field" do
        expect do
          html.should have_unchecked_field('no such field')
        end.to raise_error(/expected to find field "no such field"/)
      end
    end

    context "with should not" do
      it "fails if there is such a field and it is not checked" do
        expect do
          html.should_not have_unchecked_field('unchecked field')
        end.to raise_error(/expected not to find field "unchecked field"/)
      end

      it "passes if there is such a field but it is checked" do
        html.should_not have_unchecked_field('it is checked')
      end

      it "passes if there is no such field" do
        html.should_not have_unchecked_field('no such field')
      end
    end
  end

  describe "have_select matcher" do
    let(:html) { '<label>Select Box<select></select></label>' }

    it "gives proper description" do
      have_select('Select Box').description.should == "have select box \"Select Box\""
    end

    it "passes if there is such a select" do
      html.should have_select('Select Box')
    end

    it "fails if there is no such select" do
      expect do
        html.should have_select('No such Select box')
      end.to raise_error(/expected to find select box "No such Select box"/)
    end
  end

  describe "have_table matcher" do
    let(:html) { '<table><caption>Lovely table</caption></table>' }

    it "gives proper description" do
      have_table('Lovely table').description.should == "have table \"Lovely table\""
    end

    it "passes if there is such a select" do
      html.should have_table('Lovely table')
    end

    it "fails if there is no such select" do
      expect do
        html.should have_table('No such Table')
      end.to raise_error(/expected to find table "No such Table"/)
    end
  end
end