mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Add custom matcher css method like xpath.
This commit is contained in:
parent
6af5a5a7d7
commit
455c0513d5
3 changed files with 25 additions and 0 deletions
|
@ -621,12 +621,17 @@ find yourself using the same kinds of selectors very often:
|
||||||
xpath { |num| ".//tbody/tr[#{num}]" }
|
xpath { |num| ".//tbody/tr[#{num}]" }
|
||||||
end
|
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
|
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
|
an XPath expression generated through the XPath gem. You can now use these
|
||||||
selectors like this:
|
selectors like this:
|
||||||
|
|
||||||
find(:id, 'post_123')
|
find(:id, 'post_123')
|
||||||
find(:row, 3)
|
find(:row, 3)
|
||||||
|
find(:flash_type, :notice)
|
||||||
|
|
||||||
You can specify an optional match option which will automatically use the
|
You can specify an optional match option which will automatically use the
|
||||||
selector if it matches the argument:
|
selector if it matches the argument:
|
||||||
|
|
|
@ -55,6 +55,14 @@ module Capybara
|
||||||
@xpath
|
@xpath
|
||||||
end
|
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)
|
def match(&block)
|
||||||
@match = block if block
|
@match = block if block
|
||||||
@match
|
@match
|
||||||
|
|
|
@ -19,6 +19,10 @@ describe Capybara do
|
||||||
<option selected="selected">Capybara</option>
|
<option selected="selected">Capybara</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<section>
|
||||||
|
<div class="subsection"></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
STRING
|
STRING
|
||||||
end
|
end
|
||||||
|
@ -38,6 +42,14 @@ describe Capybara do
|
||||||
string.should_not have_selector(:lifeform, "Gorilla")
|
string.should_not have_selector(:lifeform, "Gorilla")
|
||||||
end
|
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
|
it "allows using matchers with text option" do
|
||||||
string.should have_css('h1', :text => 'Awesome')
|
string.should have_css('h1', :text => 'Awesome')
|
||||||
string.should_not have_css('h1', :text => 'Not so awesome')
|
string.should_not have_css('h1', :text => 'Not so awesome')
|
||||||
|
|
Loading…
Add table
Reference in a new issue