mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Add ability to modify a selector
This commit is contained in:
parent
710bc5dca9
commit
46c334cb8a
3 changed files with 99 additions and 21 deletions
|
@ -109,6 +109,23 @@ module Capybara
|
|||
Capybara::Selector.add(name, &block)
|
||||
end
|
||||
|
||||
##
|
||||
#
|
||||
# Modify a selector previously craeated by {Capybara.add_selector}.
|
||||
# For example modifying the :button selector to also find divs styled
|
||||
# to look like buttons might look like this
|
||||
#
|
||||
# Capybara.modfiy_selector(:button) do
|
||||
# xpath { |locator| XPath::HTML.button(locator).or(XPath::css('div.btn')[XPath::string.n.is(locator)]) }
|
||||
# end
|
||||
#
|
||||
# @param [Symbol] name The name of the selector to modify
|
||||
# @yield A block executed in the context of the existing {Capybara::Selector}
|
||||
#
|
||||
def modify_selector(name, &block)
|
||||
Capybara::Selector.update(name, &block)
|
||||
end
|
||||
|
||||
def drivers
|
||||
@drivers ||= {}
|
||||
end
|
||||
|
|
|
@ -49,6 +49,10 @@ module Capybara
|
|||
all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
|
||||
end
|
||||
|
||||
def update(name, &block)
|
||||
all[name.to_sym].instance_eval(&block)
|
||||
end
|
||||
|
||||
def remove(name)
|
||||
all.delete(name.to_sym)
|
||||
end
|
||||
|
@ -65,15 +69,19 @@ module Capybara
|
|||
end
|
||||
|
||||
def xpath(&block)
|
||||
@format = :xpath
|
||||
@xpath = block if block
|
||||
if block
|
||||
@format = :xpath
|
||||
@xpath, @css = block, nil
|
||||
end
|
||||
@xpath
|
||||
end
|
||||
|
||||
# Same as xpath, but wrap in XPath.css().
|
||||
def css(&block)
|
||||
@format = :css
|
||||
@css = block if block
|
||||
if block
|
||||
@format = :css
|
||||
@css, @xpath = block, nil
|
||||
end
|
||||
@css
|
||||
end
|
||||
|
||||
|
|
53
spec/selector_spec.rb
Normal file
53
spec/selector_spec.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
require 'spec_helper'
|
||||
|
||||
RSpec.describe Capybara do
|
||||
describe 'Selectors' do
|
||||
let :string do
|
||||
Capybara.string <<-STRING
|
||||
<html>
|
||||
<head>
|
||||
<title>selectors</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="a" id="page">
|
||||
<div class="b" id="content">
|
||||
<h1 class="a">Totally awesome</h1>
|
||||
<p>Yes it is</p>
|
||||
</div>
|
||||
<p class="b">Some Content</p>
|
||||
<p class="b"></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
STRING
|
||||
end
|
||||
|
||||
before do
|
||||
Capybara.add_selector :custom_selector do
|
||||
css { |css_class| "div.#{css_class}" }
|
||||
filter(:not_empty, boolean: true, default: true, skip_if: :all) { |node, value| value ^ (node.text == '') }
|
||||
end
|
||||
end
|
||||
|
||||
describe "modfiy_selector" do
|
||||
it "allows modifying a selector" do
|
||||
el = string.find(:custom_selector, 'a')
|
||||
expect(el.tag_name).to eq 'div'
|
||||
Capybara.modify_selector :custom_selector do
|
||||
css { |css_class| "h1.#{css_class}" }
|
||||
end
|
||||
el = string.find(:custom_selector, 'a')
|
||||
expect(el.tag_name).to eq 'h1'
|
||||
end
|
||||
|
||||
it "doesn't change existing filters" do
|
||||
Capybara.modify_selector :custom_selector do
|
||||
css { |css_class| "p.#{css_class}"}
|
||||
end
|
||||
expect(string).to have_selector(:custom_selector, 'b', count: 1)
|
||||
expect(string).to have_selector(:custom_selector, 'b', not_empty: false, count: 1)
|
||||
expect(string).to have_selector(:custom_selector, 'b', not_empty: :all, count: 2)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue