1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Very first attempt at using XPath for something

This commit is contained in:
Jonas Nicklas 2010-08-03 14:06:52 +02:00
parent 8d341948fd
commit 04e42bf2fa
7 changed files with 67 additions and 35 deletions

View file

@ -1,3 +1,4 @@
source :gemcutter
gemspec
gem 'xpath', :path => '../xpath'

View file

@ -9,6 +9,11 @@ PATH
rack-test (>= 0.5.4)
selenium-webdriver (>= 0.0.3)
PATH
remote: /Users/jonas/Projects/xpath
specs:
xpath (0.0.1)
GEM
remote: http://rubygems.org/
specs:
@ -52,4 +57,5 @@ DEPENDENCIES
rspec (>= 1.2.9)
selenium-webdriver (>= 0.0.3)
sinatra (>= 0.9.4)
xpath!
yard (>= 0.5.8)

View file

@ -11,7 +11,7 @@ module Capybara
#
def click_link_or_button(locator)
msg = "no link or button '#{locator}' found"
find(:xpath, XPath.link(locator).button(locator), :message => msg).click
find(:xpath, Capybara::XPath.link(locator).button(locator), :message => msg).click
end
##
@ -23,7 +23,7 @@ module Capybara
#
def click_link(locator)
msg = "no link with title, id or text '#{locator}' found"
find(:xpath, XPath.link(locator), :message => msg).click
find(:xpath, Capybara::XPath.link(locator), :message => msg).click
end
##
@ -34,7 +34,7 @@ module Capybara
#
def click_button(locator)
msg = "no button with value or id or text '#{locator}' found"
find(:xpath, XPath.button(locator), :message => msg).click
find(:xpath, Capybara::XPath.button(locator), :message => msg).click
end
##
@ -50,7 +50,7 @@ module Capybara
def fill_in(locator, options={})
msg = "cannot fill in, no text field, text area or password field with id, name, or label '#{locator}' found"
raise "Must pass a hash containing 'with'" if not options.is_a?(Hash) or not options.has_key?(:with)
find(:xpath, XPath.fillable_field(locator), :message => msg).set(options[:with])
find(:xpath, Capybara::XPath.fillable_field(locator), :message => msg).set(options[:with])
end
##
@ -64,7 +64,7 @@ module Capybara
#
def choose(locator)
msg = "cannot choose field, no radio button with id, name, or label '#{locator}' found"
find(:xpath, XPath.radio_button(locator), :message => msg).set(true)
find(:xpath, Capybara::XPath.radio_button(locator), :message => msg).set(true)
end
##
@ -78,7 +78,7 @@ module Capybara
#
def check(locator)
msg = "cannot check field, no checkbox with id, name, or label '#{locator}' found"
find(:xpath, XPath.checkbox(locator), :message => msg).set(true)
find(:xpath, Capybara::XPath.checkbox(locator), :message => msg).set(true)
end
##
@ -92,7 +92,7 @@ module Capybara
#
def uncheck(locator)
msg = "cannot uncheck field, no checkbox with id, name, or label '#{locator}' found"
find(:xpath, XPath.checkbox(locator), :message => msg).set(false)
find(:xpath, Capybara::XPath.checkbox(locator), :message => msg).set(false)
end
##
@ -108,8 +108,8 @@ module Capybara
def select(value, options={})
no_select_msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found"
no_option_msg = "cannot select option, no option with text '#{value}' in select box '#{options[:from]}'"
select = find(:xpath, XPath.select(options[:from]), :message => no_select_msg)
select.find(:xpath, XPath.option(value), :message => no_option_msg).select_option
select = find(:xpath, Capybara::XPath.select(options[:from]), :message => no_select_msg)
select.find(:xpath, Capybara::XPath.option(value), :message => no_option_msg).select_option
end
##
@ -125,8 +125,8 @@ module Capybara
def unselect(value, options={})
no_select_msg = "cannot unselect option, no select box with id, name, or label '#{options[:from]}' found"
no_option_msg = "cannot unselect option, no option with text '#{value}' in select box '#{options[:from]}'"
select = find(:xpath, XPath.select(options[:from]), :message => no_select_msg)
select.find(:xpath, XPath.option(value), :message => no_option_msg).unselect_option
select = find(:xpath, Capybara::XPath.select(options[:from]), :message => no_select_msg)
select.find(:xpath, Capybara::XPath.option(value), :message => no_option_msg).unselect_option
end
##
@ -141,7 +141,7 @@ module Capybara
#
def attach_file(locator, path)
msg = "cannot attach file, no file field with id, name, or label '#{locator}' found"
find(:xpath, XPath.file_field(locator), :message => msg).set(path)
find(:xpath, Capybara::XPath.file_field(locator), :message => msg).set(path)
end
##

View file

