merging in current codebase

This commit is contained in:
gaffo 2008-12-08 23:48:00 -06:00
commit 24ad539671
70 changed files with 1952 additions and 772 deletions

View File

@ -1,40 +1,100 @@
== Trunk
== Trunk (Git)
* _IMPORTANT_ Breaking change:
* Removed init.rb auto-require of webrat/rails
* Removed auto-require of webrat/rails when requiring webrat when RAILS_ENV is
defined
In your env.rb file, ensure you have:
require "webrat/rails"
* Major enhancements
* Added selects_time, selects_date, and selects_datetime to API. (Ben Mabey, Ticket #36)
* Added select_time, select_date, and select_datetime to API. [#36] (Ben Mabey)
* Use Hpricot and REXML when not parsing with Nokogiri (on JRuby, for example)
* Minor enhancements
* Now sets the mode with configuration.mode in the block (gaffo, Ticket #85)
* Allow clicking links by id and id regexp (gaffo)
* Raise Webrat::PageLoadError when a failure occurs so that application exceptions can be more accurately tested (Ryan Briones)
* Helpful error message for missing option in select box. (Ben Mabey, Ticket #40)
* Now sets the mode with configuration.mode in the block (Mike Gaffney, Ticket #85)
* Detect if the document is XML or HTML using the Content-Type when in Rails mode
* Expose #selenium method for direct access to Selenium client
* Check nokogiri gem version before requiring nokogiri
* Include the Selenium server jar file in the gem (Bryan Helmkamp, Ben Schwarz)
* Added key_down, key_up and fire_event to Selenium session (Fernando Garcia)
* Fix outputing README during Rails plugin install (Fernando Garcia)
* Strip newlines when matching label text (Miha Filej)
* Add simple support for accessing Webrat's matchers from RSpec by requiring
"webrat/rspec-rails" (David Chelimsky)
* Support save_and_open_page in Windows and Cygwin (Mike Gaffney)
* Added RadioField#checked? to indicated whether or not a radio button is checked
(Luke Melia)
* Add spec:jruby rake task for running Webrat's spec suite with JRuby
* Added field_by_xpath to locate a Webrat::Field using arbitrary XPath expressions
* Helpful error message for missing option values [#40] (Ben Mabey)
* Add set_hidden_field method (Noah Davis, Bryan Helmkamp)
* Add submit_form method for submitting a form by ID (Noah Davis, Bryan Helmkamp)
* Switch to using Nokogiri.parse for simple XML/XHTML autodetection [#66]
* Removed Webrat.root method, which seemed to be unused
* Added Webrat.configure method for global Webrat configuration [#33]
(Mike Gaffney)
* Added automatic starting and stopping of the Selenium server and a Mongrel Rails
app server when using webrat/selenium
* Switch to using selenium-client gem and vendor selenium-server.jar (Luke Melia)
* Added gemspec so the gem builds on GitHub now
* Deprecate old style methods (fills_in is deprecated in favor of fill_in, etc.)
* Improcements to the README and RDoc (Bryan Helmkamp, Mike Gaffney)
* Allow clicking links by id and id regexp (Mike Gaffney)
* Raise Webrat::DisabledFieldError when attempting to manipulate a disabled field
* Raise Webrat::NotFoundErrors when an element is not found
* Raise Webrat::PageLoadError when a failure occurs so that application exceptions
can be more accurately tested (Ryan Briones)
* Helpful error message for missing option in select box. [#40] (Ben Mabey)
* Bug fixes
* Match against link _text_ which decodes character references.
Useful for multibyte languages like Japanese (moronatural@gmail.com)
* Fix params hash generation for Mechanize when Merb is not defined [#62]
* Expose some Webrat methods that were missing from the Webrat::Methods module
(Low Chin Chau)
* Allow mechanize session to pass through basic auth (Ryan Briones)
* Improvements to the Mechanize support (Jeremy Burks)
* Fix following fully qualified local links (Lawrence Pit)
* Fixed bug where Webrat would lose complex parameters (like foo[bar[baz]][])
in Merb due to not correctly merging Mashes. (Drew Colthorp)
* Extend Rails' ActionController::IntegrationTest instead of
ActionController::Integration::Session (Fixes using Webrat's #select method and
avoids usage of method_missing)
== 0.3.2 / 2008-11-08
* Minor enhancements
* 1 Minor enhancement
* Fixes behavior or have_tag when a block is passed. It passes the matched node(s) to the block for further specs again. (Carl Lerche)
* Fixes behavior or have_tag when a block is passed. It passes the matched node(s)
to the block for further specs again. (Carl Lerche)
== 0.3.1 / 2008-11-07
* Minor enhancements
* 1 Minor enhancement
* Use @_webrat_session instance variable instead of @session for Merb integration to avoid collisions
* Use @_webrat_session instance variable instead of @session for Merb integration
to avoid collisions
== 0.3.0 / 2008-11-07
* Major enhancements
* 4 Major enhancements
* Added Merb support (Gwyn Morfey, Jeremy Burks, Rob Kaufman, Yehuda Katz)
* Added experimental Selenium support (Luke Melia)
* Add have_selector, have_xpath, have_tag and contain matchers from Merb
* Switch from Hpricot to Nokogiri for XML parsing (thanks, Aaron Patterson)
* Minor enhancements
* 37 Minor enhancements
* Added #within for manipulating the current page within a selector scope
* Add support for file fields via #attaches_file method (Rails only at the moment) (Kyle Hargraves)
* Add support for file fields via #attaches_file method (Rails only at the moment)
(Kyle Hargraves)
* Add support for simulating SSL requests (Luke Melia)
* Added #basic_auth(user, pass) to support HTTP Basic Auth (Aslak Hellesøy)
* Added support for Sinatra and Rack (Aslak Hellesøy)
@ -47,24 +107,29 @@
* Allow clicking links by a regular expression
* Add #http_accept for including MIME type HTTP "Accept" headers (Ryan Briones)
* Add #header to support inclusion of custom HTTP headers (Ryan Briones)
* Consider response codes 200-499 as successful enough to not raise a Webrat error (David Leal)
* Consider response codes 200-499 as successful enough to not raise a Webrat error
(David Leal)
* Add support for clicking areas of an image map (Alex Lang)
* Support relative links, including href="?foo=bar" (Kyle Hargraves)
* Separated Rails-specific code from the Webrat core to make it easier to use Webrat with other environments
* Separated Rails-specific code from the Webrat core to make it easier to use
Webrat with other environments
* Alias visits as visit, clicks_link as click_link, etc. for better readability
* Raise error when trying to interact with a disabled form element (Luke Melia)
* Don't send disabled form elements to the server (Nicholas A. Evans)
* Display response body when the page load is not successful (David Leal)
* CGI escape form field values (Miha Filej)
* Add support for redirect_to :back by sending HTTP_REFERER headers (Hendrik Volkmer)
* Add support for redirect_to :back by sending HTTP_REFERER headers
(Hendrik Volkmer)
* Expose current DOM (as an Hpricot object) as #current_dom
* Add support for disabling JavaScript when clicking a link to enable testing of both JS
and non-JS implementations (Luke Melia and Bryan Helmkamp)
* Add support for disabling JavaScript when clicking a link to enable testing of
both JS and non-JS implementations (Luke Melia and Bryan Helmkamp)
* Support &nbsp's as spaces in matching link text (Luke Melia)
* Support button elements (Nick Sieger)
* Support matching select options by regexp (Kyle Hargraves)
* save_and_open_page rewrites css and image references to provide a friendlier debugging experience (Luke Melia)
* Added support for matching alt attributes in fields (primarly for clicks_button) (Aaron Quint)
* save_and_open_page rewrites css and image references to provide a friendlier
debugging experience (Luke Melia)
* Added support for matching alt attributes in fields (primarly for clicks_button)
(Aaron Quint)
* Support '&' in submitted values (Kyle Hargraves)
* Support clicking links by title (Dan Barry)
* Added missing spec for clicking image buttons (Tim Harper)
@ -72,7 +137,7 @@
* Add support to click_button for IDs (Gwyn Morfey)
* Miscellaneous core refactorings (Jan Suchal)
* Bug fixes
* 8 Bug fixes
* Fix initialization of WWW::Mechanize (Derek Kastner)
* Don't open blank pages in the browser (Kyle Hargraves)
@ -81,7 +146,8 @@
* Fix bug with empty select list option (Kyle Hargraves)
* Fix regression of not sending default values in password fields
* Don't explode if encountering inputs with no type attribute (assume text)
* Fix bug where choosing a radio button in a series with a default submitted the incorrect field value (Luke Melia)
* Fix bug where choosing a radio button in a series with a default submitted the
incorrect field value (Luke Melia)
== 0.2.0 / 2008-04-04
@ -100,23 +166,27 @@
* Added clicks_link_within(selector, link_text), allowing restricting link search
to within a given css selector (Luke Melia)
* Allow specifying the input name/label when doing a select (David Chelimsky)
* Raise a specific exception if the developer tries to manipulate form elements before loading a page (James Deville)
* Raise a specific exception if the developer tries to manipulate form elements
before loading a page (James Deville)
* Add support for alternate POST, PUT and DELETE link clicking (Kyle Hargraves)
* Change clicks_link to find the shortest matching link (Luke Melia)
* Improve matching for labels in potentially ambiguous cases
* 7 Bug fixes
* Fix incorrect serializing of collection inputs, i.e. name contains [] (Kamal Fariz Mahyuddi)
* Fix incorrect serializing of collection inputs, i.e. name contains []
(Kamal Fariz Mahyuddi)
* Serialize empty text field values just like browsers (Kamal Fariz Mahyuddi)
* Quick fix to avoid @dom not initialized warnings (Kamal Fariz Mahyuddi)
* Docfix: bad reference to #select method in README (Luke Melia)
* Ensure Rails-style checkboxes work properly (checkboxes followed by a hidden input with the same name)
* Ensure Rails-style checkboxes work properly (checkboxes followed by a hidden
input with the same name)
* Fix Edge Rails (a.k.a. 2.0 RC) compatibility (David Chelimsky)
* Support param hashes nested more than one level (David Chelimsky)
== 0.1.0 / 2007-11-28
* 1 major enhancement
* Birthday!

View File

@ -4,6 +4,7 @@
- http://groups.google.com/group/webrat
- http://webrat.lighthouseapp.com/
- http://github.com/brynary/webrat
- #webrat on Freenode
== Description
@ -15,7 +16,7 @@ web application.
* Browser Simulator for expressive, high level acceptance testing without the
performance hit and browser dependency of Selenium or Watir (See Webrat::Session)
* Use the same API for Browser Simulator and real Selenium tests using
Webrat::SeleniumSession when necessary (eg. for testing AJAX interactions)
Webrat::Selenium when necessary (eg. for testing AJAX interactions)
* Supports multiple Ruby web frameworks: Rails, Merb and Sinatra
* Supports popular test frameworks: RSpec, Cucumber, Test::Unit and Shoulda
* Webrat::Matchers API for verifying rendered HTML using CSS, XPath, etc.
@ -40,6 +41,14 @@ Behind the scenes, Webrat will ensure:
* If a URL is invalid, the test will fail.
* If a page load or form submission is unsuccessful, the test will fail.
== Installing Nokogiri
Users of Debian Linux (e.g. Ubuntu) need to run:
sudo apt-get install libxslt1-dev libxml2-dev.
Otherwise the Nokogiri gem, which Webrat depends on, won't install properly.
== Install for Rails
To install the latest release as a gem:

View File

@ -21,14 +21,14 @@ spec = Gem::Specification.new do |s|
s.bindir = "bin"
s.description = s.summary
s.require_path = "lib"
s.files = %w(History.txt init.rb install.rb MIT-LICENSE.txt README.rdoc Rakefile) + Dir["lib/**/*"]
s.files = %w(History.txt install.rb MIT-LICENSE.txt README.rdoc Rakefile) + Dir["lib/**/*"] + Dir["vendor/**/*"]
# rdoc
s.has_rdoc = true
s.extra_rdoc_files = %w(README.rdoc MIT-LICENSE.txt)
# Dependencies
s.add_dependency "nokogiri", ">= 1.0.3"
s.add_dependency "nokogiri", ">= 1.0.6"
s.rubyforge_project = "webrat"
end
@ -75,8 +75,13 @@ task :install_gem => [:clean, :package] do
sh "sudo gem install --local #{gem}"
end
desc "Delete generated RDoc"
task :clobber_docs do
FileUtils.rm_rf("doc")
end
desc "Generate RDoc"
task :docs do
task :docs => :clobber_docs do
system "hanna --title 'Webrat #{Webrat::VERSION} API Documentation'"
end
@ -85,4 +90,15 @@ task "spec:jruby" do
system "jruby -S rake spec"
end
desc "Run each spec in isolation to test for dependency issues"
task :spec_deps do
Dir["spec/**/*_spec.rb"].each do |test|
if !system("spec #{test} &> /dev/null")
puts "Dependency Issues: #{test}"
end
end
end
task :default => :spec
task :precommit => ["spec", "spec:jruby"]

View File

@ -1,3 +0,0 @@
if (RAILS_ENV =~ /^test/) || RAILS_ENV == "selenium"
require File.join(File.dirname(__FILE__), "lib", "webrat")
end

View File

@ -10,6 +10,8 @@ module Webrat
VERSION = '0.3.2.1'
def self.require_xml
gem "nokogiri", ">= 1.0.6"
if on_java?
# We need Nokogiri's CSS to XPath support, even if using REXML and Hpricot for parsing and searching
require "nokogiri/css"
@ -17,7 +19,7 @@ module Webrat
require "rexml/document"
else
require "nokogiri"
require "webrat/core/nokogiri"
require "webrat/core/xml/nokogiri"
end
end

View File

@ -1,13 +1,13 @@
require "webrat/core/configuration"
require "webrat/core/xml"
require "webrat/core/nokogiri"
require "webrat/core/xml/nokogiri"
require "webrat/core/logging"
require "webrat/core/form"
require "webrat/core/elements/form"
require "webrat/core/scope"
require "webrat/core/link"
require "webrat/core/area"
require "webrat/core/label"
require "webrat/core/select_option"
require "webrat/core/elements/link"
require "webrat/core/elements/area"
require "webrat/core/elements/label"
require "webrat/core/elements/select_option"
require "webrat/core/session"
require "webrat/core/methods"
require "webrat/core/matchers"

View File

@ -7,10 +7,15 @@ module Webrat
@@configuration = configuration
end
def self.configuration
def self.configuration # :nodoc:
@@configuration ||= Webrat::Configuration.new
end
# Webrat can be configured using the Webrat.configure method. For example:
#
# Webrat.configure do |config|
# config.parse_with_nokogiri = false
# end
class Configuration
RAILS_MODE = :rails
@ -24,7 +29,7 @@ module Webrat
attr_writer :parse_with_nokogiri
# Webrat's mode, set automatically when requiring webrat/rails, webrat/merb, etc.
attr_accessor :mode
attr_accessor :mode # :nodoc:
# Save and open pages with error status codes (500-599) in a browser? Defualts to true.
attr_writer :open_error_files

View File

@ -1,35 +1,22 @@
module Webrat
class Area #:nodoc:
require "webrat/core/elements/element"
def initialize(session, element)
@session = session
@element = element
module Webrat
class Area < Element #:nodoc:
def self.xpath_search
".//area"
end
def click(method = nil, options = {})
@session.request_page(absolute_href, :get, {})
end
def matches_text?(id_or_title)
matcher = /#{Regexp.escape(id_or_title.to_s)}/i
title =~ matcher || id =~ matcher
end
protected
def href
@element["href"]
Webrat::XML.attribute(@element, "href")
end
def title
@element["title"]
end
def id
@element["id"]
end
def absolute_href
if href =~ /^\?/
"#{@session.current_url}#{href}"

View File

@ -0,0 +1,29 @@
module Webrat
class Element # :nodoc:
def self.load_all(session, dom)
Webrat::XML.xpath_search(dom, xpath_search).map do |element|
load(session, element)
end
end
def self.load(session, element)
return nil if element.nil?
session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element)
end
attr_reader :element
def initialize(session, element)
@session = session
@element = element
end
def path
Webrat::XML.xpath_to(@element)
end
end
end

View File

@ -2,34 +2,57 @@ require "cgi"
require "webrat/core_extensions/blank"
require "webrat/core_extensions/nil_to_param"
require "webrat/core/elements/element"
module Webrat
# Raised when Webrat is asked to manipulate a disabled form field
class DisabledFieldError < WebratError
end
class Field #:nodoc:
def self.class_for_element(element)
if element.name == "input"
if %w[submit image].include?(element["type"])
field_class = "button"
else
field_class = element["type"] || "text" #default type; 'type' attribute is not mandatory
end
else
field_class = element.name
end
Webrat.const_get("#{field_class.capitalize}Field")
rescue NameError
raise "Invalid field element: #{element.inspect}"
end
class Field < Element #:nodoc:
attr_reader :value
def initialize(form, element)
@form = form
@element = element
def self.xpath_search
[".//button", ".//input", ".//textarea", ".//select"]
end
def self.field_classes
@field_classes || []
end
def self.inherited(klass)
@field_classes ||= []
@field_classes << klass
# raise args.inspect
end
def self.load(session, element)
return nil if element.nil?
session.elements[Webrat::XML.xpath_to(element)] ||= field_class(element).new(session, element)
end
def self.field_class(element)
case element.name
when "button" then ButtonField
when "select" then SelectField
when "textarea" then TextareaField
else
case Webrat::XML.attribute(element, "type")
when "checkbox" then CheckboxField
when "hidden" then HiddenField
when "radio" then RadioField
when "password" then PasswordField
when "file" then FileField
when "reset" then ResetField
when "submit" then ButtonField
when "image" then ButtonField
else TextField
end
end
end
def initialize(*args)
super
@value = default_value
end
@ -39,36 +62,11 @@ module Webrat
end
def id
@element["id"]
end
def path
@element.path
end
def matches_id?(id)
if id.is_a?(Regexp)
@element["id"] =~ id
else
@element["id"] == id.to_s
end
end
def matches_name?(name)
@element["name"] == name.to_s
end
def matches_label?(label_text)
return false if labels.empty?
labels.any? { |label| label.matches_text?(label_text) }
end
def matches_alt?(alt)
@element["alt"] =~ /^\W*#{Regexp.escape(alt.to_s)}/i
Webrat::XML.attribute(@element, "id")
end
def disabled?
@element.attributes.has_key?("disabled") && @element["disabled"] != 'false'
@element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false'
end
def raise_error_if_disabled
@ -99,8 +97,21 @@ module Webrat
protected
def form
Form.load(@session, form_element)
end
def form_element
parent = @element.parent
while parent.respond_to?(:parent)
return parent if parent.name == 'form'
parent = parent.parent
end
end
def name
@element["name"]
Webrat::XML.attribute(@element, "name")
end
def escaped_value
@ -108,7 +119,9 @@ module Webrat
end
def labels
@labels ||= label_elements.map { |element| Label.new(self, element) }
@labels ||= label_elements.map do |element|
Label.load(@session, element)
end
end
def label_elements
@ -125,14 +138,14 @@ module Webrat
end
unless id.blank?
@label_elements += Webrat::XML.css_search(@form.element, "label[@for='#{id}']")
@label_elements += Webrat::XML.xpath_search(form.element, ".//label[@for = '#{id}']")
end
@label_elements
end
def default_value
@element["value"]
Webrat::XML.attribute(@element, "value")
end
def replace_param_value(params, oval, nval)
@ -154,12 +167,8 @@ module Webrat
class ButtonField < Field #:nodoc:
def matches_text?(text)
@element.inner_html =~ /#{Regexp.escape(text.to_s)}/i
end
def matches_value?(value)
@element["value"] =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value) || matches_alt?(value)
def self.xpath_search
[".//button", ".//input[@type = 'submit']", ".//input[@type = 'image']"]
end
def to_param
@ -173,19 +182,23 @@ module Webrat
def click
raise_error_if_disabled
set(@element["value"]) unless @element["name"].blank?
@form.submit
set(Webrat::XML.attribute(@element, "value")) unless Webrat::XML.attribute(@element, "name").blank?
form.submit
end
end
class HiddenField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'hidden']"
end
def to_param
if collection_name?
super
else
checkbox_with_same_name = @form.field(name, CheckboxField)
checkbox_with_same_name = form.field_named(name, CheckboxField)
if checkbox_with_same_name.to_param.blank?
super
@ -205,6 +218,10 @@ module Webrat
class CheckboxField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'checkbox']"
end
def to_param
return nil if @value.nil?
super
@ -212,11 +229,11 @@ module Webrat
def check
raise_error_if_disabled
set(@element["value"] || "on")
set(Webrat::XML.attribute(@element, "value") || "on")
end
def checked?
@element["checked"] == "checked"
Webrat::XML.attribute(@element, "checked") == "checked"
end
def uncheck
@ -227,8 +244,8 @@ module Webrat
protected
def default_value
if @element["checked"] == "checked"
@element["value"] || "on"
if Webrat::XML.attribute(@element, "checked") == "checked"
Webrat::XML.attribute(@element, "value") || "on"
else
nil
end
@ -237,10 +254,19 @@ module Webrat
end
class PasswordField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'password']"
end
end
class RadioField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'radio']"
end
def to_param
return nil if @value.nil?
super
@ -252,22 +278,22 @@ module Webrat
option.set(nil)
end
set(@element["value"] || "on")
set(Webrat::XML.attribute(@element, "value") || "on")
end
def checked?
@element["checked"] == "checked"
Webrat::XML.attribute(@element, "checked") == "checked"
end
protected
def other_options
@form.fields.select { |f| f.name == name }
form.fields.select { |f| f.name == name }
end
def default_value
if @element["checked"] == "checked"
@element["value"] || "on"
if Webrat::XML.attribute(@element, "checked") == "checked"
Webrat::XML.attribute(@element, "value") || "on"
else
nil
end
@ -277,16 +303,24 @@ module Webrat
class TextareaField < Field #:nodoc:
def self.xpath_search
".//textarea"
end
protected
def default_value
@element.inner_html
Webrat::XML.inner_html(@element)
end
end
class FileField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'file']"
end
attr_accessor :content_type
def set(value, content_type = nil)
@ -315,36 +349,38 @@ module Webrat
end
class TextField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'text']", ".//input[not(@type)]"]
end
end
class ResetField < Field #:nodoc:
def self.xpath_search
".//input[@type = 'reset']"
end
end
class SelectField < Field #:nodoc:
def find_option(text)
options.detect { |o| o.matches_text?(text) }
def self.xpath_search
".//select"
end
def options
@options ||= SelectOption.load_all(@session, @element)
end
protected
def default_value
selected_options = Webrat::XML.css_search(@element, "option[@selected='selected']")
selected_options = Webrat::XML.css_search(@element, "option:first") if selected_options.empty?
selected_options = Webrat::XML.xpath_search(@element, ".//option[@selected = 'selected']")
selected_options = Webrat::XML.xpath_search(@element, ".//option[position() = 1]") if selected_options.empty?
selected_options.map do |option|
return "" if option.nil?
option["value"] || option.inner_html
Webrat::XML.attribute(option, "value") || Webrat::XML.inner_html(option)
end
end
def options
option_elements.map { |oe| SelectOption.new(self, oe) }
end
def option_elements
Webrat::XML.css_search(@element, "option")
end
end
end

View File

@ -0,0 +1,103 @@
require "webrat/core/elements/field"
require "webrat/core_extensions/blank"
require "webrat/core/elements/element"
require "webrat/core/locators/field_named_locator"
module Webrat
class Form < Element #:nodoc:
attr_reader :element
def self.xpath_search
".//form"
end
def fields
@fields ||= Field.load_all(@session, @element)
end
def submit
@session.request_page(form_action, form_method, params)
end
def field_named(name, *field_types)
Webrat::Locators::FieldNamedLocator.new(@session, dom, name, *field_types).locate
end
protected
def dom
Webrat::XML.xpath_at(@session.dom, path)
end
def fields_by_type(field_types)
if field_types.any?
fields.select { |f| field_types.include?(f.class) }
else
fields
end
end
def params
all_params = {}
fields.each do |field|
next if field.to_param.nil?
merge(all_params, field.to_param)
end
all_params
end
def form_method
Webrat::XML.attribute(@element, "method").blank? ? :get : Webrat::XML.attribute(@element, "method").downcase
end
def form_action
Webrat::XML.attribute(@element, "action").blank? ? @session.current_url : Webrat::XML.attribute(@element, "action")
end
def merge(all_params, new_param)
new_param.each do |key, value|
case all_params[key]
when *hash_classes
merge_hash_values(all_params[key], value)
when Array
all_params[key] += value
else
all_params[key] = value
end
end
end
def merge_hash_values(a, b) # :nodoc:
a.keys.each do |k|
if b.has_key?(k)
case [a[k], b[k]].map{|value| value.class}
when *hash_classes.zip(hash_classes)
a[k] = merge_hash_values(a[k], b[k])
b.delete(k)
when [Array, Array]
a[k] += b[k]
b.delete(k)
end
end
end
a.merge!(b)
end
def hash_classes
klasses = [Hash]
case Webrat.configuration.mode
when :rails
klasses << HashWithIndifferentAccess
when :merb
klasses << Mash
end
klasses
end
end
end

View File

@ -0,0 +1,31 @@
require "webrat/core/elements/element"
module Webrat
class Label < Element #:nodoc:
attr_reader :element
def self.xpath_search
".//label"
end
def for_id
Webrat::XML.attribute(@element, "for")
end
def field
Field.load(@session, field_element)
end
protected
def field_element
if for_id.blank?
Webrat::XML.xpath_at(@element, *Field.xpath_search)
else
Webrat::XML.css_search(@session.dom, "#" + for_id).first
end
end
end
end

View File

@ -1,11 +1,12 @@
require "webrat/core_extensions/blank"
module Webrat
class Link #:nodoc:
require "webrat/core/elements/element"
def initialize(session, element)
@session = session
@element = element
module Webrat
class Link < Element #:nodoc:
def self.xpath_search
".//a[@href]"
end
def click(options = {})
@ -21,35 +22,10 @@ module Webrat
end
end
def matches_text?(link_text)
if link_text.is_a?(Regexp)
matcher = link_text
else
matcher = /#{Regexp.escape(link_text.to_s)}/i
end
replace_nbsp(text) =~ matcher || replace_nbsp_ref(inner_html) =~ matcher || title =~ matcher
end
def matches_id?(id_or_regexp)
if id_or_regexp.is_a?(Regexp)
(id =~ id_or_regexp) ? true : false
else
(id == id_or_regexp) ? true : false
end
end
def inner_html
@element.inner_html
end
def text
@element.inner_text
end
protected
def id
@element['id']
Webrat::XML.attribute(@element, "id")
end
def data
@ -57,11 +33,11 @@ module Webrat
end
def title
@element['title']
Webrat::XML.attribute(@element, "title")
end
def href
@element["href"]
Webrat::XML.attribute(@element, "href")
end
def absolute_href
@ -81,7 +57,7 @@ module Webrat
end
def onclick
@element["onclick"]
Webrat::XML.attribute(@element, "onclick")
end
def http_method
@ -110,13 +86,5 @@ module Webrat
end
end
private
def replace_nbsp(str)
str.gsub([0xA0].pack('U'), ' ')
end
def replace_nbsp_ref(str)
str.gsub('&#xA0;',' ').gsub('&nbsp;', ' ')
end
end
end

View File

@ -0,0 +1,35 @@
require "webrat/core/elements/element"
module Webrat
class SelectOption < Element #:nodoc:
def self.xpath_search
".//option"
end
def choose
select.raise_error_if_disabled
select.set(value)
end
protected
def select
SelectField.load(@session, select_element)
end
def select_element
parent = @element.parent
while parent.respond_to?(:parent)
return parent if parent.name == 'select'
parent = parent.parent
end
end
def value
Webrat::XML.attribute(@element, "value") || Webrat::XML.inner_html(@element)
end
end
end

View File

@ -1,157 +0,0 @@
require "webrat/core/field"
require "webrat/core_extensions/blank"
module Webrat
class Form #:nodoc:
attr_reader :element
def initialize(session, element)
@session = session
@element = element
@fields = nil
end
def field(locator, *field_types)
field_with_id(locator, *field_types) ||
field_named(locator, *field_types) ||
field_labeled(locator, *field_types) ||
nil
end
def field_by_element(element, *field_types)
fields_by_type(field_types).detect { |possible_field| possible_field.path == element.path }
end
def find_select_option(option_text)
select_fields = fields_by_type([SelectField])
select_fields.each do |select_field|
result = select_field.find_option(option_text)
return result if result
end
nil
end
def find_button(value = nil)
return fields_by_type([ButtonField]).first if value.nil?
possible_buttons = fields_by_type([ButtonField])
possible_buttons.detect { |possible_button| possible_button.matches_id?(value) } ||
possible_buttons.detect { |possible_button| possible_button.matches_value?(value) }
end
def fields
return @fields if @fields
@fields = Webrat::XML.css_search(@element, "button", "input", "textarea", "select").collect do |field_element|
Field.class_for_element(field_element).new(self, field_element)
end
end
def labels
@labels ||= element.search("label").map { |element| Label.new(nil, element) }
end
def submit
@session.request_page(form_action, form_method, params)
end
def field_with_id(id, *field_types)
possible_fields = fields_by_type(field_types)
possible_fields.detect { |possible_field| possible_field.matches_id?(id) }
end
def field_named(name, *field_types)
possible_fields = fields_by_type(field_types)
possible_fields.detect { |possible_field| possible_field.matches_name?(name) }
end
def field_labeled(label, *field_types)
possible_fields = fields_by_type(field_types)
matching_fields = possible_fields.select do |possible_field|
possible_field.matches_label?(label)
end
matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length }
end
def label_matching(label_text)
labels.detect { |label| label.matches_text?(label_text) }
end
def matches_id?(id)
@element["id"] == id.to_s
end
protected
def fields_by_type(field_types)
if field_types.any?
fields.select { |f| field_types.include?(f.class) }
else
fields
end
end
def params
all_params = {}
fields.each do |field|
next if field.to_param.nil?
merge(all_params, field.to_param)
end
all_params
end
def form_method
@element["method"].blank? ? :get : @element["method"].downcase
end
def form_action
@element["action"].blank? ? @session.current_url : @element["action"]
end
def merge(all_params, new_param)
new_param.each do |key, value|
case all_params[key]
when *hash_classes
merge_hash_values(all_params[key], value)
when Array
all_params[key] += value
else
all_params[key] = value
end
end
end
def merge_hash_values(a, b) # :nodoc:
a.keys.each do |k|
if b.has_key?(k)
case [a[k], b[k]].map{|value| value.class}
when *hash_classes.zip(hash_classes)
a[k] = merge_hash_values(a[k], b[k])
b.delete(k)
when [Array, Array]
a[k] += b[k]
b.delete(k)
end
end
end
a.merge!(b)
end
def hash_classes
klasses = [Hash]
case Webrat.configuration.mode
when Webrat::Configuration::RAILS_MODE
klasses << HashWithIndifferentAccess
when Webrat::Configuration::MERB_MODE
klasses << Mash
end
klasses
end
end
end

View File

@ -1,26 +0,0 @@
module Webrat
class Label #:nodoc:
def initialize(field, element)
@field = field
@element = element
end
def matches_text?(label_text)
text =~ /^\W*#{Regexp.escape(label_text.to_s)}\b/i
end
def text
str = @element.inner_text
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")
str
end
def for_id
@element['for']
end
end
end

View File

@ -1,98 +1,19 @@
require "webrat/core_extensions/detect_mapped"
require "webrat/core/locators/area_locator"
require "webrat/core/locators/button_locator"
require "webrat/core/locators/field_labeled_locator"
require "webrat/core/locators/label_locator"
require "webrat/core/locators/field_named_locator"
require "webrat/core/locators/field_by_id_locator"
require "webrat/core/locators/select_option_locator"
require "webrat/core/locators/link_locator"
require "webrat/core/locators/field_locator"
require "webrat/core/locators/form_locator"
module Webrat
module Locators
def field_by_xpath(xpath, *field_types)
element = dom.at(xpath)
return nil unless element
forms.detect_mapped do |form|
form.field_by_element(element, *field_types)
end
end
def field_labeled(label, *field_types)
find_field_labeled(label, *field_types) ||
raise(NotFoundError.new("Could not find field labeled #{label.inspect}"))
end
def field_named(name, *field_types)
field_by_xpath("//*[@name='#{id}']", *field_types) ||
raise(NotFoundError.new("Could not find field named #{name.inspect}"))
end
def field_with_id(id, *field_types)
field_by_xpath("//*[@id='#{id}']", *field_types) ||
raise(NotFoundError.new("Could not find field with id #{id.inspect}"))
end
def field(id, *field_types) # :nodoc:
# This is the default locator strategy
field_by_xpath("//*[@id='#{id}']", *field_types) ||
field_by_xpath("//*[@name='#{id}']", *field_types) ||
field_labeled(id, *field_types) ||
raise(NotFoundError.new("Could not find field: #{args.inspect}"))
end
def find_field_labeled(label, *field_types) #:nodoc:
forms.detect_mapped do |form|
form.field_labeled(label, *field_types)
end
end
def find_select_option(option_text, id_or_name_or_label) #:nodoc:
if id_or_name_or_label
field = field(id_or_name_or_label, SelectField)
return field.find_option(option_text)
else
select_option = forms.detect_mapped do |form|
form.find_select_option(option_text)
end
return select_option if select_option
end
raise NotFoundError.new("Could not find option #{option_text.inspect}")
end
def find_button(value) #:nodoc:
button = forms.detect_mapped do |form|
form.find_button(value)
end
if button
return button
else
raise NotFoundError.new("Could not find button #{value.inspect}")
end
end
def find_area(area_name) #:nodoc:
areas.detect { |area| area.matches_text?(area_name) } ||
raise(NotFoundError.new("Could not find area with name #{area_name}"))
end
def find_link(text_or_title_or_id) #:nodoc:
matching_links = links.select do |possible_link|
possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id)
end
if matching_links.any?
matching_links.min { |a, b| a.text.length <=> b.text.length }
else
raise NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}")
end
end
def find_field_id_for_label(label_text) #:nodoc:
label = forms.detect_mapped { |form| form.label_matching(label_text) }
if label
label.for_id
else
raise NotFoundError.new("Could not find the label with text #{label_text}")
end
def field_by_xpath(xpath)
Field.load(@session, Webrat::XML.xpath_at(dom, xpath))
end
end

