diff --git a/CHANGELOG.md b/CHANGELOG.md index 71b8257..323f65d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ - `Node#trigger` silently dispatched nothing for events other than mouse, focus and `submit`, such as `input`, `change` or custom events [#197] - `Node#set` with an empty string cleared a text field without firing the `input` event [#164] +- Finders with `exact_text: true` raised `InvalidSelector` because Capybara appended an empty `[]` predicate to the + XPath [#256] ### Removed diff --git a/lib/capybara/cuprite/browser.rb b/lib/capybara/cuprite/browser.rb index e3cfe46..09efce0 100644 --- a/lib/capybara/cuprite/browser.rb +++ b/lib/capybara/cuprite/browser.rb @@ -263,6 +263,10 @@ def all_text(node) private def find_all(method, selector, within = nil) + # Capybara appends its text-match hint as `(expr)[...]`, but with `exact_text: true` the hint + # is empty and the result `(expr)[]` is invalid XPath. An empty predicate is never valid, so + # dropping it can't change a valid selector. Remove once teamcapybara/capybara#2860 is released. + selector = selector.delete_suffix("[]") if method == :xpath nodes = if within evaluate("_cuprite.find(arguments[0], arguments[1], arguments[2])", method, selector, within) else diff --git a/spec/features/session_spec.rb b/spec/features/session_spec.rb index d7b015c..c5339b2 100644 --- a/spec/features/session_spec.rb +++ b/spec/features/session_spec.rb @@ -804,6 +804,11 @@ expect(@session.evaluate_script("window.last_hashchange")).to eq("#foo") end + it "finds a link with exact_text: true" do + @session.visit "/with_html" + expect(@session.find_link("A link", exact_text: true)[:href]).to end_with("/with_simple_html") + end + context "current_url" do let(:request_uri) { URI.parse(@session.current_url).request_uri }