Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added :between, :maximum, and :minimum conditions to has_css? and

has_no_css? matcher methods.
  • Loading branch information...
commit 45b55b138d84309bdb561276708b30cb586d9c9c 1 parent 8229d13
James B. Byrne authored October 29, 2010
28  lib/capybara/node/matchers.rb
@@ -37,11 +37,21 @@ def has_selector?(*args)
37 37
         wait_conditionally_until do
38 38
           results = all(*args)
39 39
 
40  
-          if options[:count]
41  
-            results.size == options[:count]
  40
+          return false if results.empty?
  41
+
  42
+          case
  43
+          when options[:between]
  44
+            options[:between] === results.size
  45
+          when options[:count]
  46
+            options[:count] == results.size
  47
+          when options[:maximum]
  48
+            options[:maximum] >= results.size
  49
+          when options[:minimum]
  50
+            options[:minimum] <= results.size
42 51
           else
43 52
             results.size > 0
44 53
           end
  54
+ 
45 55
         end
46 56
       rescue Capybara::TimeoutError
47 57
         return false
@@ -60,11 +70,21 @@ def has_no_selector?(*args)
60 70
         wait_conditionally_until do
61 71
           results = all(*args)
62 72
 
63  
-          if options[:count]
64  
-            results.size != options[:count]
  73
+          return true if results.empty?
  74
+
  75
+          case
  76
+          when options[:between]
  77
+            not(options[:between] === results.size)
  78
+          when options[:count]
  79
+            not(options[:count] == results.size)
  80
+          when options[:maximum]
  81
+            not(options[:maximum] >= results.size)
  82
+          when options[:minimum]
  83
+            not(options[:minimum] <= results.size)
65 84
           else
66 85
             results.empty?
67 86
           end
  87
+
68 88
         end
69 89
       rescue Capybara::TimeoutError
70 90
         return false
108  lib/capybara/spec/session/has_css_spec.rb
@@ -22,13 +22,30 @@
22 22
       end
23 23
     end
24 24
 
  25
+    context "with between" do
  26
+      it "should be true if the content occurs within the range given" do
  27
+        @session.should have_css("p", :between => 1..4)
  28
+        @session.should have_css("p a#foo", :between => 1..3)
  29
+      end
  30
+
  31
+      it "should be false if the content occurs more or fewer times than range" do
  32
+        @session.should_not have_css("p", :between => 6..11 )
  33
+        @session.should_not have_css("p a#foo", :between => 4..7)
  34
+      end
  35
+
  36
+      it "should be false if the content isn't on the page at all" do
  37
+        @session.should_not have_css("abbr", :between => 1..8)
  38
+        @session.should_not have_css("p a.doesnotexist", :between => 3..8)
  39
+      end
  40
+    end
  41
+
25 42
     context "with count" do
26 43
       it "should be true if the content is on the page the given number of times" do
27 44
         @session.should have_css("p", :count => 3)
28 45
         @session.should have_css("p a#foo", :count => 1)
29 46
       end
30 47
 
31  
-      it "should be false if the content is on the page the given number of times" do
  48
+      it "should be false if the content occurs the given number of times" do
32 49
         @session.should_not have_css("p", :count => 6)
33 50
         @session.should_not have_css("p a#foo", :count => 2)
34 51
       end
@@ -39,6 +56,42 @@
39 56
       end
40 57
     end
41 58
 
  59
+    context "with maximum" do
  60
+      it "should be true when content occurs same or fewer times than given" do
  61
+        @session.should have_css("h2.head", :maximum => 5) # edge case
  62
+        @session.should have_css("h2", :maximum => 10)
  63
+      end
  64
+
  65
+      it "should be false when content occurs more times than given" do
  66
+        @session.should_not have_css("h2.head", :maximum => 4) # edge case
  67
+        @session.should_not have_css("h2", :maximum => 6)
  68
+        @session.should_not have_css("p", :maximum => 1)
  69
+      end
  70
+
  71
+      it "should be false if the content isn't on the page at all" do
  72
+        @session.should_not have_css("abbr", :maximum => 2)
  73
+        @session.should_not have_css("p a.doesnotexist", :maximum => 1)
  74
+      end
  75
+    end
  76
+
  77
+    context "with minimum" do
  78
+      it "should be true when content occurs same or more times than given" do
  79
+        @session.should have_css("h2.head", :minimum => 5) # edge case
  80
+        @session.should have_css("h2", :minimum => 3)
  81
+      end
  82
+
  83
+      it "should be false when content occurs fewer times than given" do
  84
+        @session.should_not have_css("h2.head", :minimum => 6) # edge case
  85
+        @session.should_not have_css("h2", :minimum => 8)
  86
+        @session.should_not have_css("p", :minimum => 10)
  87
+      end
  88
+
  89
+      it "should be false if the content isn't on the page at all" do
  90