View File

@ -0,0 +1,38 @@
require "webrat/core/locators/locator"
module Webrat
module Locators
class AreaLocator < Locator # :nodoc:
def locate
Area.load(@session, area_element)
end
def area_element
area_elements.detect do |area_element|
Webrat::XML.attribute(area_element, "title") =~ matcher ||
Webrat::XML.attribute(area_element, "id") =~ matcher
end
end
def matcher
/#{Regexp.escape(@value.to_s)}/i
end
def area_elements
Webrat::XML.xpath_search(@dom, Area.xpath_search)
end
def error_message
"Could not find area with name #{@value}"
end
end
def find_area(id_or_title) #:nodoc:
AreaLocator.new(@session, dom, id_or_title).locate!
end
end
end

View File

@ -0,0 +1,54 @@
require "webrat/core/locators/locator"
module Webrat
module Locators
class ButtonLocator < Locator # :nodoc:
def locate
ButtonField.load(@session, button_element)
end
def button_element
button_elements.detect do |element|
@value.nil? ||
matches_id?(element) ||
matches_value?(element) ||
matches_html?(element) ||
matches_alt?(element)
end
end
def matches_id?(element)
(@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) ||
(!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s)
end
def matches_value?(element)
Webrat::XML.attribute(element, "value") =~ /^\W*#{Regexp.escape(@value.to_s)}/i
end
def matches_html?(element)
Webrat::XML.inner_html(element) =~ /#{Regexp.escape(@value.to_s)}/i
end
def matches_alt?(element)
Webrat::XML.attribute(element, "alt") =~ /^\W*#{Regexp.escape(@value.to_s)}/i
end
def button_elements
Webrat::XML.xpath_search(@dom, *ButtonField.xpath_search)
end
def error_message
"Could not find button #{@value.inspect}"
end
end
def find_button(value) #:nodoc:
ButtonLocator.new(@session, dom, value).locate!
end
end
end