@ -47,7 +47,7 @@ module Capybara
# @return [Capybara::Element] The found element
#
def find_field(locator)
find(:xpath, XPath.field(locator))
find(:xpath, Capybara::XPath.field(locator))
end
alias_method :field_labeled, :find_field
@ -59,7 +59,7 @@ module Capybara
# @return [Capybara::Element] The found element
#
def find_link(locator)
find(:xpath, XPath.link(locator))
find(:xpath, Capybara::XPath.link(locator))
end
##
@ -70,7 +70,7 @@ module Capybara
# @return [Capybara::Element] The found element
#
def find_button(locator)
find(:xpath, XPath.button(locator))
find(:xpath, Capybara::XPath.button(locator))
end
##
@ -121,7 +121,7 @@ module Capybara
def all(*args)
options = if args.last.is_a?(Hash) then args.pop else {} end
results = XPath.wrap(normalize_locator(*args)).paths.map do |path|
results = Capybara::XPath.tempwrap(normalize_locator(*args)).map do |path|
base.find(path)
end.flatten
@ -142,7 +142,7 @@ module Capybara
def normalize_locator(kind, locator=nil)
kind, locator = Capybara.default_selector, kind if locator.nil?
locator = XPath.from_css(locator) if kind == :css
locator = Capybara::XPath.from_css(locator) if kind == :css
locator
end

View file

@ -30,67 +30,67 @@ module Capybara
end
def has_css?(path, options={})
has_xpath?(XPath.from_css(path), options)
has_xpath?(Capybara::XPath.from_css(path), options)
end
def has_no_css?(path, options={})
has_no_xpath?(XPath.from_css(path), options)
has_no_xpath?(Capybara::XPath.from_css(path), options)
end
def has_content?(content)
has_xpath?(XPath.content(content))
has_xpath?(Capybara::XPath.content(content))
end
def has_no_content?(content)
has_no_xpath?(XPath.content(content))
has_no_xpath?(Capybara::XPath.content(content))
end
def has_link?(locator)
has_xpath?(XPath.link(locator))
has_xpath?(Capybara::XPath.link(locator))
end
def has_no_link?(locator)
has_no_xpath?(XPath.link(locator))
has_no_xpath?(Capybara::XPath.link(locator))
end
def has_button?(locator)
has_xpath?(XPath.button(locator))
has_xpath?(Capybara::XPath.button(locator))
end
def has_no_button?(locator)
has_no_xpath?(XPath.button(locator))
has_no_xpath?(Capybara::XPath.button(locator))
end
def has_field?(locator, options={})
has_xpath?(XPath.field(locator, options))
has_xpath?(Capybara::XPath.field(locator, options))
end
def has_no_field?(locator, options={})
has_no_xpath?(XPath.field(locator, options))
has_no_xpath?(Capybara::XPath.field(locator, options))
end
def has_checked_field?(locator)
has_xpath?(XPath.field(locator, :checked => true))
has_xpath?(Capybara::XPath.field(locator, :checked => true))
end
def has_unchecked_field?(locator)
has_xpath?(XPath.field(locator, :unchecked => true))
has_xpath?(Capybara::XPath.field(locator, :unchecked => true))
end
def has_select?(locator, options={})
has_xpath?(XPath.select(locator, options))
has_xpath?(Capybara::XPath.select(locator, options))
end
def has_no_select?(locator, options={})
has_no_xpath?(XPath.select(locator, options))
has_no_xpath?(Capybara::XPath.select(locator, options))
end
def has_table?(locator, options={})
has_xpath?(XPath.table(locator, options))
has_xpath?(Capybara::XPath.table(locator, options))
end
def has_no_table?(locator, options={})
has_no_xpath?(XPath.table(locator, options))
has_no_xpath?(Capybara::XPath.table(locator, options))
end
end
end

View file

@ -170,7 +170,7 @@ module Capybara
# @param [String] locator Id or legend of the fieldset
#
def within_fieldset(locator)
within :xpath, XPath.fieldset(locator) do
within :xpath, Capybara::XPath.fieldset(locator) do
yield
end
end
@ -182,7 +182,7 @@ module Capybara
# @param [String] locator Id or caption of the table
#
def within_table(locator)
within :xpath, XPath.table(locator) do
within :xpath, Capybara::XPath.table(locator) do
yield
end
end

View file

@ -1,3 +1,9 @@
require 'xpath'
::XPath::Collection.class_eval do
alias_method :paths, :expressions
end
module Capybara
##
@ -11,6 +17,17 @@ module Capybara
class XPath
class << self
include ::XPath
def link(locator)
link = descendant(:a).where(attr(:href))
var = varstring(:locator)
::XPath::Collection.new(
link.where(text.equals(var) | attr(:title).equals(var) | descendant(:img).where(attr(:alt).equals(var))),
link.where(attr(:id).contains(var) | contains(var) | attr(:title).contains(var) | descendant(:img).where(attr(:alt).contains(var)))
).apply(:locator => locator)
end
def escape(string)
if string.include?("'")
string = string.split("'", -1).map do |substr|
@ -30,6 +47,14 @@ module Capybara
end
end
def tempwrap(path)
if path.is_a?(::XPath::Collection)
path.map { |p| p.to_xpath }
else
wrap(path).paths
end
end
def respond_to?(method)
new.respond_to?(method)
end