Skip to content

Commit

Permalink
Add custom matcher css method like xpath.
Browse files Browse the repository at this point in the history
  • Loading branch information
ordinaryzelig committed Sep 29, 2011
1 parent 6af5a5a commit 455c051
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions lib/capybara/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/basic_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<option selected="selected">Capybara</option>
</select>
</div>
<section>
<div class="subsection"></div>
</div>
</div>
STRING
end
Expand All @@ -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')
Expand Down

0 comments on commit 455c051

Please sign in to comment.