View File

@ -0,0 +1,37 @@
require "webrat/core/locators/locator"
module Webrat
module Locators
class FieldByIdLocator < Locator # :nodoc:
def locate
Field.load(@session, field_element)
end
def field_element
field_elements.detect do |field_element|
if @value.is_a?(Regexp)
Webrat::XML.attribute(field_element, "id") =~ @value
else
Webrat::XML.attribute(field_element, "id") == @value.to_s
end
end
end
def field_elements
Webrat::XML.xpath_search(@dom, *Field.xpath_search)
end
def error_message
"Could not find field with id #{@value.inspect}"
end
end
def field_with_id(id, *field_types)
FieldByIdLocator.new(@session, dom, id, *field_types).locate!
end
end
end

View File

@ -0,0 +1,50 @@
require "webrat/core_extensions/detect_mapped"
require "webrat/core/locators/locator"
module Webrat
module Locators
class FieldLabeledLocator < Locator # :nodoc:
def locate
matching_labels.any? && matching_labels.first.field
end
def matching_labels
matching_label_elements.sort_by do |label_element|
text(label_element).length
end.map do |label_element|
Label.load(@session, label_element)
end
end
def matching_label_elements
label_elements.select do |label_element|
text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i
end
end
def label_elements
Webrat::XML.xpath_search(@dom, Label.xpath_search)
end
def error_message
"Could not find field labeled #{@value.inspect}"
end
def text(element)
str = Webrat::XML.all_inner_text(element)
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")
str
end
end
def field_labeled(label, *field_types)
FieldLabeledLocator.new(@session, dom, label, *field_types).locate!
end
end
end

View File

@ -0,0 +1,25 @@
require "webrat/core/locators/locator"
module Webrat
module Locators
class FieldLocator < Locator # :nodoc:
def locate
FieldByIdLocator.new(@session, @dom, @value).locate ||
FieldNamedLocator.new(@session, @dom, @value, *@field_types).locate ||
FieldLabeledLocator.new(@session, @dom, @value, *@field_types).locate
end
def error_message
"Could not find field: #{@value.inspect}"
end
end
def field(*args) # :nodoc:
FieldLocator.new(@session, dom, *args).locate!
end
end
end

View File

@ -0,0 +1,41 @@
require "webrat/core/locators/locator"
module Webrat
module Locators
class FieldNamedLocator < Locator # :nodoc:
def locate
Field.load(@session, field_element)
end
def field_element
field_elements.detect do |field_element|
Webrat::XML.attribute(field_element, "name") == @value.to_s
end
end
def field_elements
Webrat::XML.xpath_search(@dom, *xpath_searches)
end
def xpath_searches
if @field_types.any?
@field_types.map { |field_type| field_type.xpath_search }.flatten
else
Array(Field.xpath_search)
end
end
def error_message
"Could not find field named #{@value.inspect}"
end
end
def field_named(name, *field_types)
FieldNamedLocator.new(@session, dom, name, *field_types).locate!
end
end
end

View File

@ -0,0 +1,19 @@
require "webrat/core/locators/locator"
module Webrat
module Locators
class FormLocator < Locator # :nodoc:
def locate
Form.load(@session, form_element)
end
def form_element
Webrat::XML.css_at(@dom, "#" + @value)
end
end
end
end

View File

@ -0,0 +1,34 @@
require "webrat/core_extensions/detect_mapped"
require "webrat/core/locators/locator"
module Webrat
module Locators
class LabelLocator < Locator # :nodoc:
def locate
Label.load(@session, label_element)
end
def label_element
label_elements.detect do |label_element|
text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i
end
end
def label_elements
Webrat::XML.xpath_search(@dom, Label.xpath_search)
end
def text(label_element)
str = Webrat::XML.all_inner_text(label_element)
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")
str
end
end
end
end

View File

@ -0,0 +1,66 @@
require "webrat/core/locators/locator"
module Webrat
module Locators
class LinkLocator < Locator # :nodoc:
def locate
Link.load(@session, link_element)
end
def link_element
matching_links.min { |a, b| Webrat::XML.all_inner_text(a).length <=> Webrat::XML.all_inner_text(b).length }
end
def matching_links
@matching_links ||= link_elements.select do |link_element|
matches_text?(link_element) ||
matches_id?(link_element)
end
end
def matches_text?(link)
if @value.is_a?(Regexp)
matcher = @value
else
matcher = /#{Regexp.escape(@value.to_s)}/i
end
replace_nbsp(Webrat::XML.all_inner_text(link)) =~ matcher ||
replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher ||
Webrat::XML.attribute(link, "title")=~ matcher
end
def matches_id?(link)
if @value.is_a?(Regexp)
(Webrat::XML.attribute(link, "id") =~ @value) ? true : false
else
(Webrat::XML.attribute(link, "id") == @value) ? true : false
end
end
def link_elements
Webrat::XML.xpath_search(@dom, *Link.xpath_search)
end
def replace_nbsp(str)
str.gsub([0xA0].pack('U'), ' ')
end
def replace_nbsp_ref(str)
str.gsub('&#xA0;',' ').gsub('&nbsp;', ' ')
end
def error_message
"Could not find link with text or title or id #{@value.inspect}"
end
end
def find_link(text_or_title_or_id) #:nodoc:
LinkLocator.new(@session, dom, text_or_title_or_id).locate!
end
end
end

