mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Added custom selectors
This commit is contained in:
parent
8c534e4b70
commit
064ecda28d
4 changed files with 25 additions and 2 deletions
|
@ -45,6 +45,14 @@ module Capybara
|
||||||
def configure
|
def configure
|
||||||
yield self
|
yield self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_selector(name, &block)
|
||||||
|
selectors[name.to_sym] = block
|
||||||
|
end
|
||||||
|
|
||||||
|
def selectors
|
||||||
|
@selectors ||= {}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
autoload :Server, 'capybara/server'
|
autoload :Server, 'capybara/server'
|
||||||
|
@ -70,3 +78,6 @@ Capybara.configure do |config|
|
||||||
config.default_wait_time = 2
|
config.default_wait_time = 2
|
||||||
config.ignore_hidden_elements = false
|
config.ignore_hidden_elements = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Capybara.add_selector(:xpath) { |xpath| xpath }
|
||||||
|
Capybara.add_selector(:css) { |css| XPath::HTML.from_css(css) }
|
||||||
|
|
|
@ -142,8 +142,7 @@ module Capybara
|
||||||
|
|
||||||
def normalize_locator(kind, locator=nil)
|
def normalize_locator(kind, locator=nil)
|
||||||
kind, locator = Capybara.default_selector, kind if locator.nil?
|
kind, locator = Capybara.default_selector, kind if locator.nil?
|
||||||
locator = XPath::HTML.from_css(locator) if kind == :css
|
Capybara.selectors[kind.to_sym].call(locator)
|
||||||
locator
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def wait_conditionally_until
|
def wait_conditionally_until
|
||||||
|
|
|
@ -49,6 +49,14 @@ shared_examples_for "find" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with custom selector" do
|
||||||
|
it "should use the custom selector" do
|
||||||
|
Capybara.add_selector(:monkey) { |name| ".//*[@id='#{name}_monkey']" }
|
||||||
|
@session.find(:monkey, 'john').text.should == 'Monkey John'
|
||||||
|
@session.find(:monkey, 'paul').text.should == 'Monkey Paul'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "with css as default selector" do
|
context "with css as default selector" do
|
||||||
before { Capybara.default_selector = :css }
|
before { Capybara.default_selector = :css }
|
||||||
it "should find the first element using the given locator" do
|
it "should find the first element using the given locator" do
|
||||||
|
|
|
@ -41,3 +41,8 @@
|
||||||
<div id="hidden_via_ancestor">Inside element with hidden ancestor</div>
|
<div id="hidden_via_ancestor">Inside element with hidden ancestor</div>
|
||||||
<a href="/with_simple_html" title="awesome title" class="simple">hidden link</a>
|
<a href="/with_simple_html" title="awesome title" class="simple">hidden link</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li id="john_monkey">Monkey John</li>
|
||||||
|
<li id="paul_monkey">Monkey Paul</li>
|
||||||
|
</ul>
|
||||||
|
|
Loading…
Reference in a new issue