Browse files

Add custom matcher css method like xpath.

  • Loading branch information...
1 parent 6af5a5a commit 455c0513d53f12357811ead8215da4387401447d @ordinaryzelig ordinaryzelig committed Sep 29, 2011
Showing with 25 additions and 0 deletions.
  1. +5 −0 README.rdoc
  2. +8 −0 lib/capybara/selector.rb
  3. +12 −0 spec/basic_node_spec.rb
View
5 README.rdoc
@@ -621,12 +621,17 @@ find yourself using the same kinds of selectors very often:
xpath { |num| ".//tbody/tr[#{num}]" }
end
+ Capybara.add_selector(:flash_type) do
+ css { |type| "#flash.#{type}" }
+ end
+
The block given to xpath must always return an XPath expression as a String, or
an XPath expression generated through the XPath gem. You can now use these
selectors like this:
find(:id, 'post_123')
find(:row, 3)
+ find(:flash_type, :notice)
You can specify an optional match option which will automatically use the
selector if it matches the argument:
View
8 lib/capybara/selector.rb
@@ -55,6 +55,14 @@ def xpath(&block)
@xpath
end
+ # Same as xpath, but wrap in XPath.css().
+ def css(&block)
+ if block
+ @xpath = xpath { |*args| XPath.css(block.call(*args)) }
+ end
+ @xpath
+ end
+
def match(&block)
@match = block if block
@match
View
12 spec/basic_node_spec.rb
@@ -19,6 +19,10 @@
<option selected="selected">Capybara</option>
</select>
</div>
+
+ <section>
+ <div class="subsection"></div>
+ </div>
</div>
STRING
end
@@ -38,6 +42,14 @@
string.should_not have_selector(:lifeform, "Gorilla")
end
+ it 'allows custom matcher using css' do
+ Capybara.add_selector :section do
+ css { |css_class| "section .#{css_class}" }
+ end
+ string.should have_selector(:section, 'subsection')
+ string.should_not have_selector(:section, 'section_8')
+ end
+
it "allows using matchers with text option" do
string.should have_css('h1', :text => 'Awesome')
string.should_not have_css('h1', :text => 'Not so awesome')

0 comments on commit 455c051

Please sign in to comment.