View File

@ -0,0 +1,20 @@
module Webrat
module Locators
class Locator # :nodoc:
def initialize(session, dom, value, *field_types)
@session = session
@dom = dom
@value = value
@field_types = field_types
end
def locate!
locate || raise(NotFoundError.new(error_message))
end
end
end
end

View File

@ -0,0 +1,59 @@
require "webrat/core_extensions/detect_mapped"
require "webrat/core/locators/locator"
module Webrat
module Locators
class SelectOptionLocator < Locator # :nodoc:
def initialize(session, dom, option_text, id_or_name_or_label)
@session = session
@dom = dom
@option_text = option_text
@id_or_name_or_label = id_or_name_or_label
end
def locate
if @id_or_name_or_label
field = FieldLocator.new(@session, @dom, @id_or_name_or_label, SelectField).locate!
field.options.detect do |o|
if @option_text.is_a?(Regexp)
Webrat::XML.inner_html(o.element) =~ @option_text
else
Webrat::XML.inner_html(o.element) == @option_text.to_s
end
end
else
option_element = option_elements.detect do |o|
if @option_text.is_a?(Regexp)
Webrat::XML.inner_html(o) =~ @option_text
else
Webrat::XML.inner_html(o) == @option_text.to_s
end
end
SelectOption.load(@session, option_element)
end
end
def option_elements
Webrat::XML.xpath_search(@dom, *SelectOption.xpath_search)
end
def error_message
if @id_or_name_or_label
"The '#{@option_text}' option was not found in the #{@id_or_name_or_label.inspect} select box"
else
"Could not find option #{@option_text.inspect}"
end
end
end
def select_option(option_text, id_or_name_or_label = nil) #:nodoc:
SelectOptionLocator.new(@session, dom, option_text, id_or_name_or_label).locate!
end
end
end

View File

@ -7,8 +7,13 @@ module Webrat
end
def matches?(stringlike)
@document = Webrat::XML.document(stringlike)
@element = @document.inner_text
if Webrat.configuration.parse_with_nokogiri?
@document = Webrat.nokogiri_document(stringlike)
else
@document = Webrat.hpricot_document(stringlike)
end
@element = Webrat::XML.inner_text(@document)
case @content
when String

View File

@ -1,5 +1,5 @@
require "webrat/core/nokogiri"
require "webrat/core/rexml"
require "webrat/core/xml/nokogiri"
require "webrat/core/xml/rexml"
module Webrat
module Matchers

View File

@ -7,13 +7,17 @@ module Webrat
def #{meth}(*args, &blk)
webrat_session.#{meth}(*args, &blk)
end
RUBY
end
end
def webrat
webrat_session
end
def webrat_session
@_webrat_session ||= ::Webrat.session_class.new(self)
end
RUBY
end
end
# all of these methods delegate to the @session, which should
# be created transparently.
@ -32,8 +36,6 @@ module Webrat
:chooses, :choose,
:selects, :select,
:attaches_file, :attach_file,
:cookies,
:response,
:current_page,
:current_url,
:clicks_link, :click_link,
@ -42,12 +44,16 @@ module Webrat
:reload, :reloads,
:clicks_link_within, :click_link_within,
:field_labeled,
:select_option,
:set_hidden_field, :submit_form,
:request_page, :current_dom,
:selects_date, :selects_time, :selects_datetime,
:select_date, :select_time, :select_datetime,
:wait_for_page_to_load,
:field_by_xpath
:field_by_xpath,
:field_with_id,
:selenium,
:simulate, :automate
end

View File

@ -1,4 +1,4 @@
require "webrat/core/form"
require "webrat/core/elements/form"
require "webrat/core/locators"
require "webrat/core_extensions/deprecate"
@ -25,6 +25,8 @@ module Webrat
end
end
attr_reader :session
def initialize(session, &block) #:nodoc:
@session = session
instance_eval(&block) if block_given?
@ -96,12 +98,7 @@ module Webrat
# select "February", :from => "event_month"
# select "February", :from => "Event Month"
def select(option_text, options = {})
if option = find_select_option(option_text, options[:from])
option.choose
else
select_box_text = options[:from] ? " in the '#{options[:from]}' select box" : ''
raise NotFoundError.new("The '#{option_text}' option was not found#{select_box_text}")
end
select_option(option_text, options[:from]).choose
end
webrat_deprecate :selects, :select
@ -134,7 +131,7 @@ module Webrat
date_to_select : Date.parse(date_to_select)
id_prefix = locate_id_prefix(options) do
year_field = field_by_xpath("//*[contains(@id, '_#{DATE_TIME_SUFFIXES[:year]}')]")
year_field = FieldByIdLocator.new(@session, dom, /(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/).locate
raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/
$1
end
@ -168,7 +165,7 @@ module Webrat
time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)
id_prefix = locate_id_prefix(options) do
hour_field = field_by_xpath("//*[contains(@id, '_#{DATE_TIME_SUFFIXES[:hour]}')]")
hour_field = FieldByIdLocator.new(@session, dom, /(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/).locate
raise NotFoundError.new("No time fields were found") unless hour_field && hour_field.id =~ /(.*?)_4i/
$1
end
@ -190,7 +187,7 @@ module Webrat
def select_datetime(time_to_select, options ={})
time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)
options[:id_prefix] ||= (options[:from] ? field_by_xpath("//*[@id='#{options[:from]}']") : nil)
options[:id_prefix] ||= (options[:from] ? FieldByIdLocator.new(@session, dom, options[:from]).locate : nil)
select_date time, options
select_time time, options
@ -263,8 +260,7 @@ module Webrat
webrat_deprecate :clicks_button, :click_button
def submit_form(id)
form = forms.detect { |f| f.matches_id?(id) }
form.submit
FormLocator.new(@session, dom, id).locate.submit
end
def dom # :nodoc:
@ -283,13 +279,25 @@ module Webrat
def page_dom #:nodoc:
return @response.dom if @response.respond_to?(:dom)
dom = Webrat::XML.document(@response_body)
if @session.xml_content_type?
dom = Webrat::XML.xml_document(@response_body)
else
dom = Webrat::XML.html_document(@response_body)
end
Webrat.define_dom_method(@response, dom)
return dom
end
def scoped_dom #:nodoc:
Webrat::XML.document(@scope.dom.search(@selector).first.to_html)
source = Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first)
if @session.xml_content_type?
Webrat::XML.xml_document(source)
else
Webrat::XML.html_document(source)
end
end
def locate_field(field_locator, *field_types) #:nodoc:
@ -302,27 +310,20 @@ module Webrat
def locate_id_prefix(options, &location_strategy) #:nodoc:
return options[:id_prefix] if options[:id_prefix]
id_prefix = options[:from] ? find_field_id_for_label(options[:from]) : yield
end
def areas #:nodoc:
Webrat::XML.css_search(dom, "area").map do |element|
Area.new(@session, element)
if options[:from]
if (label = LabelLocator.new(@session, dom, options[:from]).locate)
label.for_id
else
raise NotFoundError.new("Could not find the label with text #{options[:from]}")
end
end
def links #:nodoc:
Webrat::XML.css_search(dom, "a[@href]").map do |link_element|
Link.new(@session, link_element)
else
yield
end
end
def forms #:nodoc:
return @forms if @forms
@forms = Webrat::XML.css_search(dom, "form").map do |form_element|
Form.new(@session, form_element)
end
@forms ||= Form.load_all(@session, dom)
end
end

View File

@ -1,29 +0,0 @@
module Webrat
class SelectOption #:nodoc:
def initialize(select, element)
@select = select
@element = element
end
def matches_text?(text)
if text.is_a?(Regexp)
@element.inner_html =~ text
else
@element.inner_html == text.to_s
end
end
def choose
@select.raise_error_if_disabled
@select.set(value)
end
protected
def value
@element["value"] || @element.inner_html
end
end
end

View File