+        @session.should_not have_css("abbr", :minimum => 2)
  91
+        @session.should_not have_css("p a.doesnotexist", :minimum => 7)
  92
+      end
  93
+    end
  94
+
42 95
     context "with text" do
43 96
       it "should discard all matches where the given string is not contained" do
44 97
         @session.should have_css("p a", :text => "Redirect", :count => 1)
@@ -75,6 +128,23 @@
75 128
       end
76 129
     end
77 130
 
  131
+    context "with between" do
  132
+      it "should be false if the content occurs within the range given" do
  133
+        @session.should_not have_no_css("p", :between => 1..4)
  134
+        @session.should_not have_no_css("p a#foo", :between => 1..3)
  135
+      end
  136
+
  137
+      it "should be true if the content occurs more or fewer times than range" do
  138
+        @session.should have_no_css("p", :between => 6..11 )
  139
+        @session.should have_no_css("p a#foo", :between => 4..7)
  140
+      end
  141
+
  142
+      it "should be true if the content isn't on the page at all" do
  143
+        @session.should have_no_css("abbr", :between => 1..8)
  144
+        @session.should have_no_css("p a.doesnotexist", :between => 3..8)
  145
+      end
  146
+    end
  147
+
78 148
     context "with count" do
79 149
       it "should be false if the content is on the page the given number of times" do
80 150
         @session.should_not have_no_css("p", :count => 3)
@@ -92,6 +162,42 @@
92 162
       end
93 163
     end
94 164
 
  165
+    context "with maximum" do
  166
+      it "should be false when content occurs same or fewer times than given" do
  167
+        @session.should_not have_no_css("h2.head", :maximum => 5) # edge case
  168
+        @session.should_not have_no_css("h2", :maximum => 10)
  169
+      end
  170
+
  171
+      it "should be true when content occurs more times than given" do
  172
+        @session.should have_no_css("h2.head", :maximum => 4) # edge case
  173
+        @session.should have_no_css("h2", :maximum => 6)
  174
+        @session.should have_no_css("p", :maximum => 1)
  175
+      end
  176
+
  177
+      it "should be true if the content isn't on the page at all" do
  178
+        @session.should have_no_css("abbr", :maximum => 5)
  179
+        @session.should have_no_css("p a.doesnotexist", :maximum => 10)
  180
+      end
  181
+    end
  182
+
  183
+    context "with minimum" do
  184
+      it "should be false when content occurs more times than given" do
  185
+        @session.should_not have_no_css("h2.head", :minimum => 4) # edge case
  186
+        @session.should_not have_no_css("h2", :minimum => 3)
  187
+      end
  188
+
  189
+      it "should be true when content occurs fewer times than given" do
  190
+        @session.should have_no_css("h2.head", :minimum => 6) # edge case
  191
+        @session.should have_no_css("h2", :minimum => 8)
  192
+        @session.should have_no_css("p", :minimum => 15)
  193
+      end
  194
+
  195
+      it "should be true if the content isn't on the page at all" do
  196
+        @session.should have_no_css("abbr", :minimum => 5)
  197
+        @session.should have_no_css("p a.doesnotexist", :minimum => 10)
  198
+      end
  199
+    end
  200
+
95 201
     context "with text" do
96 202
       it "should discard all matches where the given string is not contained" do
97 203
         @session.should_not have_no_css("p a", :text => "Redirect", :count => 1)
12  lib/capybara/spec/views/with_html.erb
... ...
@@ -1,6 +1,14 @@
1 1
 <h1>This is a test</h1>
2 2
 
3  
-<p id="first">
  3
+<h2 class="no text"></h2>
  4
+<h2 class="no text"></h2>
  5
+<h2 class="head" id="h2one">Header Class Test One</h2>
  6
+<h2 class="head" id="h2two">Header Class Test Two</h2>
  7
+<h2 class="head">Header Class Test Three</h2>
  8
+<h2 class="head">Header Class Test Four</h2>
  9
+<h2 class="head">Header Class Test Five</h2>
  10
+
  11
+<p class="para" id="first">
4 12
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
5 13
   tempor incididunt ut <a href="/with_simple_html" title="awesome title" class="simple">labore</a>
6 14
   et dolore magna aliqua. Ut enim ad minim veniam,
@@ -9,7 +17,7 @@
9 17
   <a href="/with_simple_html"><img src="http://www.foobar.sun/dummy_image.jpg" width="20" height="20" alt="awesome image" /></a>
10 18
 </p>
11 19
 
12  
-<p id="second">
  20
+<p class="para" id="second">
13 21
   Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
14 22
   dolore eu fugiat <a href="/redirect" id="red">Redirect</a> pariatur. Excepteur sint occaecat cupidatat non proident,
15 23
   sunt in culpa qui officia

0 notes on commit 45b55b1

Please sign in to comment.
Something went wrong with that request. Please try again.