Add custom matcher css method like xpath.

This commit is contained in:
Jared Ning 2011-09-29 15:04:51 -05:00
parent 6af5a5a7d7
commit 455c0513d5
3 changed files with 25 additions and 0 deletions

View File

@ -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 File

@ -55,6 +55,14 @@ module Capybara
@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 File

@ -19,6 +19,10 @@ describe Capybara do
<option selected="selected">Capybara</option>
</select>
</div>
<section>
<div class="subsection"></div>
</div>
</div>
STRING
end
@ -38,6 +42,14 @@ describe Capybara do
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')