@ -32,6 +32,7 @@ module Webrat
include Logging
attr_reader :current_url
attr_reader :elements
def initialize(context = nil) #:nodoc:
@http_method = :get
@ -39,6 +40,8 @@ module Webrat
@default_headers = {}
@custom_headers = {}
@context = context
reset
end
# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
@ -110,8 +113,8 @@ module Webrat
save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
@_scopes = nil
@_page_scope = nil
reset
@current_url = url
@http_method = http_method
@data = data
@ -179,7 +182,7 @@ module Webrat
end
end
def rewrite_css_and_image_references(response_html) #:nodoc
def rewrite_css_and_image_references(response_html) # :nodoc:
return response_html unless doc_root
response_html.gsub(/"\/(stylesheets|images)/, doc_root + '/\1')
end
@ -197,6 +200,24 @@ module Webrat
@_page_scope ||= Scope.from_page(self, response, response_body)
end
def dom
page_scope.dom
end
def xml_content_type?
false
end
def simulate
return if Webrat.configuration.mode == :selenium
yield
end
def automate
return unless Webrat.configuration.mode == :selenium
yield
end
def_delegators :current_scope, :fill_in, :fills_in
def_delegators :current_scope, :set_hidden_field
def_delegators :current_scope, :submit_form
@ -215,8 +236,17 @@ module Webrat
def_delegators :current_scope, :should_not_see
def_delegators :current_scope, :field_labeled
def_delegators :current_scope, :field_by_xpath
def_delegators :current_scope, :field_with_id
def_delegators :current_scope, :select_option
private
def reset
@elements = {}
@_scopes = nil
@_page_scope = nil
end
# accessor for testing
def ruby_platform
RUBY_PLATFORM

View File

@ -1,3 +1,7 @@
require "webrat/core/xml/nokogiri"
require "webrat/core/xml/hpricot"
require "webrat/core/xml/rexml"
module Webrat #:nodoc:
module XML #:nodoc:
@ -5,30 +9,106 @@ module Webrat #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
Webrat.nokogiri_document(stringlike)
else
return stringlike.dom if stringlike.respond_to?(:dom)
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
end
end
if Hpricot::Doc === stringlike
stringlike
elsif Hpricot::Elements === stringlike
stringlike
elsif StringIO === stringlike
Hpricot(stringlike.string)
elsif stringlike.respond_to?(:body)
Hpricot(stringlike.body.to_s)
def self.html_document(stringlike) #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
Webrat.html_nokogiri_document(stringlike)
else
Hpricot(stringlike.to_s)
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
end
end
def self.xml_document(stringlike) #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
Webrat.xml_nokogiri_document(stringlike)
else
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
end
end
def self.to_html(element)
if Webrat.configuration.parse_with_nokogiri?
element.to_html
else
element.to_s
end
end
def self.inner_html(element)
if Webrat.configuration.parse_with_nokogiri?
element.inner_html
else
element.text
end
end
def self.all_inner_text(element)
if Webrat.configuration.parse_with_nokogiri?
element.inner_text
else
Hpricot(element.to_s).children.first.inner_text
end
end
def self.inner_text(element)
if Webrat.configuration.parse_with_nokogiri?
element.inner_text
else
if defined?(Hpricot::Doc) && element.is_a?(Hpricot::Doc)
element.inner_text
else
element.text
end
end
end
def self.xpath_to(element)
if Webrat.configuration.parse_with_nokogiri?
element.path
else
element.xpath
end
end
def self.attribute(element, attribute_name)
return element[attribute_name] if element.is_a?(Hash)
if Webrat.configuration.parse_with_nokogiri?
element[attribute_name]
else
element.attributes[attribute_name]
end
end
def self.xpath_at(*args)
xpath_search(*args).first
end
def self.css_at(*args)
css_search(*args).first
end
def self.xpath_search(element, *searches)
searches.flatten.map do |search|
if Webrat.configuration.parse_with_nokogiri?
element.xpath(search)
else
REXML::XPath.match(element, search)
end
end.flatten.compact
end
def self.css_search(element, *searches) #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
element.css(*searches)
else
searches.map do |search|
element.search(search)
end.flatten.compact
xpath_search(element, css_to_xpath(*searches))
end
def self.css_to_xpath(*selectors)
selectors.map do |rule|
Nokogiri::CSS.xpath_for(rule, :prefix => ".//")
end.flatten.uniq
end
end

View File

@ -0,0 +1,19 @@
module Webrat
def self.hpricot_document(stringlike)
return stringlike.dom if stringlike.respond_to?(:dom)
if Hpricot::Doc === stringlike
stringlike
elsif Hpricot::Elements === stringlike
stringlike
elsif StringIO === stringlike
Hpricot(stringlike.string)
elsif stringlike.respond_to?(:body)
Hpricot(stringlike.body.to_s)
else
Hpricot(stringlike.to_s)
end
end
end

View File

@ -18,6 +18,38 @@ module Webrat
end
end
def self.html_nokogiri_document(stringlike) #:nodoc:
return stringlike.dom if stringlike.respond_to?(:dom)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::HTML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::HTML(stringlike.body.to_s)
else
Nokogiri::HTML(stringlike.to_s)
end
end
def self.xml_nokogiri_document(stringlike) #:nodoc:
return stringlike.dom if stringlike.respond_to?(:dom)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::XML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::XML(stringlike.body.to_s)
else
Nokogiri::XML(stringlike.to_s)
end
end
def self.define_dom_method(object, dom) #:nodoc:
object.meta_class.send(:define_method, :dom) do
dom

View File

@ -1,4 +1,5 @@
require "webrat"
require "action_controller/integration"
module Webrat
class RailsSession < Session #:nodoc:
@ -35,6 +36,10 @@ module Webrat
response.code.to_i
end
def xml_content_type?
response.headers["Content-Type"].to_s =~ /xml/
end
protected
def integration_session
@ -71,13 +76,15 @@ end
module ActionController #:nodoc:
module Integration #:nodoc:
class Session #:nodoc:
Session.class_eval do
unless instance_methods.include?("put_via_redirect")
require "webrat/rails/redirect_actions"
include Webrat::RedirectActions
end
include Webrat::Methods
end
end
IntegrationTest.class_eval do
include Webrat::Methods
include Webrat::Matchers
end
end

View File

@ -1,5 +1,5 @@
# Supports using the matchers in controller, helper, and view specs if you're
# using rspec-rails. Just add a require statement to spec/spec_helper.rb:
# using rspec-rails. Just add a require statement to spec/spec_helper.rb or env.rb:
#
# require 'webrat/rspec-rails'
#

View File

@ -2,6 +2,7 @@ require "webrat"
gem "selenium-client", ">=1.2.9"
require "selenium/client"
require "webrat/selenium/selenium_session"
require "webrat/selenium/matchers"
module Webrat
@ -34,24 +35,68 @@ module Webrat
system "mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}"
end
module Selenium #:nodoc:
# To use Webrat's Selenium support, you'll need the selenium-client gem installed.
# Activate it with (for example, in your <tt>env.rb</tt>):
#
# require "webrat/selenium"
#
# Then, if you're using Cucumber, configure it to use a
# <tt>Webrat::Selenium::Rails::World</tt> as the scenario context by adding
# the following to <tt>env.rb</tt>:
#
# World do
# Webrat::Selenium::Rails::World.new
# end
#
# == Dropping down to the selenium-client API
#
# If you ever need to do something with Selenium not provided in the Webrat API,
# you can always drop down to the selenium-client API using the <tt>selenium</tt> method.
# For example:
#
# When "I drag the photo to the left" do
# selenium.dragdrop("id=photo_123", "+350, 0")
# end
#
# == Auto-starting of the mongrel and java server
#
# Webrat will automatically start the Selenium Java server process and an instance
# of Mongrel when a test is run. The Mongrel will run in the "selenium" environment
# instead of "test", so ensure you've got that defined, and will run on port 3001.
#
# == Waiting
#
# In order to make writing Selenium tests as easy as possible, Webrat will automatically
# wait for the correct elements to exist on the page when trying to manipulate them
# with methods like <tt>fill_in</tt>, etc. In general, this means you should be able to write
# your Webrat::Selenium tests ignoring the concurrency issues that can plague in-browser
# testing, so long as you're using the Webrat API.
module Selenium
module Rails #:nodoc:
class World < ::ActionController::IntegrationTest
include Webrat::Selenium::Matchers
def initialize #:nodoc:
@_result = Test::Unit::TestResult.new
end
def response
webrat_session.response
end
def wait_for(*args, &block)
webrat_session.wait_for(*args, &block)
end
end
end
end
end
module ::ActionController #:nodoc:
module Integration #:nodoc:
class Session #:nodoc:
module ActionController #:nodoc:
IntegrationTest.class_eval do
include Webrat::Methods
end
end
end

View File

@ -0,0 +1,108 @@
module Webrat
module Selenium
module Matchers
class HaveXpath
def initialize(expected)
@expected = expected
end
def matches?(response)
response.session.wait_for do
response.selenium.is_element_present("xpath=#{@expected}")
end
end
# ==== Returns
# String:: The failure message.
def failure_message
"expected following text to match xpath #{@expected}:\n#{@document}"
end
# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected following text to not match xpath #{@expected}:\n#{@document}"
end
end
def have_xpath(xpath)
HaveXpath.new(xpath)
end
class HaveSelector
def initialize(expected)
@expected = expected
end
def matches?(response)
response.session.wait_for do
response.selenium.is_element_present("css=#{@expected}")
end
end
# ==== Returns
# String:: The failure message.
def failure_message
"expected following text to match selector #{@expected}:\n#{@document}"
end
# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected following text to not match selector #{@expected}:\n#{@document}"
end
end
def have_selector(content)
HaveSelector.new(content)
end
class HasContent #:nodoc:
def initialize(content)
@content = content
end
def matches?(response)
if @content.is_a?(Regexp)
text_finder = "regexp:#{@content.source}"
else
text_finder = @content
end
response.session.wait_for do
response.selenium.is_text_present(text_finder)
end
end
# ==== Returns
# String:: The failure message.
def failure_message
"expected the following element's content to #{content_message}:\n#{@element}"
end
# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected the following element's content to not #{content_message}:\n#{@element}"
end
def content_message
case @content
when String
"include \"#{@content}\""
when Regexp
"match #{@content.inspect}"
end
end
end
# Matches the contents of an HTML document with
# whatever string is supplied
def contain(content)
HasContent.new(content)
end
end
end
end

View File

@ -1,9 +1,31 @@
module Webrat
class TimeoutError < WebratError
end
class SeleniumResponse
attr_reader :body
attr_reader :session
def initialize(session, body)
@session = session
@body = body
end
def selenium
session.selenium
end
end
class SeleniumSession
def initialize(*args) # :nodoc:
extend_selenium
define_location_strategies
end
def simulate
end
def automate
yield
end
def visit(url)
@ -14,11 +36,16 @@ module Webrat
def fill_in(field_identifier, options)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.wait_for_element locator, 5
selenium.type(locator, "#{options[:with]}")
end
webrat_deprecate :fills_in, :fill_in
def response
SeleniumResponse.new(self, response_body)
end
def response_body #:nodoc:
selenium.get_html_source
end
@ -30,54 +57,31 @@ module Webrat
pattern = adjust_if_regexp(button_text_or_regexp)
end
pattern ||= '*'
selenium.click("button=#{pattern}")
wait_for_result(options[:wait])
locator = "button=#{pattern}"
selenium.wait_for_element locator, 5
selenium.click locator
end
webrat_deprecate :clicks_button, :click_button
def click_link(link_text_or_regexp, options = {})
pattern = adjust_if_regexp(link_text_or_regexp)
selenium.click("webratlink=#{pattern}")
wait_for_result(options[:wait])
locator = "webratlink=#{pattern}"
selenium.wait_for_element locator, 5
selenium.click locator
end
webrat_deprecate :clicks_link, :click_link
def click_link_within(selector, link_text, options = {})
selenium.click("webratlinkwithin=#{selector}|#{link_text}")
wait_for_result(options[:wait])
locator = "webratlinkwithin=#{selector}|#{link_text}"
selenium.wait_for_element locator, 5
selenium.click locator
end
webrat_deprecate :clicks_link_within, :click_link_within
def wait_for_result(wait_type)
if wait_type == :ajax
wait_for_ajax
elsif wait_type == :effects
wait_for_effects
else
wait_for_page_to_load
end
end
def wait_for_page_to_load(timeout = 15000)
selenium.wait_for_page_to_load(timeout)
end
def wait_for_ajax(timeout = 15000)
selenium.wait_for_condition "Ajax.activeRequestCount == 0", timeout
end
def wait_for_effects(timeout = 15000)
selenium.wait_for_condition "window.Effect.Queue.size() == 0", timeout
end
def wait_for_ajax_and_effects
wait_for_ajax
wait_for_effects
end
def select(option_text, options = {})
id_or_name_or_label = options[:from]
@ -86,31 +90,29 @@ module Webrat
else
select_locator = "webratselectwithoption=#{option_text}"
end
selenium.wait_for_element select_locator, 5
selenium.select(select_locator, option_text)
end
webrat_deprecate :selects, :select
def choose(label_text)
selenium.click("webrat=#{label_text}")
locator = "webrat=#{label_text}"
selenium.wait_for_element locator, 5
selenium.click locator
end
webrat_deprecate :chooses, :choose
def check(label_text)
selenium.check("webrat=#{label_text}")
locator = "webrat=#{label_text}"
selenium.wait_for_element locator, 5
selenium.check locator
end
webrat_deprecate :checks, :check
def is_ordered(*args) #:nodoc:
selenium.is_ordered(*args)
end
def dragdrop(*args) #:nodoc:
selenium.dragdrop(*args)
end
def fire_event(field_identifier, event)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.fire_event(locator, "#{event}")
@ -126,12 +128,38 @@ module Webrat
selenium.key_up(locator, key_code)
end
def browser
def wait_for(params={})
timeout = params[:timeout] || 5
message = params[:message] || "Timeout exceeded"
begin_time = Time.now
while (Time.now - begin_time) < timeout
value = nil
begin
value = yield
rescue ::Spec::Expectations::ExpectationNotMetError, ::Selenium::CommandError, Webrat::WebratError
value = nil
end
return value if value
sleep 0.25
end
raise Webrat::TimeoutError.new(message + " (after #{timeout} sec)")
true
end
def selenium
return $browser if $browser
setup
$browser
end
webrat_deprecate :browser, :selenium
protected
def setup #:nodoc:
@ -140,15 +168,13 @@ module Webrat
Webrat.start_app_server
end
$browser = ::Selenium::Client::Driver.new("localhost", 4444, "*firefox", "http://0.0.0.0:3001")
$browser.set_speed(0)
$browser.start
teardown_at_exit
end
def selenium #:nodoc:
browser
extend_selenium
define_location_strategies
end
def teardown_at_exit #:nodoc:

View File

@ -12,9 +12,11 @@ describe "Basic Auth HTTP headers" do
it "should be present in form submits" do
with_html <<-HTML
<html>
<form method="post" action="/form1">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
click_button

View File

@ -26,12 +26,14 @@ describe "check" do
it "should check rails style checkboxes" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" />
<input name="user[tos]" type="hidden" value="0" />
<label for="user_tos">TOS</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
@ -86,8 +88,10 @@ end
describe "uncheck" do
it "should fail if no checkbox found" do
with_html <<-HTML
<html>
<form method="post" action="/login">
</form>
</html>
HTML
lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError)
@ -95,9 +99,11 @@ describe "uncheck" do
it "should fail if input is not a checkbox" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="text" name="remember_me" />
</form>
</html>
HTML
lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError)
@ -105,22 +111,26 @@ describe "uncheck" do
it "should fail if the checkbox is disabled" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" checked="checked" disabled="disabled" />
<input type="submit" />
</form>
</html>
HTML
lambda { uncheck "remember_me" }.should raise_error(Webrat::DisabledFieldError)
end
it "should uncheck rails style checkboxes" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
<input name="user[tos]" type="hidden" value="0" />
<label for="user_tos">TOS</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
check "TOS"
@ -130,10 +140,12 @@ describe "uncheck" do
it "should result in value not being posted" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="remember_me" value="yes" checked="checked" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", {})
uncheck "remember_me"

View File

@ -3,8 +3,10 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "choose" do
it "should fail if no radio buttons found" do
with_html <<-HTML
<html>
<form method="post" action="/login">
</form>
</html>
HTML
lambda { choose "first option" }.should raise_error(Webrat::NotFoundError)
@ -12,9 +14,11 @@ describe "choose" do
it "should fail if input is not a radio button" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="text" name="first_option" />
</form>
</html>
HTML
lambda { choose "first_option" }.should raise_error(Webrat::NotFoundError)
@ -22,6 +26,7 @@ describe "choose" do
it "should check rails style radio buttons" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
@ -29,6 +34,7 @@ describe "choose" do
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "M"})
choose "Male"
@ -37,6 +43,7 @@ describe "choose" do
it "should only submit last chosen value" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
@ -44,6 +51,7 @@ describe "choose" do
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "M"})
choose "Female"
@ -53,10 +61,12 @@ describe "choose" do
it "should fail if the radio button is disabled" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="radio" name="first_option" disabled="disabled" />
<input type="submit" />
</form>
</html>
HTML
lambda { choose "first_option" }.should raise_error(Webrat::DisabledFieldError)
@ -64,10 +74,12 @@ describe "choose" do
it "should result in the value on being posted if not specified" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="radio" name="first_option" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "first_option" => "on")
choose "first_option"
@ -76,10 +88,12 @@ describe "choose" do
it "should result in the value on being posted if not specified and checked by default" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="radio" name="first_option" checked="checked"/>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "first_option" => "on")
click_button
@ -87,6 +101,7 @@ describe "choose" do
it "should result in the value of the selected radio button being posted when a subsequent one is checked by default" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
@ -94,6 +109,7 @@ describe "choose" do
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"gender" => "M"})
choose "Male"

View File

