diff --git a/README.md b/README.md index 197738d34..b1b42f291 100644 --- a/README.md +++ b/README.md @@ -481,9 +481,9 @@ certain elements, and working with and manipulating those elements. ```ruby page.has_selector?('table tr') -page.has_selector?(:xpath, '//table/tr') +page.has_selector?(:xpath, './/table/tr') -page.has_xpath?('//table/tr') +page.has_xpath?('.//table/tr') page.has_css?('table tr.foo') page.has_content?('foo') ``` @@ -495,9 +495,9 @@ You can use these with RSpec's magic matchers: ```ruby expect(page).to have_selector('table tr') -expect(page).to have_selector(:xpath, '//table/tr') +expect(page).to have_selector(:xpath, './/table/tr') -expect(page).to have_xpath('//table/tr') +expect(page).to have_xpath('.//table/tr') expect(page).to have_css('table tr.foo') expect(page).to have_content('foo') ``` @@ -517,7 +517,7 @@ find_link(class: ['some_class', 'some_other_class'], :visible => :all).visible? find_button('Send').click find_button(value: '1234').click -find(:xpath, "//table/tr").click +find(:xpath, ".//table/tr").click find("#overlay").find("h1").click all('a').each { |a| a[:href] } ``` @@ -554,7 +554,7 @@ within("li#employee") do fill_in 'Name', with: 'Jimmy' end -within(:xpath, "//li[@id='employee']") do +within(:xpath, ".//li[@id='employee']") do fill_in 'Name', with: 'Jimmy' end ``` @@ -807,7 +807,7 @@ module MyModule include Capybara::DSL def login! - within(:xpath, "//form[@id='session']") do + within(:xpath, ".//form[@id='session']") do fill_in 'Email', with: 'user@example.com' fill_in 'Password', with: 'password' end @@ -892,16 +892,16 @@ and will always use CSS by default. If you want to use XPath, you'll need to do: ```ruby -within(:xpath, '//ul/li') { ... } -find(:xpath, '//ul/li').text -find(:xpath, '//li[contains(.//a[@href = "#"]/text(), "foo")]').value +within(:xpath, './/ul/li') { ... } +find(:xpath, './/ul/li').text +find(:xpath, './/li[contains(.//a[@href = "#"]/text(), "foo")]').value ``` Alternatively you can set the default selector to XPath: ```ruby Capybara.default_selector = :xpath -find('//ul/li').text +find('.//ul/li').text ``` Capybara allows you to add custom selectors, which can be very useful if you diff --git a/lib/capybara/node/finders.rb b/lib/capybara/node/finders.rb index 3e013388a..643695463 100644 --- a/lib/capybara/node/finders.rb +++ b/lib/capybara/node/finders.rb @@ -18,7 +18,7 @@ module Finders # +find+ takes the same options as +all+. # # page.find('#foo').find('.bar') - # page.find(:xpath, '//div[contains(., "bar")]') + # page.find(:xpath, './/div[contains(., "bar")]') # page.find('li', text: 'Quox').click_link('Delete') # # @param (see Capybara::Node::Finders#all) @@ -155,7 +155,7 @@ def find_by_id(id, options={}, &optional_filter_block) # following statements are equivalent: # # page.all(:css, 'a#person_123') - # page.all(:xpath, '//a[@id="person_123"]') + # page.all(:xpath, './/a[@id="person_123"]') # # # If the type of selector is left out, Capybara uses @@ -164,7 +164,7 @@ def find_by_id(id, options={}, &optional_filter_block) # page.all("a#person_123") # # Capybara.default_selector = :xpath - # page.all('//a[@id="person_123"]') + # page.all('.//a[@id="person_123"]') # # The set of found elements can further be restricted by specifying # options. It's possible to select elements by their text or visibility: diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 0a4f22b08..fb9c273ab 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -263,7 +263,7 @@ def go_forward # block, any command to Capybara will be handled as though it were scoped # to the given element. # - # within(:xpath, '//div[@id="delivery-address"]') do + # within(:xpath, './/div[@id="delivery-address"]') do # fill_in('Street', with: '12 Main Street') # end # diff --git a/spec/basic_node_spec.rb b/spec/basic_node_spec.rb index 01616ed89..ffa00f053 100644 --- a/spec/basic_node_spec.rb +++ b/spec/basic_node_spec.rb @@ -52,7 +52,7 @@ it "allows using custom matchers" do Capybara.add_selector :lifeform do - xpath { |name| "//option[contains(.,'#{name}')]" } + xpath { |name| ".//option[contains(.,'#{name}')]" } end expect(string).to have_selector(:id, "page") expect(string).not_to have_selector(:id, 'does-not-exist')