Permalink
Browse files

Merge pull request #1760 from jnicklas/matches_sugar

Add matches_xpath? and matches_css? on top of matches_selector?
  • Loading branch information...
2 parents 6af2a46 + 63c55dc commit d17f6bfa96b53d583cd5b043b21e41b8a1b8b346 @twalpole committed on GitHub Sep 13, 2016
Showing with 72 additions and 3 deletions.
  1. +1 −1 History.md
  2. +42 −1 lib/capybara/node/matchers.rb
  3. +29 −1 lib/capybara/spec/session/element/matches_selector_spec.rb
View
2 History.md
@@ -8,7 +8,7 @@ Release date: unreleased
* `:class` option to many of the built-in selectors [Thomas Walpole]
* Removed need to specify value when creating `:boolean` filter type in custom selectors [Thomas Walpole]
* Filters can now be implemented through the XPath/CSS expressions in custom selectors [Thomas Walpole]
-
+* `Element#matches_xpath?` and `Element#matches_css?` [Thomas Walpole]
#Version 2.8.1
Release date: 2016-08-25
View
43 lib/capybara/node/matchers.rb
@@ -59,7 +59,6 @@ def has_no_selector?(*args)
##
#
# Checks if the current node matches given selector
- # Usage is identical to Capybara::Node::Matchers#has_selector?
#
# @param (see Capybara::Node::Finders#has_selector?)
# @return [Boolean]
@@ -70,6 +69,27 @@ def matches_selector?(*args)
return false
end
+ ##
+ #
+ # Checks if the current node matches given XPath expression
+ #
+ # @param [String, XPath::Expression] xpath The XPath expression to match against the current code
+ # @return [Boolean]
+ #
+ def matches_xpath?(xpath, options={})
+ matches_selector?(:xpath, xpath, options)
+ end
+
+ ##
+ #
+ # Checks if the current node matches given CSS selector
+ #
+ # @param [String] css The CSS selector to match against the current code
+ # @return [Boolean]
+ #
+ def matches_css?(css, options={})
+ matches_selector?(:css, css, options)
+ end
##
#
@@ -85,6 +105,27 @@ def not_matches_selector?(*args)
return false
end
+ ##
+ #
+ # Checks if the current node does not match given XPath expression
+ #
+ # @param [String, XPath::Expression] xpath The XPath expression to match against the current code
+ # @return [Boolean]
+ #
+ def not_matches_xpath?(xpath, options={})
+ not_matches_selector?(:xpath, xpath, options)
+ end
+
+ ##
+ #
+ # Checks if the current node does not match given CSS selector
+ #
+ # @param [String] css The CSS selector to match against the current code
+ # @return [Boolean]
+ #
+ def not_matches_css?(css, options={})
+ not_matches_selector?(:css, css, options)
+ end
##
#
View
30 lib/capybara/spec/session/element/matches_selector_spec.rb
@@ -1,4 +1,4 @@
-Capybara::SpecHelper.spec '#match_xpath?' do
+Capybara::SpecHelper.spec '#match_selector?' do
before do
@session.visit('/with_html')
@element = @session.find('//span', text: '42')
@@ -28,6 +28,20 @@
expect(@element).not_to match_selector("//span", :text => "Doesnotexist")
end
end
+
+ it "should have css sugar" do
+ expect(@element.matches_css?('span.number')).to be true
+ expect(@element.matches_css?('span.not_a_number')).to be false
+ expect(@element.matches_css?('span.number', text: "42")).to be true
+ expect(@element.matches_css?('span.number', text: "Nope")).to be false
+ end
+
+ it "should have xpath sugar" do
+ expect(@element.matches_xpath?("//span")).to be true
+ expect(@element.matches_xpath?("//div")).to be false
+ expect(@element.matches_xpath?("//span", text: '42')).to be true
+ expect(@element.matches_xpath?("//span", text: 'Nope')).to be false
+ end
end
Capybara::SpecHelper.spec '#not_matches_selector?' do
@@ -60,4 +74,18 @@
expect(@element).to not_match_selector(:css, "span.number", :text => "Doesnotexist")
end
end
+
+ it "should have CSS sugar" do
+ expect(@element.not_matches_css?("span.number")).to be false
+ expect(@element.not_matches_css?("p a#doesnotexist")).to be true
+ expect(@element.not_matches_css?("span.number", :text => "42")).to be false
+ expect(@element.not_matches_css?("span.number", :text => "Doesnotexist")).to be true
+ end
+
+ it "should have xpath sugar" do
+ expect(@element.not_matches_xpath?("//span")).to be false
+ expect(@element.not_matches_xpath?("//div")).to be true
+ expect(@element.not_matches_xpath?("//span", :text => "42")).to be false
+ expect(@element.not_matches_xpath?("//span", :text => "Doesnotexist")).to be true
+ end
end if Gem::Version.new(RSpec::Expectations::Version::STRING) >= Gem::Version.new('3.1')

0 comments on commit d17f6bf

Please sign in to comment.