@ -3,9 +3,11 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "click_area" do
it "should use get by default" do
with_html <<-HTML
<html>
<map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_area "Berlin"
@ -13,9 +15,11 @@ describe "click_area" do
it "should assert valid response" do
with_html <<-HTML
<html>
<map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map>
</html>
HTML
webrat_session.response_code = 501
lambda { click_area "Berlin" }.should raise_error(Webrat::PageLoadError)
@ -24,9 +28,11 @@ describe "click_area" do
[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
with_html <<-HTML
<html>
<map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map>
</html>
HTML
webrat_session.response_code = status
lambda { click_area "Berlin" }.should_not raise_error
@ -35,9 +41,11 @@ describe "click_area" do
it "should fail if the area doesn't exist" do
with_html <<-HTML
<html>
<map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map>
</html>
HTML
lambda {
@ -47,9 +55,11 @@ describe "click_area" do
it "should not be case sensitive" do
with_html <<-HTML
<html>
<map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_area "berlin"
@ -59,9 +69,11 @@ describe "click_area" do
it "should follow relative links" do
webrat_session.stub!(:current_url => "/page")
with_html <<-HTML
<html>
<map name="map_de" id="map_de">
<area href="sub" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map>
</html>
HTML
webrat_session.should_receive(:get).with("/page/sub", {})
click_area "Berlin"
@ -69,9 +81,11 @@ describe "click_area" do
it "should follow fully qualified local links" do
with_html <<-HTML
<html>
<map name="map_de" id="map_de">
<area href="http://www.example.com/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map>
</html>
HTML
webrat_session.should_receive(:get).with("http://www.example.com/page", {})
click_area "Berlin"
@ -79,9 +93,11 @@ describe "click_area" do
it "should follow query parameters" do
with_html <<-HTML
<html>
<map name="map_de" id="map_de">
<area href="/page?foo=bar" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map>
</html>
HTML
webrat_session.should_receive(:get).with("/page?foo=bar", {})
click_area "Berlin"

View File

@ -3,7 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "click_button" do
it "should fail if no buttons" do
with_html <<-HTML
<html>
<form method="get" action="/login"></form>
</html>
HTML
lambda { click_button }.should raise_error(Webrat::NotFoundError)
@ -11,9 +13,11 @@ describe "click_button" do
it "should fail if input is not a submit button" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input type="reset" />
</form>
</html>
HTML
lambda { click_button }.should raise_error(Webrat::NotFoundError)
@ -22,9 +26,11 @@ describe "click_button" do
it "should fail if button is disabled" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input type="submit" disabled="disabled" />
</form>
</html>
HTML
lambda { click_button }.should raise_error(Webrat::DisabledFieldError)
@ -32,9 +38,11 @@ describe "click_button" do
it "should default to get method" do
with_html <<-HTML
<html>
<form action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button
@ -42,9 +50,11 @@ describe "click_button" do
it "should assert valid response" do
with_html <<-HTML
<html>
<form action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.response_code = 501
lambda { click_button }.should raise_error(Webrat::PageLoadError)
@ -53,9 +63,11 @@ describe "click_button" do
[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
with_html <<-HTML
<html>
<form action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.response_code = status
lambda { click_button }.should_not raise_error
@ -64,12 +76,14 @@ describe "click_button" do
it "should submit the first form by default" do
with_html <<-HTML
<html>
<form method="get" action="/form1">
<input type="submit" />
</form>
<form method="get" action="/form2">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/form1", {})
click_button
@ -77,10 +91,12 @@ describe "click_button" do
it "should not explode on file fields" do
with_html <<-HTML
<html>
<form method="get" action="/form1">
<input type="file" />
<input type="submit" />
</form>
</html>
HTML
click_button
end
@ -102,9 +118,11 @@ describe "click_button" do
it "should use action from form" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", {})
click_button
@ -112,9 +130,11 @@ describe "click_button" do
it "should use method from form" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post)
click_button
@ -122,10 +142,12 @@ describe "click_button" do
it "should send button as param if it has a name" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="submit" name="cancel" value="Cancel" />
<input type="submit" name="login" value="Login" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "login" => "Login")
click_button("Login")
@ -133,10 +155,12 @@ describe "click_button" do
it "should not send button as param if it has no name" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="submit" name="cancel" value="Cancel" />
<input type="submit" value="Login" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", {})
click_button("Login")
@ -144,10 +168,12 @@ describe "click_button" do
it "should send default password field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_password" name="user[password]" value="mypass" type="password" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"password" => "mypass"})
click_button
@ -155,10 +181,12 @@ describe "click_button" do
it "should send default hidden field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="hidden" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
click_button
@ -166,10 +194,12 @@ describe "click_button" do
it "should send default text field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
click_button
@ -177,14 +207,16 @@ describe "click_button" do
it "should not send disabled field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input disabled id="user_email" name="user[email]" value="test@example.com" type="text" />
<input disabled id="user_gender_male" name="user[gender]" type="radio" value="M" />
<input disabled="disabled" id="user_email" name="user[email]" value="test@example.com" type="text" />
<input disabled="disabled" id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input disabled id="user_gender_female" name="user[gender]" type="radio" value="F" checked="checked" />
<input disabled="disabled" id="user_gender_female" name="user[gender]" type="radio" value="F" checked="checked" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", {})
click_button
@ -192,10 +224,12 @@ describe "click_button" do
it "should send default checked fields" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" value="1" type="checkbox" checked="checked" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
click_button
@ -203,6 +237,7 @@ describe "click_button" do
it "should send default radio options" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
@ -210,6 +245,7 @@ describe "click_button" do
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "F"})
click_button
@ -217,11 +253,13 @@ describe "click_button" do
it "should send correct data for rails style unchecked fields" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" />
<input name="user[tos]" type="hidden" value="0" /> TOS
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
click_button
@ -229,11 +267,13 @@ describe "click_button" do
it "should send correct data for rails style checked fields" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
<input name="user[tos]" type="hidden" value="0" /> TOS
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
click_button
@ -241,6 +281,7 @@ describe "click_button" do
it "should send default collection fields" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="checkbox" name="options[]" value="burger" checked="checked" />
<input type="radio" name="options[]" value="fries" checked="checked" />
@ -254,6 +295,7 @@ describe "click_button" do
<input type="hidden" name="response[choices][][selected]" value="two" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login",
"options" => ["burger", "fries", "soda", "soda", "dessert"],
@ -263,10 +305,12 @@ describe "click_button" do
it "should not send default unchecked fields" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" value="1" type="checkbox" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", {})
click_button
@ -274,17 +318,35 @@ describe "click_button" do
it "should send default textarea values" do
with_html <<-HTML
<html>
<form method="post" action="/posts">
<textarea name="post[body]">Post body here!</textarea>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Post body here!"})
click_button
end
it "should properly handle HTML entities in textarea default values" do
pending "needs bug fix" do
with_html <<-HTML
<html>
<form method="post" action="/posts">
<textarea name="post[body]">Peanut butter &amp; jelly</textarea>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Peanut butter & jelly"})
click_button
end
end
it "should send default selected option value from select" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<select name="month">
<option value="1">January</option>
@ -292,6 +354,7 @@ describe "click_button" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "month" => "2")
click_button
@ -299,6 +362,7 @@ describe "click_button" do
it "should send default selected option inner html from select when no value attribute" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<select name="month">
<option>January</option>
@ -306,6 +370,7 @@ describe "click_button" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "month" => "February")
click_button
@ -313,6 +378,7 @@ describe "click_button" do
it "should send first select option value when no option selected" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<select name="month">
<option value="1">January</option>
@ -320,6 +386,7 @@ describe "click_button" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "month" => "1")
click_button
@ -327,11 +394,13 @@ describe "click_button" do
it "should handle nested properties" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="text" id="contestant_scores_12" name="contestant[scores][1]" value="2"/>
<input type="text" id="contestant_scores_13" name="contestant[scores][3]" value="4"/>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}})
click_button
@ -339,10 +408,12 @@ describe "click_button" do
it "should send default empty text field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
click_button
@ -350,10 +421,12 @@ describe "click_button" do
it "should recognize button tags" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<button type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
click_button
@ -361,9 +434,11 @@ describe "click_button" do
it "should recognize image button tags" do
with_html <<-HTML
<html>
<form action="/">
<input type="image" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button
@ -371,9 +446,11 @@ describe "click_button" do
it "should find buttons by their IDs" do
with_html <<-HTML
<html>
<form action="/">
<input type="submit" id="my_button" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button "my_button"
@ -381,9 +458,11 @@ describe "click_button" do
it "should find image buttons by their alt text" do
with_html <<-HTML
<html>
<form action="/">
<input type="image" alt="Go" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button "Go"
@ -391,10 +470,12 @@ describe "click_button" do
it "should recognize button tags by content" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<button type="submit">Login</button>
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
click_button "Login"

View File

@ -3,7 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "click_link" do
it "should click links with ampertands" do
with_html <<-HTML
<html>
<a href="/page">Save &amp; go back</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Save & go back"
@ -11,7 +13,9 @@ describe "click_link" do
it "should use get by default" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text"
@ -19,7 +23,9 @@ describe "click_link" do
it "should click get links" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text", :method => :get
@ -27,7 +33,9 @@ describe "click_link" do
it "should click link on substring" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "ink tex", :method => :get
@ -35,7 +43,9 @@ describe "click_link" do
it "should click delete links" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:delete).with("/page", {})
click_link "Link text", :method => :delete
@ -44,7 +54,9 @@ describe "click_link" do
it "should click post links" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:post).with("/page", {})
click_link "Link text", :method => :post
@ -52,7 +64,9 @@ describe "click_link" do
it "should click put links" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:put).with("/page", {})
click_link "Link text", :method => :put
@ -60,7 +74,9 @@ describe "click_link" do
it "should click links by regexp" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link /link [a-z]/i
@ -68,7 +84,9 @@ describe "click_link" do
it "should click links by id" do
with_html <<-HTML
<html>
<a id="link_text_link" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "link_text_link"
@ -76,7 +94,9 @@ describe "click_link" do
it "should click links by id regexp" do
with_html <<-HTML
<html>
<a id="link_text_link" href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link /_text_/
@ -84,6 +104,7 @@ describe "click_link" do
it "should click rails javascript links with authenticity tokens" do
with_html <<-HTML
<html>
<a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
@ -96,6 +117,7 @@ describe "click_link" do
f.appendChild(s);
f.submit();
return false;">Posts</a>
</html>
HTML
webrat_session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
click_link "Posts"
@ -103,6 +125,7 @@ describe "click_link" do
it "should click rails javascript delete links" do
with_html <<-HTML
<html>
<a href="/posts/1" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
@ -115,6 +138,7 @@ describe "click_link" do
f.appendChild(m);
f.submit();
return false;">Delete</a>
</html>
HTML
webrat_session.should_receive(:delete).with("/posts/1", {})
click_link "Delete"
@ -122,6 +146,7 @@ describe "click_link" do
it "should click rails javascript post links" do
with_html <<-HTML
<html>
<a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
@ -129,6 +154,7 @@ describe "click_link" do
f.action = this.href;
f.submit();
return false;">Posts</a>
</html>
HTML
webrat_session.should_receive(:post).with("/posts", {})
click_link "Posts"
@ -136,6 +162,7 @@ describe "click_link" do
it "should click rails javascript post links without javascript" do
with_html <<-HTML
<html>
<a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
@ -143,6 +170,7 @@ describe "click_link" do
f.action = this.href;
f.submit();
return false;">Posts</a>
</html>
HTML
webrat_session.should_receive(:get).with("/posts", {})
click_link "Posts", :javascript => false
@ -150,6 +178,7 @@ describe "click_link" do
it "should click rails javascript put links" do
with_html <<-HTML
<html>
<a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
@ -162,6 +191,7 @@ describe "click_link" do
f.appendChild(m);
f.submit();
return false;">Put</a></h2>
</html>
HTML
webrat_session.should_receive(:put).with("/posts", {})
click_link "Put"
@ -169,6 +199,7 @@ describe "click_link" do
it "should fail if the javascript link doesn't have a value for the _method input" do
with_html <<-HTML
<html>
<a href="/posts/1" onclick="var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
@ -180,6 +211,7 @@ describe "click_link" do
f.appendChild(m);
f.submit();
return false;">Link</a>
</html>
HTML
lambda {
@ -189,7 +221,9 @@ describe "click_link" do
it "should assert valid response" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.response_code = 501
lambda { click_link "Link text" }.should raise_error(Webrat::PageLoadError)
@ -198,7 +232,9 @@ describe "click_link" do
[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.response_code = status
lambda { click_link "Link text" }.should_not raise_error
@ -207,7 +243,9 @@ describe "click_link" do
it "should fail is the link doesn't exist" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
lambda {
@ -217,7 +255,9 @@ describe "click_link" do
it "should not be case sensitive" do
with_html <<-HTML
<html>
<a href="/page">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "LINK TEXT"
@ -225,7 +265,9 @@ describe "click_link" do
it "should match link substrings" do
with_html <<-HTML
<html>
<a href="/page">This is some cool link text, isn't it?</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text"
@ -233,7 +275,9 @@ describe "click_link" do
it "should work with elements in the link" do
with_html <<-HTML
<html>
<a href="/page"><span>Link text</span></a>
</html>
HTML
webrat_session.should_receive(:get).with("/page", {})
click_link "Link text"
@ -241,8 +285,10 @@ describe "click_link" do
it "should match the first matching link" do
with_html <<-HTML
<html>
<a href="/page1">Link text</a>
<a href="/page2">Link text</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page1", {})
click_link "Link text"
@ -274,8 +320,10 @@ describe "click_link" do
it "should not match on non-text contents" do
pending "needs fix" do
with_html <<-HTML
<html>
<a href="/page1"><span class="location">My house</span></a>
<a href="/page2">Location</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page2", {})
@ -299,7 +347,9 @@ describe "click_link" do
it "should not make request when link is local anchor" do
with_html <<-HTML
<html>
<a href="#section-1">Jump to Section 1</a>
</html>
HTML
# Don't know why webrat_session.should_receive(:get).never doesn't work here
webrat_session.should_receive(:send).with('get_via_redirect', '#section-1', {}).never
@ -309,7 +359,9 @@ describe "click_link" do
it "should follow relative links" do
webrat_session.stub!(:current_url => "/page")
with_html <<-HTML
<html>
<a href="sub">Jump to sub page</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page/sub", {})
click_link "Jump to sub page"
@ -318,7 +370,9 @@ describe "click_link" do
it "should follow fully qualified local links" do
webrat_session.stub!(:current_url => "/page")
with_html <<-HTML
<html>
<a href="http://subdomain.example.com/page/sub">Jump to sub page</a>
</html>
HTML
webrat_session.should_receive(:get).with("http://subdomain.example.com/page/sub", {})
click_link "Jump to sub page"
@ -326,7 +380,9 @@ describe "click_link" do
it "should follow fully qualified local links to example.com" do
with_html <<-HTML
<html>
<a href="http://www.example.com/page/sub">Jump to sub page</a>
</html>
HTML
webrat_session.should_receive(:get).with("http://www.example.com/page/sub", {})
click_link "Jump to sub page"
@ -335,9 +391,78 @@ describe "click_link" do
it "should follow query parameters" do
webrat_session.stub!(:current_url => "/page")
with_html <<-HTML
<html>
<a href="?foo=bar">Jump to foo bar</a>
</html>
HTML
webrat_session.should_receive(:get).with("/page?foo=bar", {})
click_link "Jump to foo bar"
end
it "should matches_text? on regexp" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?(/link/i).should == 0
end
it "should matches_text? on link_text" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("Link Text").should == 0
end
it "should matches_text? on substring" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.should_receive(:inner_html).and_return('Link&nbsp;Text')
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end
it "should match text not include &nbsp;" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('LinkText')
link.matches_text?("LinkText").should == 0
end
it "should not matches_text? on wrong text" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
nbsp = [0xA0].pack("U")
link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link")
link.should_receive(:inner_html).and_return("Some&nbsp;Other&nbsp;Link")
link.should_receive(:title).and_return(nil)
link.matches_text?("Link Text").should == false
end
it "should match text including character reference" do
pending "need to update these"
no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA]
nokogiri_ja_kana = no_ko_gi_ri.pack("U*")
nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("")
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(nokogiri_ja_kana)
link.matches_text?(nokogiri_ja_kana).should == 0
end
it "should match img link" do
pending "need to update these"
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('')
link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
end

View File

@ -3,11 +3,13 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "fill_in" do
it "should work with textareas" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_text">User Text</label>
<textarea id="user_text" name="user[text]"></textarea>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"text" => "filling text area"})
fill_in "User Text", :with => "filling text area"
@ -16,10 +18,12 @@ describe "fill_in" do
it "should work with password fields" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_text" name="user[text]" type="password" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"text" => "pass"})
fill_in "user_text", :with => "pass"
@ -28,8 +32,10 @@ describe "fill_in" do
it "should fail if input not found" do
with_html <<-HTML
<html>
<form method="get" action="/login">
</form>
</html>
HTML
lambda { fill_in "Email", :with => "foo@example.com" }.should raise_error(Webrat::NotFoundError)
@ -37,11 +43,13 @@ describe "fill_in" do
it "should fail if input is disabled" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="text" disabled="disabled" />
<input type="submit" />
</form>
</html>
HTML
lambda { fill_in "Email", :with => "foo@example.com" }.should raise_error(Webrat::DisabledFieldError)
@ -49,11 +57,13 @@ describe "fill_in" do
it "should allow overriding default form values" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in "user[email]", :with => "foo@example.com"
@ -62,6 +72,7 @@ describe "fill_in" do
it "should choose the shortest label match" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_mail1">Some other mail</label>
<input id="user_mail1" name="user[mail1]" type="text" />
@ -69,6 +80,7 @@ describe "fill_in" do
<input id="user_mail2" name="user[mail2]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "", "mail2" => "value"})
@ -78,6 +90,7 @@ describe "fill_in" do
it "should choose the first label match if closest is a tie" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_mail1">Some mail one</label>
<input id="user_mail1" name="user[mail1]" type="text" />
@ -85,6 +98,7 @@ describe "fill_in" do
<input id="user_mail2" name="user[mail2]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "value", "mail2" => ""})
@ -94,10 +108,12 @@ describe "fill_in" do
it "should anchor label matches to start of label" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_email">Some mail</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
</form>
</html>
HTML
lambda { fill_in "mail", :with => "value" }.should raise_error(Webrat::NotFoundError)
@ -105,10 +121,12 @@ describe "fill_in" do
it "should anchor label matches to word boundaries" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_email">Emailtastic</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
</form>
</html>
HTML
lambda { fill_in "Email", :with => "value" }.should raise_error(Webrat::NotFoundError)
@ -116,6 +134,7 @@ describe "fill_in" do
it "should work with inputs nested in labels" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label>
Email
@ -123,6 +142,7 @@ describe "fill_in" do
</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in "Email", :with => "foo@example.com"
@ -131,10 +151,12 @@ describe "fill_in" do
it "should work with full input names" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_email" name="user[email]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in "user[email]", :with => "foo@example.com"
@ -143,10 +165,12 @@ describe "fill_in" do
it "should work if the input type is not set" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_email" name="user[email]" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in "user[email]", :with => "foo@example.com"
@ -155,11 +179,13 @@ describe "fill_in" do
it "should work with symbols" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
fill_in :email, :with => "foo@example.com"
@ -168,11 +194,13 @@ describe "fill_in" do
it "should escape field values" do
with_html <<-HTML
<html>
<form method="post" action="/users">
<label for="user_phone">Phone</label>
<input id="user_phone" name="user[phone]" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/users", "user" => {"phone" => "+1 22 33"})
fill_in 'Phone', :with => "+1 22 33"

