mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
selector as first argument to find/all, Closes #28
Just as with within, the default is changed by setting Capybara.default_selector.
This commit is contained in:
parent
ecdc2bd8c5
commit
05fd0d3f06
7 changed files with 106 additions and 28 deletions
|
@ -300,12 +300,16 @@ if you want to use CSS with your 'within' declarations for example, you'll need
|
|||
to do:
|
||||
|
||||
within(:css, 'ul li') { ... }
|
||||
find(:css, 'ul li').text
|
||||
locate(:css, 'input#name').value
|
||||
|
||||
Alternatively you can set the default selector to CSS, which may help if you are
|
||||
moving from Webrat and used CSS a lot, or simply generally prefer CSS:
|
||||
|
||||
Capybara.default_selector = :css
|
||||
within('ul li') { ... }
|
||||
find('ul li').text
|
||||
locate('input#name').value
|
||||
|
||||
== Gotchas:
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
module Capybara
|
||||
module Searchable
|
||||
def find(locator, options = {})
|
||||
all(locator, options).first
|
||||
def find(*args)
|
||||
all(*args).first
|
||||
end
|
||||
|
||||
def find_field(locator)
|
||||
|
@ -21,7 +21,15 @@ module Capybara
|
|||
find(XPath.for_css("##{id}"))
|
||||
end
|
||||
|
||||
def all(locator, options = {})
|
||||
def all(*args)
|
||||
options = if args.last.is_a?(Hash) then args.pop else {} end
|
||||
if args[1].nil?
|
||||
kind, locator = Capybara.default_selector, args.first
|
||||
else
|
||||
kind, locator = args
|
||||
end
|
||||
locator = XPath.from_css(locator) if kind == :css
|
||||
|
||||
results = all_unfiltered(locator)
|
||||
|
||||
if options[:text]
|
||||
|
|
|
@ -47,53 +47,53 @@ module Capybara
|
|||
|
||||
def click(locator)
|
||||
msg = "no link or button '#{locator}' found"
|
||||
locate(XPath.link(locator).button(locator), msg).click
|
||||
locate(:xpath, XPath.link(locator).button(locator), msg).click
|
||||
end
|
||||
|
||||
def click_link(locator)
|
||||
msg = "no link with title, id or containing text / image with alternative text like '#{locator}' found"
|
||||
locate(XPath.link(locator), msg).click
|
||||
locate(:xpath, XPath.link(locator), msg).click
|
||||
end
|
||||
|
||||
def click_button(locator)
|
||||
msg = "no button with value or id or text '#{locator}' found"
|
||||
locate(XPath.button(locator)).click
|
||||
locate(:xpath, XPath.button(locator)).click
|
||||
end
|
||||
|
||||
def drag(source_locator, target_locator)
|
||||
source = locate(source_locator, "drag source '#{source_locator}' not found on page")
|
||||
target = locate(target_locator, "drag target '#{target_locator}' not found on page")
|
||||
source = locate(:xpath, source_locator, "drag source '#{source_locator}' not found on page")
|
||||
target = locate(:xpath, target_locator, "drag target '#{target_locator}' not found on page")
|
||||
source.drag_to(target)
|
||||
end
|
||||
|
||||
def fill_in(locator, options={})
|
||||
msg = "cannot fill in, no text field, text area or password field with id, name, or label '#{locator}' found"
|
||||
locate(XPath.fillable_field(locator), msg).set(options[:with])
|
||||
locate(:xpath, XPath.fillable_field(locator), msg).set(options[:with])
|
||||
end
|
||||
|
||||
def choose(locator)
|
||||
msg = "cannot choose field, no radio button with id, name, or label '#{locator}' found"
|
||||
locate(XPath.radio_button(locator), msg).set(true)
|
||||
locate(:xpath, XPath.radio_button(locator), msg).set(true)
|
||||
end
|
||||
|
||||
def check(locator)
|
||||
msg = "cannot check field, no checkbox with id, name, or label '#{locator}' found"
|
||||
locate(XPath.checkbox(locator), msg).set(true)
|
||||
locate(:xpath, XPath.checkbox(locator), msg).set(true)
|
||||
end
|
||||
|
||||
def uncheck(locator)
|
||||
msg = "cannot uncheck field, no checkbox with id, name, or label '#{locator}' found"
|
||||
locate(XPath.checkbox(locator), msg).set(false)
|
||||
locate(:xpath, XPath.checkbox(locator), msg).set(false)
|
||||
end
|
||||
|
||||
def select(value, options={})
|
||||
msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
|
||||
locate(XPath.select(options[:from]), msg).select(value)
|
||||
locate(:xpath, XPath.select(options[:from]), msg).select(value)
|
||||
end
|
||||
|
||||
def attach_file(locator, path)
|
||||
msg = "cannot attach file, no file field with id, name, or label '#{locator}' found"
|
||||
locate(XPath.file_field(locator), msg).set(path)
|
||||
locate(:xpath, XPath.file_field(locator), msg).set(path)
|
||||
end
|
||||
|
||||
def body
|
||||
|
@ -107,7 +107,7 @@ module Capybara
|
|||
def within(kind, scope=nil)
|
||||
kind, scope = Capybara.default_selector, kind unless scope
|
||||
scope = XPath.from_css(scope) if kind == :css
|
||||
locate(scope, "scope '#{scope}' not found on page")
|
||||
locate(:xpath, scope, "scope '#{scope}' not found on page")
|
||||
scopes.push(scope)
|
||||
yield
|
||||
scopes.pop
|
||||
|
@ -175,10 +175,10 @@ module Capybara
|
|||
end
|
||||
|
||||
#return node identified by locator or raise ElementNotFound(using desc)
|
||||
def locate(locator, fail_msg = nil)
|
||||
node = wait_conditionally_until { find(locator) }
|
||||
def locate(kind_or_locator, locator=nil, fail_msg = nil)
|
||||
node = wait_conditionally_until { find(kind_or_locator, locator) }
|
||||
ensure
|
||||
raise Capybara::ElementNotFound, fail_msg || "Unable to locate '#{locator}'" unless node
|
||||
raise Capybara::ElementNotFound, fail_msg || "Unable to locate '#{kind_or_locator}'" unless node
|
||||
return node
|
||||
end
|
||||
|
||||
|
|
|
@ -11,6 +11,29 @@ module AllSpec
|
|||
@session.all("//input[@id='test_field']").first[:value].should == 'monkey'
|
||||
end
|
||||
|
||||
context "with css selectors" do
|
||||
it "should find the first element using the given locator" do
|
||||
@session.all(:css, 'h1').first.text.should == 'This is a test'
|
||||
@session.all(:css, "input[id='test_field']").first[:value].should == 'monkey'
|
||||
end
|
||||
end
|
||||
|
||||
context "with xpath selectors" do
|
||||
it "should find the first element using the given locator" do
|
||||
@session.all(:xpath, '//h1').first.text.should == 'This is a test'
|
||||
@session.all(:xpath, "//input[@id='test_field']").first[:value].should == 'monkey'
|
||||
end
|
||||
end
|
||||
|
||||
context "with css as default selector" do
|
||||
before { Capybara.default_selector = :css }
|
||||
it "should find the first element using the given locator" do
|
||||
@session.all('h1').first.text.should == 'This is a test'
|
||||
@session.all("input[id='test_field']").first[:value].should == 'monkey'
|
||||
end
|
||||
after { Capybara.default_selector = :xpath }
|
||||
end
|
||||
|
||||
it "should return an empty array when nothing was found" do
|
||||
@session.all('//div[@id="nosuchthing"]').should be_empty
|
||||
end
|
||||
|
@ -35,4 +58,4 @@ module AllSpec
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,6 +10,29 @@ module FindSpec
|
|||
@session.find("//input[@id='test_field']")[:value].should == 'monkey'
|
||||
end
|
||||
|
||||
context "with css selectors" do
|
||||
it "should find the first element using the given locator" do
|
||||
@session.find(:css, 'h1').text.should == 'This is a test'
|
||||
@session.find(:css, "input[id='test_field']")[:value].should == 'monkey'
|
||||
end
|
||||
end
|
||||
|
||||
context "with xpath selectors" do
|
||||
it "should find the first element using the given locator" do
|
||||
@session.find(:xpath, '//h1').text.should == 'This is a test'
|
||||
@session.find(:xpath, "//input[@id='test_field']")[:value].should == 'monkey'
|
||||
end
|
||||
end
|
||||
|
||||
context "with css as default selector" do
|
||||
before { Capybara.default_selector = :css }
|
||||
it "should find the first element using the given locator" do
|
||||
@session.find('h1').text.should == 'This is a test'
|
||||
@session.find("input[id='test_field']")[:value].should == 'monkey'
|
||||
end
|
||||
after { Capybara.default_selector = :xpath }
|
||||
end
|
||||
|
||||
it "should return nil when nothing was found" do
|
||||
@session.find('//div[@id="nosuchthing"]').should be_nil
|
||||
end
|
||||
|
@ -33,4 +56,4 @@ module FindSpec
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,9 +10,32 @@ module LocateSpec
|
|||
@session.locate("//input[@id='test_field']")[:value].should == 'monkey'
|
||||
end
|
||||
|
||||
context "with css selectors" do
|
||||
it "should find the first element using the given locator" do
|
||||
@session.locate(:css, 'h1').text.should == 'This is a test'
|
||||
@session.locate(:css, "input[id='test_field']")[:value].should == 'monkey'
|
||||
end
|
||||
end
|
||||
|
||||
context "with xpath selectors" do
|
||||
it "should find the first element using the given locator" do
|
||||
@session.locate(:xpath, '//h1').text.should == 'This is a test'
|
||||
@session.locate(:xpath, "//input[@id='test_field']")[:value].should == 'monkey'
|
||||
end
|
||||
end
|
||||
|
||||
context "with css as default selector" do
|
||||
before { Capybara.default_selector = :css }
|
||||
it "should find the first element using the given locator" do
|
||||
@session.locate('h1').text.should == 'This is a test'
|
||||
@session.locate("input[id='test_field']")[:value].should == 'monkey'
|
||||
end
|
||||
after { Capybara.default_selector = :xpath }
|
||||
end
|
||||
|
||||
it "should raise ElementNotFound with specified fail message if nothing was found" do
|
||||
running do
|
||||
@session.locate('//div[@id="nosuchthing"]', 'arghh').should be_nil
|
||||
@session.locate(:xpath, '//div[@id="nosuchthing"]', 'arghh').should be_nil
|
||||
end.should raise_error(Capybara::ElementNotFound, "arghh")
|
||||
end
|
||||
|
||||
|
@ -35,4 +58,4 @@ module LocateSpec
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,17 +33,14 @@ module WithinSpec
|
|||
end
|
||||
|
||||
context "with the default selector set to CSS" do
|
||||
after do
|
||||
Capybara.default_selector = :xpath
|
||||
end
|
||||
|
||||
before { Capybara.default_selector = :css }
|
||||
it "should use CSS" do
|
||||
Capybara.default_selector = :css
|
||||
@session.within("ul li[contains('With Simple HTML')]") do
|
||||
@session.click_link('Go')
|
||||
end
|
||||
@session.body.should include('Bar')
|
||||
end
|
||||
after { Capybara.default_selector = :xpath }
|
||||
end
|
||||
|
||||
context "with click_link" do
|
||||
|
@ -142,4 +139,4 @@ module WithinSpec
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue