Skip to content

Commit

Permalink
dont convert css to xpath
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Feb 16, 2013
1 parent e22c937 commit 4f805d5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/capybara/node/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ def first(*args)

def resolve_query(query, exact=nil)
elements = synchronize do
base.find(query.xpath(exact)).map do |node|
# base.find(query.xpath(exact)).map do |node|
if query.selector.name==:css && base.respond_to?(:find_css)
base.find_css(query.locator)
else
base.find(query.xpath(exact))
end.map do |node|

This comment has been minimized.

Copy link
@betelgeuse

betelgeuse Oct 3, 2013

@jnicklas at least with capybara-webkit this leads to regressions as the css engine there can't handle everything that XPath could convert to.

thoughtbot/capybara-webkit#510

One can maybe say that the syntax was not supported to begin with but still I would have in 2.1 kept this feature behind a config switch. What do you think?

This comment has been minimized.

Copy link
@jnicklas

jnicklas Oct 3, 2013

Collaborator

I've actually seen many people tripped up by :first behaving differently than it does in jQuery, so from that perspective, it was a "feature" which both violated the spec and people's expectations of how it should behave. So, I disagree, we can't be paralyzed into not making any changes at all, that's not progress. The new behaviour is better in just about every way, it's faster, more standardized and allows for interesting features which were not possible before, there's no sense in putting this behind a config flag.

This comment has been minimized.

Copy link
@betelgeuse

betelgeuse Oct 3, 2013

Actually my problem is not with :first but with the QT engine underlying capybara-webkit failing when provided a valid selector like this:

input[name$=']foo[en]']

This comment has been minimized.

Copy link
@betelgeuse

betelgeuse Oct 3, 2013

I am not arguing against making progress. I am talking about how we should follow semantic versioning. I would have no problem if 2.1 was 3.0.

This comment has been minimized.

Copy link
@jnicklas

jnicklas Oct 3, 2013

Collaborator

If you're satisfied with a config option, it is actually possible to change this back, you'd need to do something like this:

Capybara.add_selector(:css) do
  xpath { |css| XPath.css(css) }
end

I think this should force Capybara to use Nokogiri to convert XPath conversion instead.

Capybara::Node::Element.new(session, node, self, query)
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/capybara/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def match

def xpath(exact=nil)
exact = @options[:exact] if exact == nil

if @xpath.respond_to?(:to_xpath) and exact
@xpath.to_xpath(:exact)
else
Expand Down
15 changes: 13 additions & 2 deletions lib/capybara/rack_test/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,19 @@ def dom
@dom ||= Nokogiri::HTML(html)
end

def find(selector)
dom.xpath(selector).map { |node| Capybara::RackTest::Node.new(self, node) }
def find(selector, type=:xpath)
if type==:css
dom.css(selector, Class.new {
def disabled list
list.find_all { |node| node.has_attribute? 'disabled' }
end
def enabled list
list.find_all { |node| !node.has_attribute? 'disabled' }
end
}.new)
else
dom.xpath(selector)
end.map { |node| Capybara::RackTest::Node.new(self, node) }
end

def html
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/rack_test/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def status_code
def find(selector)
browser.find(selector)
end

def find_css(selector)
browser.find(selector, :css)
end

def html
browser.html
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/selenium/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def current_url
def find(selector)
browser.find_elements(:xpath, selector).map { |node| Capybara::Selenium::Node.new(self, node) }
end

def find_css(selector)
browser.find_elements(:css, selector).map { |node| Capybara::Selenium::Node.new(self, node) }
end

def wait?; true; end
def needs_server?; true; end
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/spec/session/find_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
@session.find(:css, 'h1').text.should == 'This is a test'
@session.find(:css, "input[id='test_field']")[:value].should == 'monkey'
end

it "should support pseudo selectors" do
@session.find(:css, 'input:disabled').value.should == 'This is disabled'
end
end

context "with xpath selectors" do
Expand Down
2 changes: 2 additions & 0 deletions lib/capybara/spec/views/with_html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ banana</textarea>
<div class="almost_singular but_not_quite">almost singular but not quite</div>
<div class="almost_singular">almost singular</div>
</div>

<input type="text" disabled name="disabled_text" value="This is disabled"/>

0 comments on commit 4f805d5

Please sign in to comment.