View File

@ -12,6 +12,8 @@ describe "field_by_xpath" do
</html>
HTML
field_by_xpath(".//input").id.should == "element_42"
field = field_by_xpath(".//input")
field.should_not be_nil
field.id.should == "element_42"
end
end

View File

@ -37,16 +37,22 @@ describe "field_labeled" do
def match_id(id)
simple_matcher "element with id #{id.inspect}" do |element, matcher|
element.matches_id? id
if id.is_a?(Regexp)
element.id =~ id
else
element.id == id.to_s
end
end
end
describe "finding a text field" do
using_this_html <<-HTML
<html>
<form>
<label for="element_42">The Label</label>
<input type="text" id="element_42">
</form>
</html>
HTML
should_return_a Webrat::TextField, :for => "The Label"
@ -56,10 +62,12 @@ describe "field_labeled" do
describe "finding a hidden field" do
using_this_html <<-HTML
<html>
<form>
<label for="element_42">The Label</label>
<input type="hidden" id="element_42">
</form>
</html>
HTML
should_return_a Webrat::HiddenField, :for => "The Label"
@ -69,10 +77,12 @@ describe "field_labeled" do
describe "finding a checkbox" do
using_this_html <<-HTML
<html>
<form>
<label for="element_42">The Label</label>
<input type="checkbox" id="element_42">
</form>
</html>
HTML
should_return_a Webrat::CheckboxField, :for => "The Label"
@ -82,10 +92,12 @@ describe "field_labeled" do
describe "finding a radio button" do
using_this_html <<-HTML
<html>
<form>
<label for="element_42">The Label</label>
<input type="radio" id="element_42">
</form>
</html>
HTML
should_return_a Webrat::RadioField, :for => "The Label"
@ -96,10 +108,12 @@ describe "field_labeled" do
describe "finding a text area" do
using_this_html <<-HTML
<html>
<form>
<label for="element_42">The Label</label>
<textarea id="element_42"></textarea>
</form>
</html>
HTML
should_return_a Webrat::TextareaField, :for => "The Label"
@ -109,6 +123,7 @@ describe "field_labeled" do
describe "finding a field with it's label containing newlines" do
using_this_html <<-HTML
<html>
<form>
<label for="element_42">
A label with
@ -116,6 +131,7 @@ describe "field_labeled" do
</label>
<input type="text" id="element_42">
</form>
</html>
HTML
should_return_a Webrat::TextField, :for => "A label with a link on it's own line"

View File

@ -0,0 +1,16 @@
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe "field_with_id" do
it "should work when there is a single quote in the ID" do
with_html <<-HTML
<html>
<form>
<input type="text" id="user's name">
</form>
</html>
HTML
field_with_id("user's name").id.should == "user's name"
end
end

View File

@ -5,7 +5,7 @@ describe Webrat::Matchers do
include Webrat::HaveTagMatcher
before(:each) do
@body = <<-EOF
@body = <<-HTML
<div id='main'>
<div class='inner'>hello, world!</div>
<ul>
@ -13,7 +13,7 @@ describe Webrat::Matchers do
<li>Second</li>
</ul>
</div>
EOF
HTML
end
describe "#have_xpath" do

View File

@ -3,6 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "select_date" do
it "should send the values for each individual date component" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<label for="appointment_date">Date</label><br />
<select id="appointment_date_1i" name="appointment[date(1i)]">
@ -16,6 +17,7 @@ describe "select_date" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"})
@ -25,6 +27,7 @@ describe "select_date" do
it "should accept a date object" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<label for="appointment_date">date</label><br />
<select id="appointment_date_1i" name="appointment[date(1i)]">
@ -38,6 +41,7 @@ describe "select_date" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"})
@ -47,6 +51,7 @@ describe "select_date" do
it "should work when no label is specified" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<select id="appointment_date_1i" name="appointment[date(1i)]">
<option value="2003">2003</option>
@ -59,6 +64,7 @@ describe "select_date" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"})
@ -68,10 +74,12 @@ describe "select_date" do
it "should fail if the specified label is not found" do
with_html <<-HTML
<html>
<form method="post" action="/appointments">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
</html>
HTML
lambda { select_date "December 25, 2003", :from => "date" }.should raise_error(Webrat::NotFoundError)

View File

@ -3,6 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "select_datetime" do
it "should send the values for each individual date and time components" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<label for="appointment_time">Time</label><br />
<select id="appointment_time_1i" name="appointment[time(1i)]">
@ -22,6 +23,7 @@ describe "select_datetime" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"})
@ -31,6 +33,7 @@ describe "select_datetime" do
it "should accept a time object" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<label for="appointment_time">Time</label><br />
<select id="appointment_time_1i" name="appointment[time(1i)]">
@ -50,6 +53,7 @@ describe "select_datetime" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"})
@ -59,6 +63,7 @@ describe "select_datetime" do
it "should work when no label is specified" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<select id="appointment_time_1i" name="appointment[time(1i)]">
<option value="2003">2003</option>
@ -77,6 +82,7 @@ describe "select_datetime" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"})
@ -86,10 +92,12 @@ describe "select_datetime" do
it "should fail if the specified label is not found" do
with_html <<-HTML
<html>
<form method="post" action="/appointments">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
</html>
HTML
lambda { select_datetime "December 25, 2003 9:30", :from => "Time" }.should raise_error(Webrat::NotFoundError)

View File

@ -3,21 +3,25 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "select" do
it "should fail with a helpful message when option not found" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
</form>
</html>
HTML
lambda { select "February", :from => "month" }.should raise_error(Webrat::NotFoundError,
"The 'February' option was not found in the 'month' select box")
"The 'February' option was not found in the \"month\" select box")
end
it "should fail if option not found in list specified by element name" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
<select name="year"><option value="2008">2008</option></select>
</form>
</html>
HTML
lambda { select "February", :from => "year" }.should raise_error(Webrat::NotFoundError)
@ -25,9 +29,11 @@ describe "select" do
it "should fail if specified list not found" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
</form>
</html>
HTML
lambda { select "February", :from => "year" }.should raise_error(Webrat::NotFoundError)
@ -36,10 +42,12 @@ describe "select" do
it "should fail if the select is disabled" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month" disabled="disabled"><option value="1">January</option></select>
<input type="submit" />
</form>
</html>
HTML
lambda { select "January", :from => "month" }.should raise_error(Webrat::DisabledFieldError)
@ -47,10 +55,12 @@ describe "select" do
it "should send value from option" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month"><option value="1">January</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "month" => "1")
select "January", :from => "month"
@ -59,10 +69,12 @@ describe "select" do
it "should send values with HTML encoded ampersands" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="encoded"><option value="A &amp; B">Encoded</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "encoded" => "A & B")
select "Encoded", :from => "encoded"
@ -71,10 +83,12 @@ describe "select" do
it "should work with empty select lists" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month"></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", 'month' => '')
click_button
@ -82,10 +96,12 @@ describe "select" do
it "should work without specifying the field name or label" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month"><option value="1">January</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "month" => "1")
select "January"
@ -94,11 +110,13 @@ describe "select" do
it "should send value from option in list specified by name" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="start_month"><option value="s1">January</option></select>
<select name="end_month"><option value="e1">January</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
select "January", :from => "end_month"
@ -107,6 +125,7 @@ describe "select" do
it "should send value from option in list specified by label" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="start_month">Start Month</label>
<select id="start_month" name="start_month"><option value="s1">January</option></select>
@ -114,6 +133,7 @@ describe "select" do
<select id="end_month" name="end_month"><option value="e1">January</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
select "January", :from => "End Month"
@ -122,10 +142,12 @@ describe "select" do
it "should use option text if no value" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "month" => "January")
select "January", :from => "month"
@ -134,10 +156,12 @@ describe "select" do
it "should find option by regexp" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "month" => "January")
select /jan/i
@ -146,10 +170,12 @@ describe "select" do
it "should fail if no option matching the regexp exists" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
</html>
HTML
lambda {
@ -159,6 +185,7 @@ describe "select" do
it "should find option by regexp in list specified by label" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<label for="start_month">Start Month</label>
<select id="start_month" name="start_month"><option value="s1">January</option></select>
@ -166,9 +193,42 @@ describe "select" do
<select id="end_month" name="end_month"><option value="e1">January</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
select /jan/i, :from => "End Month"
click_button
end
it "should properly handle submitting HTML entities in select values" do
pending "needs bug fix" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month"><option>Peanut butter &amp; jelly</option></select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "month" => "Peanut butter & jelly")
click_button
end
end
it "should properly handle locating with HTML entities in select values" do
pending "needs bug fix" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<select name="month"><option>Peanut butter &amp; jelly</option></select>
<input type="submit" />
</form>
</html>
HTML
lambda {
select "Peanut butter & jelly"
}.should_not raise_error(Webrat::NotFoundError)
end
end
end

View File

@ -3,6 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "select_time" do
it "should send the values for each individual time component" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<label for="appointment_time">Time</label><br />
<select id="appointment_time_4i" name="appointment[time(4i)]">
@ -13,6 +14,7 @@ describe "select_time" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(4i)" => "09", "time(5i)" => "30"})
@ -22,6 +24,7 @@ describe "select_time" do
it "should accept a time object" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<label for="appointment_time">Time</label><br />
<select id="appointment_time_4i" name="appointment[time(4i)]">
@ -32,6 +35,7 @@ describe "select_time" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(4i)" => "09", "time(5i)" => "30"})
@ -41,6 +45,7 @@ describe "select_time" do
it "should work when no label is specified" do
with_html <<-HTML
<html>
<form action="/appointments" method="post">
<select id="appointment_time_4i" name="appointment[time(4i)]">
<option value="09">09</option>
@ -50,6 +55,7 @@ describe "select_time" do
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(4i)" => "09", "time(5i)" => "30"})
@ -59,10 +65,12 @@ describe "select_time" do
it "should fail if the specified label is not found" do
with_html <<-HTML
<html>
<form method="post" action="/appointments">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
</html>
HTML
lambda { select_time "9:30", :from => "Time" }.should raise_error(Webrat::NotFoundError)

View File

@ -0,0 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "set_hidden_field" do
it "needs specs"
it "needs docs"
end

View File

@ -0,0 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "submit_form" do
it "needs specs"
it "needs docs"
end

View File

@ -3,7 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "visit" do
before do
with_html <<-HTML
<html>
Hello world
</html>
HTML
end
@ -33,7 +35,9 @@ describe "visit with referer" do
before do
webrat_session.instance_variable_set(:@current_url, "/old_url")
with_html <<-HTML
<html>
Hello world
</html>
HTML
end

View File

@ -7,18 +7,25 @@ begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
webrat_path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
$LOAD_PATH.unshift(webrat_path) unless $LOAD_PATH.include?(webrat_path)
require "merb-core"
require "webrat/merb"
require "webrat"
require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session")
require "merb-core"
Spec::Runner.configure do |config|
include Webrat::Methods
def with_html(html)
raise "This doesn't look like HTML. Wrap it in a <html> tag" unless html =~ /^\s*<[^Hh>]*html/i
webrat_session.response_body = html
end
def with_xml(xml)
raise "This looks like HTML" if xml =~ /^\s*<[^Hh>]*html/i
webrat_session.response_body = xml
end
end
module Webrat

View File

@ -3,24 +3,50 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
module Webrat
describe CheckboxField do
it "should say it is checked if it is" do
checkbox = CheckboxField.new(nil, (Webrat::XML.document("<input type='checkbox' checked='checked'>").search('input')).first)
html = <<-HTML
<html>
<input type='checkbox' checked='checked' />
</html>
HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first
checkbox = CheckboxField.new(nil, element)
checkbox.should be_checked
end
it "should say it is not checked if it is not" do
checkbox = CheckboxField.new(nil, (Webrat::XML.document("<input type='checkbox'>").search('input')).first)
html = <<-HTML
<html>
<input type='checkbox' />
</html>
HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first
checkbox = CheckboxField.new(nil, element)
checkbox.should_not be_checked
end
end
describe RadioField do
it "should say it is checked if it is" do
radio_button = RadioField.new(nil, (Webrat::XML.document("<input type='radio' checked='checked'>").search('input')).first)
html = <<-HTML
<html>
<input type='radio' checked='checked' />
</html>
HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first
radio_button = RadioField.new(nil, element)
radio_button.should be_checked
end
it "should say it is not checked if it is not" do
radio_button = RadioField.new(nil, (Webrat::XML.document("<input type='radio'>").search('input')).first)
html = <<-HTML
<html><input type='radio' /></html>
HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first
radio_button = RadioField.new(nil, element)
radio_button.should_not be_checked
end
end

View File

@ -21,80 +21,4 @@ describe Webrat::Link do
link.click
end
it "should matches_text? on regexp" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?(/link/i).should == 0
end
it "should matches_text? on link_text" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("Link Text").should == 0
end
it "should matches_text? on substring" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.should_receive(:inner_html).and_return('Link&nbsp;Text')
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end
it "should match text not include &nbsp;" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('LinkText')
link.matches_text?("LinkText").should == 0
end
it "should not matches_text? on wrong text" do
link = Webrat::Link.new(webrat_session, nil)
nbsp = [0xA0].pack("U")
link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link")
link.should_receive(:inner_html).and_return("Some&nbsp;Other&nbsp;Link")
link.should_receive(:title).and_return(nil)
link.matches_text?("Link Text").should == false
end
it "should match text including character reference" do
no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA]
nokogiri_ja_kana = no_ko_gi_ri.pack("U*")
nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("")
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(nokogiri_ja_kana)
link.matches_text?(nokogiri_ja_kana).should == 0
end
it "should match img link" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('')
link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
it "should matches_id? on exact matching id" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("some_id")
link.matches_id?("some_id").should == true
end
it "should not matches_id? on incorrect id" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("other_id")
link.matches_id?("some_id").should == false
end
it "should matches_id? on matching id by regexp" do
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("some_id")
link.matches_id?(/some/).should == true
end
end

View File

@ -18,7 +18,7 @@ describe Webrat::Session do
"<html></html>"
end
session.current_dom.should respond_to(:search)
session.should respond_to(:current_dom)
end
it "should open the page in the browser in MacOSX" do

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
if defined?(Nokogiri::XML)
if defined?(Nokogiri::XML) && Webrat.configuration.parse_with_nokogiri?
describe "Nokogiri Extension" do
include Webrat::Matchers

View File

@ -9,18 +9,22 @@ describe "attach_file" do
it "should fail if no file field found" do
with_html <<-HTML
<html>
<form method="post" action="/widgets">
</form>
</html>
HTML
lambda { attach_file("Doc", "/some/path") }.should raise_error(Webrat::NotFoundError)
end
it "should submit empty strings for blank file fields" do
with_html <<-HTML
<html>
<form method="post" action="/widgets">
<input type="file" id="widget_file" name="widget[file]" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } })
click_button
@ -28,11 +32,13 @@ describe "attach_file" do
it "should submit the attached file" do
with_html <<-HTML
<html>
<form method="post" action="/widgets">
<label for="widget_file">Document</label>
<input type="file" id="widget_file" name="widget[file]" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } })
attach_file "Document", @filename
@ -41,6 +47,7 @@ describe "attach_file" do
it "should support collections" do
with_html <<-HTML
<html>
<form method="post" action="/widgets">
<label for="widget_file1">Document</label>
<input type="file" id="widget_file1" name="widget[files][]" />
@ -48,6 +55,7 @@ describe "attach_file" do
<input type="file" id="widget_file2" name="widget[files][]" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
attach_file "Document", @filename
@ -57,11 +65,13 @@ describe "attach_file" do
it "should allow the content type to be specified" do
with_html <<-HTML
<html>
<form method="post" action="/widgets">
<label for="person_picture">Picture</label>
<input type="file" id="person_picture" name="person[picture]" />
<input type="submit" />
</form>
</html>
HTML
ActionController::TestUploadedFile.should_receive(:new).with(@filename, "image/png").any_number_of_times
attach_file "Picture", @filename, "image/png"

View File

@ -4,11 +4,11 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Bryan Helmkamp"]
s.date = %q{2008-11-24}
s.date = %q{2008-11-30}
s.description = %q{Webrat. Ruby Acceptance Testing for Web applications}
s.email = %q{bryan@brynary.com}
s.extra_rdoc_files = ["README.rdoc", "MIT-LICENSE.txt"]
s.files = ["History.txt", "init.rb", "install.rb", "MIT-LICENSE.txt", "README.rdoc", "Rakefile", "lib/webrat", "lib/webrat/core", "lib/webrat/core/area.rb", "lib/webrat/core/configuration.rb", "lib/webrat/core/field.rb", "lib/webrat/core/form.rb", "lib/webrat/core/hpricot.rb", "lib/webrat/core/label.rb", "lib/webrat/core/link.rb", "lib/webrat/core/locators.rb", "lib/webrat/core/logging.rb", "lib/webrat/core/matchers", "lib/webrat/core/matchers/have_content.rb", "lib/webrat/core/matchers/have_selector.rb", "lib/webrat/core/matchers/have_tag.rb", "lib/webrat/core/matchers/have_xpath.rb", "lib/webrat/core/matchers.rb", "lib/webrat/core/methods.rb", "lib/webrat/core/mime.rb", "lib/webrat/core/nokogiri.rb", "lib/webrat/core/rexml.rb", "lib/webrat/core/scope.rb", "lib/webrat/core/select_option.rb", "lib/webrat/core/session.rb", "lib/webrat/core/xml.rb", "lib/webrat/core.rb", "lib/webrat/core_extensions", "lib/webrat/core_extensions/blank.rb", "lib/webrat/core_extensions/deprecate.rb", "lib/webrat/core_extensions/detect_mapped.rb", "lib/webrat/core_extensions/hash_with_indifferent_access.rb", "lib/webrat/core_extensions/meta_class.rb", "lib/webrat/core_extensions/nil_to_param.rb", "lib/webrat/mechanize.rb", "lib/webrat/merb.rb", "lib/webrat/rack.rb", "lib/webrat/rails", "lib/webrat/rails/redirect_actions.rb", "lib/webrat/rails.rb", "lib/webrat/rspec-rails.rb", "lib/webrat/selenium", "lib/webrat/selenium/location_strategy_javascript", "lib/webrat/selenium/location_strategy_javascript/button.js", "lib/webrat/selenium/location_strategy_javascript/label.js", "lib/webrat/selenium/location_strategy_javascript/webrat.js", "lib/webrat/selenium/location_strategy_javascript/webratlink.js", "lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js", "lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js", "lib/webrat/selenium/selenium_extensions.js", "lib/webrat/selenium/selenium_session.rb", "lib/webrat/selenium.rb", "lib/webrat/sinatra.rb", "lib/webrat.rb"]
s.files = ["History.txt", "install.rb", "MIT-LICENSE.txt", "README.rdoc", "Rakefile", "lib/webrat", "lib/webrat/core", "lib/webrat/core/configuration.rb", "lib/webrat/core/elements", "lib/webrat/core/elements/area.rb", "lib/webrat/core/elements/element.rb", "lib/webrat/core/elements/field.rb", "lib/webrat/core/elements/form.rb", "lib/webrat/core/elements/label.rb", "lib/webrat/core/elements/link.rb", "lib/webrat/core/elements/select_option.rb", "lib/webrat/core/locators", "lib/webrat/core/locators/area_locator.rb", "lib/webrat/core/locators/button_locator.rb", "lib/webrat/core/locators/field_by_id_locator.rb", "lib/webrat/core/locators/field_labeled_locator.rb", "lib/webrat/core/locators/field_locator.rb", "lib/webrat/core/locators/field_named_locator.rb", "lib/webrat/core/locators/form_locator.rb", "lib/webrat/core/locators/label_locator.rb", "lib/webrat/core/locators/link_locator.rb", "lib/webrat/core/locators/locator.rb", "lib/webrat/core/locators/select_option_locator.rb", "lib/webrat/core/locators.rb", "lib/webrat/core/logging.rb", "lib/webrat/core/matchers", "lib/webrat/core/matchers/have_content.rb", "lib/webrat/core/matchers/have_selector.rb", "lib/webrat/core/matchers/have_tag.rb", "lib/webrat/core/matchers/have_xpath.rb", "lib/webrat/core/matchers.rb", "lib/webrat/core/methods.rb", "lib/webrat/core/mime.rb", "lib/webrat/core/scope.rb", "lib/webrat/core/session.rb", "lib/webrat/core/xml", "lib/webrat/core/xml/hpricot.rb", "lib/webrat/core/xml/nokogiri.rb", "lib/webrat/core/xml/rexml.rb", "lib/webrat/core/xml.rb", "lib/webrat/core.rb", "lib/webrat/core_extensions", "lib/webrat/core_extensions/blank.rb", "lib/webrat/core_extensions/deprecate.rb", "lib/webrat/core_extensions/detect_mapped.rb", "lib/webrat/core_extensions/hash_with_indifferent_access.rb", "lib/webrat/core_extensions/meta_class.rb", "lib/webrat/core_extensions/nil_to_param.rb", "lib/webrat/mechanize.rb", "lib/webrat/merb.rb", "lib/webrat/rack.rb", "lib/webrat/rails", "lib/webrat/rails/redirect_actions.rb", "lib/webrat/rails.rb", "lib/webrat/rspec-rails.rb", "lib/webrat/selenium", "lib/webrat/selenium/location_strategy_javascript", "lib/webrat/selenium/location_strategy_javascript/button.js", "lib/webrat/selenium/location_strategy_javascript/label.js", "lib/webrat/selenium/location_strategy_javascript/webrat.js", "lib/webrat/selenium/location_strategy_javascript/webratlink.js", "lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js", "lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js", "lib/webrat/selenium/selenium_extensions.js", "lib/webrat/selenium/selenium_session.rb", "lib/webrat/selenium.rb", "lib/webrat/sinatra.rb", "lib/webrat.rb", "vendor/selenium-server.jar"]
s.has_rdoc = true
s.homepage = %q{http://github.com/brynary/webrat}
s.require_paths = ["lib"]
@ -21,11 +21,11 @@ Gem::Specification.new do |s|
s.specification_version = 2
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<nokogiri>, [">= 1.0.3"])
s.add_runtime_dependency(%q<nokogiri>, [">= 1.0.6"])
else
s.add_dependency(%q<nokogiri>, [">= 1.0.3"])
s.add_dependency(%q<nokogiri>, [">= 1.0.6"])
end
else
s.add_dependency(%q<nokogiri>, [">= 1.0.3"])
s.add_dependency(%q<nokogiri>, [">= 1.0.6"])
end
end