Merge branch 'master' of git://github.com/brynary/webrat into brynary/master

Conflicts:
	Manifest.txt
	lib/webrat/mechanize/mechanize_session.rb
This commit is contained in:
Jeremy Burks 2008-11-11 19:47:49 -06:00
commit 92fde1d8de
70 changed files with 1563 additions and 857 deletions

View File

@ -1,23 +1,49 @@
== Trunk
* Major enhancements
* Added Merb support (Gwyn Morfey, Jeremy Burks, Rob Kaufman)
* Added #basic_auth(user, pass) to support HTTP Basic Auth (Aslak Hellesøy)
* Added support for Sinatra and Rack (Aslak Hellesøy)
* Added #within for manipulating the current page within a selector scope
* Add support for simulating SSL requests (Luke Melia)
* Add support for file fields via #attaches_file method (Kyle Hargraves)
* Minor enhancements
* Raise Webrat::PageLoadError when a failure occurs so that application exceptions can be more accurately tested (Ryan Briones)
== 0.3.2 / 2008-11-08
* Minor enhancements
* 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
* Use @_webrat_session instance variable instead of @session for Merb integration to avoid collisions
== 0.3.0 / 2008-11-07
* 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
* 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 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)
* Rename visits to visit, fills_in to fill_in, etc.
* Add #field_labeled for looking up form fields by label (David Chelimsky)
* Add #field_named and #field_with_id locators
* Don't depend on hoe anymore
* Return responses after sending requests
* Allow clicking links and buttons by a regular expression in Selenium (Luke Melia)
* 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)
* Add #field_labeled for looking up form fields by label (David Chelimsky)
* Add Webrat.root method for cross-framework support (Krzysztof Zylawy)
* Add support for clicking areas of an image map (Alex Lang)
* Add should_see and should_not_see for verifying HTML response bodys
* 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
* Alias visits as visit, clicks_link as click_link, etc. for better readability

View File

@ -26,7 +26,6 @@ lib/webrat/core/select_option.rb
lib/webrat/core/session.rb
lib/webrat/core/url_encoded_pair_parser.rb
lib/webrat/mechanize.rb
lib/webrat/mechanize/mechanize_session.rb
lib/webrat/merb.rb
lib/webrat/rack/rack_session.rb
lib/webrat/rails.rb

View File

@ -78,13 +78,6 @@ In your stories/helper.rb:
You could also unpack the gem into vendor/plugins.
=== Requirements
- Rails >= 1.2.6
- Hpricot >= 0.6
- Rails integration tests in Test::Unit _or_
- Cucumber
=== Authors
- Maintained by [Bryan Helmkamp](mailto:bryan@brynary.com)

View File

@ -1,43 +1,52 @@
require 'rubygems'
require 'hoe'
require "rake/gempackagetask"
require 'rake/rdoctask'
require "rake/clean"
require 'spec'
require 'spec/rake/spectask'
require 'spec/rake/verify_rcov'
require './lib/webrat.rb'
Hoe.new('webrat', Webrat::VERSION) do |p|
p.rubyforge_name = 'webrat'
p.summary = 'Ruby Acceptance Testing for Web applications'
##############################################################################
# Package && release
##############################################################################
spec = Gem::Specification.new do |s|
s.name = "webrat"
s.version = Webrat::VERSION
s.platform = Gem::Platform::RUBY
s.author = "Bryan Helmkamp"
s.email = "bryan" + "@" + "brynary.com"
s.homepage = "http://github.com/brynary/webrat"
s.summary = "Webrat. Ruby Acceptance Testing for Web applications"
s.bindir = "bin"
s.description = s.summary
s.require_path = "lib"
s.files = %w(History.txt init.rb install.rb MIT-LICENSE.txt README.txt Rakefile TODO.txt) + Dir["lib/**/*"]
p.developer "Bryan Helmkamp", "bryan@brynary.com"
p.developer "Seth Fitzsimmons", "seth@mojodna.net"
# rdoc
s.has_rdoc = true
s.extra_rdoc_files = %w(README.txt MIT-LICENSE.txt)
p.description = p.paragraphs_of('README.txt', 4..6).join("\n\n")
p.url = p.paragraphs_of('README.txt', 1).first.split("\n").first.strip
p.changes = p.paragraphs_of('History.txt', 0..3).join("\n\n")
p.extra_deps << ["hpricot", ">= 0.6"]
p.remote_rdoc_dir = '' # Release to root
# Dependencies
s.add_dependency "nokogiri", ">= 1.0.3"
end
Rake::GemPackageTask.new(spec) do |package|
package.gem_spec = spec
end
desc 'Show information about the gem.'
task :debug_gem do
puts spec.to_ruby
end
CLEAN.include ["pkg", "*.gem", "doc", "ri", "coverage"]
desc "Upload rdoc to brynary.com"
task :publish_rdoc => :docs do
sh "scp -r doc/ brynary.com:/apps/uploads/webrat"
end
Rake::TaskManager.class_eval do
def remove_task(task_name)
@tasks.delete(task_name.to_s)
end
end
def remove_task(task_name)
Rake.application.remove_task(task_name)
end
remove_task "test"
remove_task "test_deps"
desc "Run API and Core specs"
Spec::Rake::SpecTask.new do |t|
t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
@ -54,12 +63,27 @@ Spec::Rake::SpecTask.new(:rcov) do |t|
end
end
require 'spec/rake/verify_rcov'
RCov::VerifyTask.new(:verify_rcov => :rcov) do |t|
t.threshold = 96.2 # Make sure you have rcov 0.7 or higher!
end
remove_task "default"
task :default do
Rake::Task["verify_rcov"].invoke
end
desc 'Install the package as a gem.'
task :install_gem => [:clean, :package] do
gem = Dir['pkg/*.gem'].first
sh "sudo gem install --local #{gem}"
end
Rake::RDocTask.new(:docs) do |rd|
rd.main = "README.txt"
rd.rdoc_dir = 'doc'
files = spec.files.grep(/^(lib|bin|ext)|txt$/)
files -= ["TODO.txt"]
files -= files.grep(/\.js$/)
rd.rdoc_files = files.uniq
title = "webrat-#{Webrat::VERSION} Documentation"
rd.options << "-t #{title}"
end

View File

@ -1,7 +1,10 @@
Run tests in separate processes to eliminate constant-level dependencies
Add rake tasks for selenium runs
Add tests for selenium
Add tests for locator strategies
Use Webrat::Methods for Rails integration
Get file uploads workign with merb
Fix #within scoping for forms that exist outside the scope
Figure out what the deal is with #select not working
Restore SSL support for Rails (See 73d3b72108254c0f1ad00e63f8e712115cc8ca7c)
Full support for multiple forms on a page
Track the current form based on the location of the last manipulated input, use this as a default for clicks_button
Make current_url work with redirections
Support for a hash mapping page names to page URLs
Track the current form based on the location of the last manipulated input, use this as a default for click_button
Verify SSL support in Rails and Merb

View File

@ -3,16 +3,32 @@ require "rubygems"
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
module Webrat
VERSION = '0.2.2'
VERSION = '0.3.2'
def self.root
def self.root #:nodoc:
defined?(RAILS_ROOT) ? RAILS_ROOT : Merb.root
end
class WebratError < StandardError
end
end
# We need Nokogiri's CSS to XPath support, even if using REXML
require "nokogiri/css"
# Require nokogiri and fall back on rexml
begin
require "nokogiri"
require "webrat/core/nokogiri"
rescue LoadError => e
require "rexml/document"
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
end
# require "webrat/merb/param_parser"
# require "webrat/merb/url_encoded_pair_parser"
require "webrat/core"
require "webrat/rails" if defined?(RAILS_ENV)
require "webrat/merb" if defined?(Merb)
# TODO: This is probably not a good idea.
# Probably better for webrat users to require "webrat/rails" etc. directly
if defined?(RAILS_ENV)
require "webrat/rails"
end

View File

@ -1,3 +1,4 @@
require "webrat/core/nokogiri"
require "webrat/core/logging"
require "webrat/core/flunk"
require "webrat/core/param_parser"
@ -8,3 +9,5 @@ require "webrat/core/area"
require "webrat/core/label"
require "webrat/core/select_option"
require "webrat/core/session"
require "webrat/core/methods"
require "webrat/core/matchers"

View File

@ -1,5 +1,5 @@
module Webrat
class Area
class Area #:nodoc:
def initialize(session, element)
@session = session

View File

@ -1,27 +0,0 @@
module Webrat
module Assertions
def should_see(text_or_regexp)
case text_or_regexp
when Regexp
return if scoped_html.match(text_or_regexp)
else
return if scoped_html.include?(text_or_regexp)
end
flunk("Should see #{text_or_regexp.inspect} but didn't")
end
def should_not_see(text_or_regexp)
case text_or_regexp
when Regexp
return unless scoped_html.match(text_or_regexp)
else
return unless scoped_html.include?(text_or_regexp)
end
flunk("Should not see #{text_or_regexp.inspect} but did")
end
end
end

View File

@ -3,7 +3,7 @@ require "webrat/core_extensions/blank"
require "webrat/core_extensions/nil_to_param"
module Webrat
class Field
class Field #:nodoc:
def self.class_for_element(element)
if element.name == "input"
@ -59,7 +59,16 @@ module Webrat
def to_param
return nil if disabled?
param_parser.parse_query_parameters("#{name}=#{escaped_value}")
key_and_value = "#{name}=#{escaped_value}"
if defined?(CGIMethods)
CGIMethods.parse_query_parameters(key_and_value)
elsif defined?(ActionController::AbstractRequest)
ActionController::AbstractRequest.parse_query_parameters(key_and_value)
else
::Merb::Parse.query(key_and_value)
end
end
def set(value)
@ -102,7 +111,7 @@ module Webrat
end
unless id.blank?
@label_elements += @form.element / "label[@for=#{id}]"
@label_elements += @form.element.search("label[@for='#{id}']")
end
@label_elements
@ -112,16 +121,6 @@ module Webrat
@element["value"]
end
def param_parser
if defined?(CGIMethods)
CGIMethods
elsif defined?(ActionController::AbstractRequest)
ActionController::AbstractRequest
else
Webrat::ParamParser #used for Merb
end
end
def replace_param_value(params, oval, nval)
output = Hash.new
params.each do |key, value|
@ -139,10 +138,10 @@ module Webrat
end
end
class ButtonField < Field
class ButtonField < Field #:nodoc:
def matches_text?(text)
@element.innerHTML =~ /#{Regexp.escape(text.to_s)}/i
@element.inner_html =~ /#{Regexp.escape(text.to_s)}/i
end
def matches_value?(value)
@ -166,13 +165,13 @@ module Webrat
end
class HiddenField < Field
class HiddenField < Field #:nodoc:
def to_param
if collection_name?
super
else
checkbox_with_same_name = @form.find_field(name, CheckboxField)
checkbox_with_same_name = @form.field(name, CheckboxField)
if checkbox_with_same_name.to_param.blank?
super
@ -190,7 +189,7 @@ module Webrat
end
class CheckboxField < Field
class CheckboxField < Field #:nodoc:
def to_param
return nil if @value.nil?
@ -223,10 +222,10 @@ module Webrat
end
class PasswordField < Field
class PasswordField < Field #:nodoc:
end
class RadioField < Field
class RadioField < Field #:nodoc:
def to_param
return nil if @value.nil?
@ -258,7 +257,7 @@ module Webrat
end
class TextareaField < Field
class TextareaField < Field #:nodoc:
protected
@ -268,7 +267,7 @@ module Webrat
end
class FileField < Field
class FileField < Field #:nodoc:
attr_accessor :content_type
@ -297,13 +296,13 @@ module Webrat
end
class TextField < Field
class TextField < Field #:nodoc:
end
class ResetField < Field
class ResetField < Field #:nodoc:
end
class SelectField < Field
class SelectField < Field #:nodoc:
def find_option(text)
options.detect { |o| o.matches_text?(text) }
@ -312,11 +311,12 @@ module Webrat
protected
def default_value
selected_options = @element / "option[@selected='selected']"
selected_options = @element / "option:first" if selected_options.empty?
selected_options = @element.search(".//option[@selected='selected']")
selected_options = @element.search(".//option[position() = 1]") if selected_options.empty?
selected_options.map do |option|
return "" if option.nil?
option["value"] || option.innerHTML
option["value"] || option.inner_html
end
end
@ -325,7 +325,7 @@ module Webrat
end
def option_elements
(@element / "option")
@element.search(".//option")
end
end

View File

@ -1,6 +1,6 @@
module Flunk
def flunk(message)
def flunk(message) #:nodoc:
raise message
end

View File

@ -2,7 +2,7 @@ require "webrat/core/field"
require "webrat/core_extensions/blank"
module Webrat
class Form
class Form #:nodoc:
attr_reader :element
def initialize(session, element)
@ -11,12 +11,10 @@ module Webrat
@fields = nil
end
def find_field(id_or_name_or_label, *field_types)
possible_fields = fields_by_type(field_types)
find_field_by_id(possible_fields, id_or_name_or_label) ||
find_field_by_name(possible_fields, id_or_name_or_label) ||
find_field_by_label(possible_fields, id_or_name_or_label) ||
def field(locator, *field_types)
field_with_id(locator, *field_types) ||
field_named(locator, *field_types) ||
field_labeled(locator, *field_types) ||
nil
end
@ -41,7 +39,7 @@ module Webrat
def fields
return @fields if @fields
@fields = (@element / "button, input, textarea, select").collect do |field_element|
@fields = (@element.search(".//button", ".//input", ".//textarea", ".//select")).collect do |field_element|
Field.class_for_element(field_element).new(self, field_element)
end
end
@ -50,25 +48,32 @@ module Webrat
@session.request_page(form_action, form_method, params)
end
protected
def find_field_by_id(possible_fields, id)
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 find_field_by_name(possible_fields, name)
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 find_field_by_label(possible_fields, label)
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
protected
def fields_by_type(field_types)
fields.select { |f| field_types.include?(f.class) }
if field_types.any?
fields.select { |f| field_types.include?(f.class) }
else
fields
end
end
def params

View File

@ -1,5 +1,5 @@
module Webrat
class Label
class Label #:nodoc:
def initialize(field, element)
@field = field
@ -11,7 +11,7 @@ module Webrat
end
def text
@element.innerText
@element.inner_text
end
end

View File

@ -1,7 +1,7 @@
require "webrat/core_extensions/blank"
module Webrat
class Link
class Link #:nodoc:
def initialize(session, element)
@session = session
@ -22,13 +22,19 @@ module Webrat
end
def matches_text?(link_text)
html = text.gsub('&nbsp;',' ')
matcher = /#{Regexp.escape(link_text.to_s)}/i
html = text.gsub('&#xA0;',' ')
if link_text.is_a?(Regexp)
matcher = link_text
else
matcher = /#{Regexp.escape(link_text.to_s)}/i
end
html =~ matcher || title =~ matcher
end
def text
@element.innerHTML
@element.inner_html
end
protected

View File

@ -0,0 +1,92 @@
require "webrat/core_extensions/detect_mapped"
module Webrat
module Locators
def field(*args)
# This is the default locator strategy
find_field_with_id(*args) ||
find_field_named(*args) ||
field_labeled(*args) ||
flunk("Could not find field: #{args.inspect}")
end
def field_labeled(label, *field_types)
find_field_labeled(label, *field_types) ||
flunk("Could not find field labeled #{label.inspect}")
end
def field_named(name, *field_types)
find_field_named(name, *field_types) ||
flunk("Could not find field named #{name.inspect}")
end
def field_with_id(id, *field_types)
find_field_with_id(id, *field_types) ||
flunk("Could not find field with id #{id.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_field_named(name, *field_types) #:nodoc:
forms.detect_mapped do |form|
form.field_named(name, *field_types)
end
end
def find_field_with_id(id, *field_types) #:nodoc:
forms.detect_mapped do |form|
form.field_with_id(id, *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
flunk("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
flunk("Could not find button #{value.inspect}")
end
end
def find_area(area_name) #:nodoc:
areas.detect { |area| area.matches_text?(area_name) } ||
flunk("Could not find area with name #{area_name}")
end
def find_link(text) #:nodoc:
matching_links = links.select do |possible_link|
possible_link.matches_text?(text)
end
if matching_links.any?
matching_links.min { |a, b| a.text.length <=> b.text.length }
else
flunk("Could not find link with text #{text.inspect}")
end
end
end
end

View File

@ -1,5 +1,5 @@
module Webrat
module Logging
module Logging #:nodoc:
def warn_log(message) # :nodoc:
return unless logger

View File

@ -0,0 +1,4 @@
require "webrat/core/matchers/have_xpath"
require "webrat/core/matchers/have_selector"
require "webrat/core/matchers/have_tag"
require "webrat/core/matchers/have_content"

View File

@ -0,0 +1,94 @@
module Webrat
module Matchers
class HasContent #:nodoc:
def initialize(content)
@content = content
end
def matches?(stringlike)
if defined?(Nokogiri::XML)
matches_nokogiri?(stringlike)
else
matches_rexml?(stringlike)
end
end
def matches_rexml?(stringlike)
@document = rexml_document(stringlike)
@element = @document.inner_text
case @content
when String
@element.include?(@content)
when Regexp
@element.match(@content)
end
end
def matches_nokogiri?(stringlike)
@document = Webrat.nokogiri_document(stringlike)
@element = @document.inner_text
case @content
when String
@element.include?(@content)
when Regexp
@element.match(@content)
end
end
def rexml_document(stringlike)
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
case stringlike
when REXML::Document
stringlike.root
when REXML::Node
stringlike
when StringIO, String
begin
REXML::Document.new(stringlike.to_s).root
rescue REXML::ParseException => e
if e.message.include?("second root element")
REXML::Document.new("<fake-root-element>#{stringlike}</fake-root-element>").root
else
raise e
end
end
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
#
# ---
# @api public
def contain(content)
HasContent.new(content)
end
end
end

View File

@ -0,0 +1,39 @@
module Webrat
module Matchers
class HaveSelector < HaveXpath #:nodoc:
# ==== 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
def query
Nokogiri::CSS::Parser.parse(*super).map { |ast| ast.to_xpath }
end
end
# Matches HTML content against a CSS 3 selector.
#
# ==== Parameters
# expected<String>:: The CSS selector to look for.
#
# ==== Returns
# HaveSelector:: A new have selector matcher.
# ---
# @api public
def have_selector(expected, &block)
HaveSelector.new(expected, &block)
end
alias_method :match_selector, :have_selector
end
end

View File

@ -0,0 +1,58 @@
module Webrat
module HaveTagMatcher
class HaveTag < ::Webrat::Matchers::HaveSelector #:nodoc:
# ==== Returns
# String:: The failure message.
def failure_message
"expected following output to contain a #{tag_inspect} tag:\n#{@document}"
end
# ==== Returns
# String:: The failure message to be displayed in negative matches.
def negative_failure_message
"expected following output to omit a #{tag_inspect}:\n#{@document}"
end
def tag_inspect
options = @expected.last.dup
content = options.delete(:content)
html = "<#{@expected.first}"
options.each do |k,v|
html << " #{k}='#{v}'"
end
if content
html << ">#{content}</#{@expected.first}>"
else
html << "/>"
end
html
end
def query
options = @expected.last.dup
selector = @expected.first.to_s
selector << ":contains('#{options.delete(:content)}')" if options[:content]
options.each do |key, value|
selector << "[#{key}='#{value}']"
end
Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }
end
end
def have_tag(name, attributes = {}, &block)
HaveTag.new([name, attributes], &block)
end
alias_method :match_tag, :have_tag
end
end

View File

@ -0,0 +1,85 @@
require "webrat/core/nokogiri"
require "webrat/core/rexml"
module Webrat
module Matchers
class HaveXpath #:nodoc:
def initialize(expected, &block)
@expected = expected
@block = block
end
def matches?(stringlike)
if defined?(Nokogiri::XML)
matches_nokogiri?(stringlike)
else
matches_rexml?(stringlike)
end
end
def matches_rexml?(stringlike)
if REXML::Node === stringlike || Array === stringlike
@query = query.map { |q| q.gsub(%r'//', './') }
else
@query = query
end
@document = Webrat.rexml_document(stringlike)
matched = @query.map do |q|
if @document.is_a?(Array)
@document.map { |d| REXML::XPath.match(d, q) }
else
REXML::XPath.match(@document, q)
end
end.flatten.compact
matched.any? && (!@block || @block.call(matched))
end
def matches_nokogiri?(stringlike)
if Nokogiri::XML::NodeSet === stringlike
@query = query.map { |q| q.gsub(%r'//', './') }
else
@query = query
end
@document = Webrat.nokogiri_document(stringlike)
matched = @document.xpath(*@query)
matched.any? && (!@block || @block.call(matched))
end
def query
[@expected].flatten.compact
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
# Matches HTML content against an XPath query
#
# ==== Parameters
# expected<String>:: The XPath query to look for.
#
# ==== Returns
# HaveXpath:: A new have xpath matcher.
# ---
# @api public
def have_xpath(expected, &block)
HaveXpath.new(expected, &block)
end
alias_method :match_xpath, :have_xpath
end
end

View File

@ -0,0 +1,44 @@
module Webrat
module Methods #:nodoc:
def self.delegate_to_session(*meths)
meths.each do |meth|
self.class_eval <<-RUBY
def #{meth}(*args, &blk)
@_webrat_session ||= ::Webrat::MerbSession.new
@_webrat_session.#{meth}(*args, &blk)
end
RUBY
end
end
# all of these methods delegate to the @session, which should
# be created transparently.
#
# Note that when using Webrat, #request also uses @session, so
# that #request and webrat native functions behave interchangably
delegate_to_session \
:visits, :visit,
:within,
:header, :http_accept, :basic_auth,
:save_and_open_page,
:fill_in,
:check,
:uncheck,
:choose,
:select,
:attach_file,
:cookies,
:response,
:current_page,
:current_url,
:click_link,
:click_area,
:click_button,
:reload, :reloads,
:clicks_link_within,
:field_labeled
end
end

View File

@ -0,0 +1,44 @@
require "webrat/core_extensions/meta_class"
module Webrat
def self.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.define_dom_method(object, dom) #:nodoc:
object.meta_class.send(:define_method, :dom) do
dom
end
end
end
module Nokogiri
module CSS
class XPathVisitor
def visit_pseudo_class_text(node) #:nodoc:
"@type='text'"
end
def visit_pseudo_class_password(node) #:nodoc:
"@type='password'"
end
end
end
end

24
lib/webrat/core/rexml.rb Normal file
View File

@ -0,0 +1,24 @@
module Webrat
def self.rexml_document(stringlike)
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
case stringlike
when REXML::Document
stringlike.root
when REXML::Node, Array
stringlike
else
begin
REXML::Document.new(stringlike.to_s).root
rescue REXML::ParseException => e
if e.message.include?("second root element")
REXML::Document.new("<fake-root-element>#{stringlike}</fake-root-element>").root
else
raise e
end
end
end
end
end

View File

@ -1,69 +1,81 @@
require "hpricot"
require "webrat/core/form"
require "webrat/core/assertions"
require "webrat/core/locators"
module Webrat
class Scope
include Logging
include Flunk
include Assertions
include Locators
def initialize(session, html, selector = nil)
@session = session
@html = html
@selector = selector
def self.from_page(session, response, response_body) #:nodoc:
new(session) do
@response = response
@response_body = response_body
end
end
def self.from_scope(session, scope, selector) #:nodoc:
new(session) do
@scope = scope
@selector = selector
end
end
def initialize(session, &block) #:nodoc:
@session = session
instance_eval(&block) if block_given?
end
# Verifies an input field or textarea exists on the current page, and stores a value for
# it which will be sent when the form is submitted.
#
# Examples:
# fills_in "Email", :with => "user@example.com"
# fills_in "user[email]", :with => "user@example.com"
# fill_in "Email", :with => "user@example.com"
# fill_in "user[email]", :with => "user@example.com"
#
# The field value is required, and must be specified in <tt>options[:with]</tt>.
# <tt>field</tt> can be either the value of a name attribute (i.e. <tt>user[email]</tt>)
# or the text inside a <tt><label></tt> element that points at the <tt><input></tt> field.
def fills_in(id_or_name_or_label, options = {})
field = find_field(id_or_name_or_label, TextField, TextareaField, PasswordField)
def fill_in(field_locator, options = {})
field = locate_field(field_locator, TextField, TextareaField, PasswordField)
field.raise_error_if_disabled
field.set(options[:with])
end
alias_method :fill_in, :fills_in
alias_method :fills_in, :fill_in
# Verifies that an input checkbox exists on the current page and marks it
# as checked, so that the value will be submitted with the form.
#
# Example:
# checks 'Remember Me'
def checks(id_or_name_or_label)
find_field(id_or_name_or_label, CheckboxField).check
# check 'Remember Me'
def check(field_locator)
locate_field(field_locator, CheckboxField).check
end
alias_method :check, :checks
alias_method :checks, :check
# Verifies that an input checkbox exists on the current page and marks it
# as unchecked, so that the value will not be submitted with the form.
#
# Example:
# unchecks 'Remember Me'
def unchecks(id_or_name_or_label)
find_field(id_or_name_or_label, CheckboxField).uncheck
# uncheck 'Remember Me'
def uncheck(field_locator)
locate_field(field_locator, CheckboxField).uncheck
end
alias_method :uncheck, :unchecks
alias_method :unchecks, :uncheck
# Verifies that an input radio button exists on the current page and marks it
# as checked, so that the value will be submitted with the form.
#
# Example:
# chooses 'First Option'
def chooses(label)
find_field(label, RadioField).choose
# choose 'First Option'
def choose(field_locator)
locate_field(field_locator, RadioField).choose
end
alias_method :choose, :chooses
alias_method :chooses, :choose
# Verifies that a an option element exists on the current page with the specified
# text. You can optionally restrict the search to a specific select list by
@ -87,22 +99,22 @@ module Webrat
# Example:
# attaches_file "Resume", "/path/to/the/resume.txt"
# attaches_file "Photo", "/path/to/the/image.png", "image/png"
def attaches_file(id_or_name_or_label, path, content_type = nil)
find_field(id_or_name_or_label, FileField).set(path, content_type)
def attach_file(field_locator, path, content_type = nil)
locate_field(field_locator, FileField).set(path, content_type)
end
alias_method :attach_file, :attaches_file
alias_method :attaches_file, :attach_file
def clicks_area(area_name)
def click_area(area_name)
find_area(area_name).click
end
alias_method :click_area, :clicks_area
alias_method :clicks_area, :click_area
# Issues a request for the URL pointed to by a link on the current page,
# follows any redirects, and verifies the final page load was successful.
#
# clicks_link has very basic support for detecting Rails-generated
# click_link has very basic support for detecting Rails-generated
# JavaScript onclick handlers for PUT, POST and DELETE links, as well as
# CSRF authenticity tokens if they are present.
#
@ -112,139 +124,81 @@ module Webrat
# for making the link request
#
# Example:
# clicks_link "Sign up"
# click_link "Sign up"
#
# clicks_link "Sign up", :javascript => false
# click_link "Sign up", :javascript => false
#
# clicks_link "Sign up", :method => :put
def clicks_link(link_text, options = {})
# click_link "Sign up", :method => :put
def click_link(link_text, options = {})
find_link(link_text).click(options)
end
alias_method :click_link, :clicks_link
def clicks_get_link(link_text) # :nodoc:
clicks_link link_text, :method => :get
end
alias_method :click_get_link, :clicks_get_link
def clicks_delete_link(link_text) # :nodoc:
clicks_link link_text, :method => :delete
end
alias_method :click_delete_link, :clicks_delete_link
def clicks_post_link(link_text) # :nodoc:
clicks_link link_text, :method => :post
end
alias_method :click_post_link, :clicks_post_link
def clicks_put_link(link_text) # :nodoc:
clicks_link link_text, :method => :put
end
alias_method :click_put_link, :clicks_put_link
alias_method :clicks_link, :click_link
# Verifies that a submit button exists for the form, then submits the form, follows
# any redirects, and verifies the final page was successful.
#
# Example:
# clicks_button "Login"
# clicks_button
# click_button "Login"
# click_button
#
# The URL and HTTP method for the form submission are automatically read from the
# <tt>action</tt> and <tt>method</tt> attributes of the <tt><form></tt> element.
def clicks_button(value = nil)
def click_button(value = nil)
find_button(value).click
end
alias_method :click_button, :clicks_button
alias_method :clicks_button, :click_button
def dom # :nodoc:
@dom ||= Hpricot(scoped_html)
end
return @dom if @dom
def field_labeled(label)
find_field(label, TextField, TextareaField, CheckboxField, RadioField, HiddenField)
if @selector
@dom = scoped_dom
else
@dom = page_dom
end
return @dom
end
protected
def scoped_html
@scoped_html ||= begin
if @selector
(Hpricot(@html) / @selector).first.to_html
else
@html
end
end
def page_dom #:nodoc:
return @response.dom if @response.respond_to?(:dom)
dom = Webrat.nokogiri_document(@response_body)
Webrat.define_dom_method(@response, dom)
return dom
end
def find_select_option(option_text, id_or_name_or_label)
if id_or_name_or_label
field = find_field(id_or_name_or_label, SelectField)
return field.find_option(option_text)
def scoped_dom #:nodoc:
Webrat.nokogiri_document(@scope.dom.search(@selector).first.to_html)
end
def locate_field(field_locator, *field_types) #:nodoc:
if field_locator.is_a?(Field)
field_locator
else
forms.each do |form|
result = form.find_select_option(option_text)
return result if result
end
field(field_locator, *field_types)
end
flunk("Could not find option #{option_text.inspect}")
end
def find_button(value)
forms.each do |form|
button = form.find_button(value)
return button if button
end
flunk("Could not find button #{value.inspect}")
end
def find_area(area_name)
areas.select{|area| area.matches_text?(area_name)}.first || flunk("Could not find area with name #{area_name}")
end
def areas
(dom / "area").map do |element|
def areas #:nodoc:
dom.search("area").map do |element|
Area.new(@session, element)
end
end
def find_link(text, selector = nil)
matching_links = links_within(selector).select do |possible_link|
possible_link.matches_text?(text)
end
if matching_links.any?
matching_links.min { |a, b| a.text.length <=> b.text.length }
else
flunk("Could not find link with text #{text.inspect}")
end
end
def find_field(id_or_name_or_label, *field_types)
forms.each do |form|
result = form.find_field(id_or_name_or_label, *field_types)
return result if result
end
flunk("Could not find #{field_types.inspect}: #{id_or_name_or_label.inspect}")
end
def links_within(selector)
(dom / selector / "a[@href]").map do |link_element|
def links #:nodoc:
dom.search("a[@href]").map do |link_element|
Link.new(@session, link_element)
end
end
def forms
def forms #:nodoc:
return @forms if @forms
@forms = (dom / "form").map do |form_element|
@forms = dom.search("form").map do |form_element|
Form.new(@session, form_element)
end
end

View File

@ -1,5 +1,5 @@
module Webrat
class SelectOption
class SelectOption #:nodoc:
def initialize(select, element)
@select = select
@ -8,9 +8,9 @@ module Webrat
def matches_text?(text)
if text.is_a?(Regexp)
@element.innerHTML =~ text
@element.inner_html =~ text
else
@element.innerHTML == text.to_s
@element.inner_html == text.to_s
end
end
@ -22,7 +22,7 @@ module Webrat
protected
def value
@element["value"] || @element.innerHTML
@element["value"] || @element.inner_html
end
end

View File

@ -4,14 +4,16 @@ require "ostruct"
require "webrat/core/mime"
module Webrat
class PageLoadError < WebratError
end
class Session
extend Forwardable
include Logging
include Flunk
attr_reader :current_url
def initialize
def initialize #:nodoc:
@http_method = :get
@data = {}
@default_headers = {}
@ -35,12 +37,12 @@ module Webrat
open_in_browser(filename)
end
def current_dom
def current_dom #:nodoc:
current_scope.dom
end
# For backwards compatibility -- removing in 1.0
def current_page
def current_page #:nodoc:
page = OpenStruct.new
page.url = @current_url
page.http_method = @http_method
@ -48,11 +50,11 @@ module Webrat
page
end
def doc_root
def doc_root #:nodoc:
nil
end
def saved_page_dir
def saved_page_dir #:nodoc:
File.expand_path(".")
end
@ -69,11 +71,11 @@ module Webrat
header('HTTP_AUTHORIZATION', "Basic #{encoded_login}")
end
def headers
def headers #:nodoc:
@default_headers.dup.merge(@custom_headers.dup)
end
def request_page(url, http_method, data)
def request_page(url, http_method, data) #:nodoc:
h = headers
h['HTTP_REFERER'] = @current_url if @current_url
@ -85,24 +87,27 @@ module Webrat
end
save_and_open_page if exception_caught?
flunk("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
@scope = nil
@_scopes = nil
@_page_scope = nil
@current_url = url
@http_method = http_method
@data = data
return response
end
def success_code?
def success_code? #:nodoc:
(200..499).include?(response_code)
end
def exception_caught?
def exception_caught? #:nodoc:
response_body =~ /Exception caught/
end
def current_scope
@scope ||= Scope.new(self, response_body)
def current_scope #:nodoc:
scopes.last || page_scope
end
# Reloads the last page requested. Note that this will resubmit forms
@ -117,47 +122,58 @@ module Webrat
alias_method :reload, :reloads
# Works like clicks_link, but only looks for the link text within a given selector
# Works like click_link, but only looks for the link text within a given selector
#
# Example:
# clicks_link_within "#user_12", "Vote"
def clicks_link_within(selector, link_text)
within(selector) do |scope|
scope.clicks_link(link_text)
# click_link_within "#user_12", "Vote"
def click_link_within(selector, link_text)
within(selector) do
click_link(link_text)
end
end
alias_method :click_link_within, :clicks_link_within
alias_method :clicks_link_within, :click_link_within
def within(selector)
yield Scope.new(self, response_body, selector)
scopes.push(Scope.from_scope(self, current_scope, selector))
ret = yield(current_scope)
scopes.pop
return ret
end
# Issues a GET request for a page, follows any redirects, and verifies the final page
# load was successful.
#
# Example:
# visits "/"
def visits(url = nil, http_method = :get, data = {})
# visit "/"
def visit(url = nil, http_method = :get, data = {})
request_page(url, http_method, data)
end
alias_method :visit, :visits
alias_method :visits, :visit
def open_in_browser(path) # :nodoc
def open_in_browser(path) #:nodoc
`open #{path}`
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
# Subclasses can override this to show error messages without html
def formatted_error
def formatted_error #:nodoc:
response_body
end
def scopes #:nodoc:
@_scopes ||= []
end
def page_scope #:nodoc:
@_page_scope ||= Scope.from_page(self, response, response_body)
end
def_delegators :current_scope, :fill_in, :fills_in
def_delegators :current_scope, :check, :checks
def_delegators :current_scope, :uncheck, :unchecks
@ -166,10 +182,6 @@ module Webrat
def_delegators :current_scope, :attach_file, :attaches_file
def_delegators :current_scope, :click_area, :clicks_area
def_delegators :current_scope, :click_link, :clicks_link
def_delegators :current_scope, :click_get_link, :clicks_get_link
def_delegators :current_scope, :click_delete_link, :clicks_delete_link
def_delegators :current_scope, :click_post_link, :clicks_post_link
def_delegators :current_scope, :click_put_link, :clicks_put_link
def_delegators :current_scope, :click_button, :clicks_button
def_delegators :current_scope, :should_see
def_delegators :current_scope, :should_not_see

View File

@ -1,4 +1,4 @@
class Object
class Object #:nodoc:
# An object is blank if it's false, empty, or a whitespace string.
# For example, "", " ", +nil+, [], and {} are blank.
#

View File

@ -0,0 +1,8 @@
class Module #:nodoc:
def deprecate(old_method_name, new_method_name)
define_method old_method_name do |*args|
warn "#{old_method_name} is deprecated. Use #{new_method_name} instead."
__send__(new_method_name, *args)
end
end
end

View File

@ -0,0 +1,12 @@
class Array #:nodoc:
def detect_mapped
each do |element|
result = yield element
return result if result
end
return nil
end
end

View File

@ -1,7 +1,7 @@
# This class has dubious semantics and we only have it so that
# people can write params[:key] instead of params['key']
# and they get the same value for both keys.
class HashWithIndifferentAccess < Hash
class HashWithIndifferentAccess < Hash #:nodoc:
def initialize(constructor = {})
if constructor.is_a?(Hash)
super()
@ -122,7 +122,7 @@ class HashWithIndifferentAccess < Hash
end
end
class Hash
class Hash #:nodoc:
def with_indifferent_access
hash = HashWithIndifferentAccess.new(self)
hash.default = self.default

View File

@ -0,0 +1,6 @@
class ::Object #:nodoc:
def meta_class
class << self; self end
end
end

View File

@ -1,4 +1,4 @@
class NilClass
class NilClass #:nodoc:
def to_param
nil
end

View File

@ -1,3 +1,41 @@
require "mechanize"
require "webrat/mechanize/mechanize_session"
module Webrat
class MechanizeSession < Session
def initialize(mechanize = WWW::Mechanize.new)
super()
@mechanize = mechanize
end
def page
@mechanize_page
end
def get(url, data, headers_argument_not_used = nil)
@mechanize_page = @mechanize.get(url, data)
end
def post(url, data, headers_argument_not_used = nil)
post_data = data.inject({}) do |memo, param|
case param.last
when Hash
param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
else
memo[param.first] = param.last
end
memo
end
@mechanize_page = @mechanize.post(url, post_data)
end
def response_body
@mechanize_page.content
end
def response_code
@mechanize_page.code.to_i
end
end
end

View File

@ -1,39 +0,0 @@
module Webrat
class MechanizeSession < Session
def initialize(mechanize = WWW::Mechanize.new)
super()
@mechanize = mechanize
end
def page
@mechanize_page
end
def get(url, data, headers_argument_not_used = nil)
@mechanize_page = @mechanize.get(url, data)
end
def post(url, data, headers_argument_not_used = nil)
post_data = data.inject({}) do |memo, param|
case param.last
when Hash
param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
else
memo[param.first] = param.last
end
memo
end
@mechanize_page = @mechanize.post(url, post_data)
end
def response_body
@mechanize_page.content
end
def response_code
@mechanize_page.code.to_i
end
end
end

View File

@ -1,10 +1,17 @@
require "webrat/core"
module Webrat
class Session
include Merb::Test::RequestHelper
require "cgi"
gem "extlib"
require "extlib"
require "merb-core"
attr_reader :response
HashWithIndifferentAccess = Mash
module Webrat
class MerbSession < Session #:nodoc:
include Merb::Test::MakeRequest
attr_accessor :response
def get(url, data, headers = nil)
do_request(url, data, headers, "GET")
@ -30,19 +37,39 @@ module Webrat
@response.status
end
protected
def do_request(url, data, headers, method)
@response = request(url, :params => (data && data.any?) ? data : nil, :headers => headers, :method => method)
@response = request(url,
:params => (data && data.any?) ? data : nil,
:headers => headers,
:method => method)
follow_redirect
end
def follow_redirect
self.get(@response.headers['Location'], nil, @response.headers) if @response.status == 302
end
end
end
class Merb::Test::RspecStory
def browser
@browser ||= Webrat::Session.new
module Merb
module Test
module RequestHelper #:nodoc:
def request(uri, env = {})
@_webrat_session ||= Webrat::MerbSession.new
@_webrat_session.response = @_webrat_session.request(uri, env)
end
def follow_redirect
@_webrat_session.follow_redirect
end
end
end
end
class Merb::Test::RspecStory #:nodoc:
def browser
@browser ||= Webrat::MerbSession.new
end
end

View File

@ -1,6 +1,6 @@
require 'webrat'
class CGIMethods
class CGIMethods #:nodoc:
def self.parse_query_parameters(params)
hash = {}
params.split('&').each do |p|
@ -12,7 +12,7 @@ class CGIMethods
end
module Webrat
class RackSession < Session
class RackSession < Session #:nodoc:
def response_body
@response.body
end

View File

@ -1,4 +1,102 @@
require "webrat/core"
require "webrat/rails/redirect_actions"
require "webrat/rails/rails_session"
require "webrat/rails/session"
require "webrat"
module Webrat
class RailsSession < Session #:nodoc:
def initialize(integration_session)
super()
@integration_session = integration_session
end
def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public'))
end
def saved_page_dir
File.expand_path(File.join(RAILS_ROOT, "tmp"))
end
def get(url, data, headers = nil)
do_request(:get, url, data, headers)
end
def post(url, data, headers = nil)
do_request(:post, url, data, headers)
end
def put(url, data, headers = nil)
do_request(:put, url, data, headers)
end
def delete(url, data, headers = nil)
do_request(:delete, url, data, headers)
end
def response_body
response.body
end
def response_code
response.code.to_i
end
protected
def do_request(http_method, url, data, headers) #:nodoc:
update_protocol(url)
@integration_session.request_via_redirect(http_method, remove_protocol(url), data, headers)
end
def remove_protocol(href) #:nodoc:
if href =~ %r{^https?://www.example.com(/.*)}
$LAST_MATCH_INFO.captures.first
else
href
end
end
def update_protocol(href) #:nodoc:
if href =~ /^https:/
@integration_session.https!(true)
elsif href =~ /^http:/
@integration_session.https!(false)
end
end
def response #:nodoc:
@integration_session.response
end
end
end
module ActionController
module Integration
class Session #:nodoc:
unless instance_methods.include?("put_via_redirect")
require "webrat/rails/redirect_actions"
include Webrat::RedirectActions
end
def respond_to?(name)
super || webrat_session.respond_to?(name)
end
def method_missing(name, *args, &block)
if webrat_session.respond_to?(name)
webrat_session.send(name, *args, &block)
else
super
end
end
protected
def webrat_session
@webrat_session ||= Webrat::RailsSession.new(self)
end
end
end
end

View File

@ -1,68 +0,0 @@
module Webrat
class RailsSession < Session
def initialize(integration_session)
super()
@integration_session = integration_session
end
def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public'))
end
def saved_page_dir
File.expand_path(File.join(RAILS_ROOT, "tmp"))
end
def get(url, data, headers = nil)
update_protocol(url)
@integration_session.get_via_redirect(remove_protocol(url), data, headers)
end
def post(url, data, headers = nil)
update_protocol(url)
@integration_session.post_via_redirect(remove_protocol(url), data, headers)
end
def put(url, data, headers = nil)
update_protocol(url)
@integration_session.put_via_redirect(remove_protocol(url), data, headers)
end
def delete(url, data, headers = nil)
update_protocol(url)
@integration_session.delete_via_redirect(remove_protocol(url), data, headers)
end
def response_body
response.body
end
def response_code
response.code.to_i
end
protected
def remove_protocol(href)
if href =~ %r{^https?://www.example.com(/.*)}
$LAST_MATCH_INFO.captures.first
else
href
end
end
def update_protocol(href)
if href =~ /^https:/
@integration_session.https!(true)
elsif href =~ /^http:/
@integration_session.https!(false)
end
end
def response
@integration_session.response
end
end
end

View File

@ -1,6 +1,6 @@
# For Rails before http://dev.rubyonrails.org/ticket/10497 was committed
module Webrat
module RedirectActions
module RedirectActions #:nodoc:
def put_via_redirect(path, parameters = {}, headers = {})
put path, parameters, headers

View File

@ -1,29 +0,0 @@
module ActionController
module Integration
class Session
unless instance_methods.include?("put_via_redirect")
include Webrat::RedirectActions
end
def respond_to?(name)
super || webrat_session.respond_to?(name)
end
def method_missing(name, *args, &block)
if webrat_session.respond_to?(name)
webrat_session.send(name, *args, &block)
else
super
end
end
protected
def webrat_session
@webrat_session ||= Webrat::RailsSession.new(self)
end
end
end
end

View File

@ -6,7 +6,7 @@ return $A(inputs).find(function(candidate){
inputType = candidate.getAttribute('type');
if (inputType == 'submit' || inputType == 'image') {
var buttonText = $F(candidate);
return (PatternMatcher.matches(locator + '*', buttonText));
return (PatternMatcher.matches(locator, buttonText));
}
return false;
});

View File

@ -0,0 +1,6 @@
PatternMatcher.strategies['evalregex'] = function(regexpString) {
this.regexp = eval(regexpString);
this.matches = function(actual) {
return this.regexp.test(actual);
};
};

View File

@ -1,46 +1,57 @@
module Webrat
class SeleniumSession < Session
class SeleniumSession
def initialize(selenium_driver)
super()
def initialize(selenium_driver) #:nodoc:
@selenium = selenium_driver
extend_selenium
define_location_strategies
end
def visits(url)
def visit(url)
@selenium.open(url)
end
alias_method :visit, :visits
alias_method :visits, :visit
def fills_in(field_identifier, options)
def fill_in(field_identifier, options)
locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.type(locator, "#{options[:with]}")
end
def response_body
alias_method :fills_in, :fill_in
def response_body #:nodoc:
@selenium.get_html_source
end
def clicks_button(button_text = nil, options = {})
button_text, options = nil, button_text if button_text.is_a?(Hash) && options == {}
button_text ||= '*'
@selenium.click("button=#{button_text}")
def click_button(button_text_or_regexp = nil, options = {})
if button_text_or_regexp.is_a?(Hash) && options == {}
pattern, options = nil, button_text_or_regexp
else
pattern = adjust_if_regexp(button_text_or_regexp)
end
pattern ||= '*'
@selenium.click("button=#{pattern}")
wait_for_result(options[:wait])
end
alias_method :click_button, :clicks_button
def clicks_link(link_text, options = {})
@selenium.click("webratlink=#{link_text}")
alias_method :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])
end
alias_method :click_link, :clicks_link
def clicks_link_within(selector, link_text, options = {})
alias_method :clicks_link, :click_link
def click_link_within(selector, link_text, options = {})
@selenium.click("webratlinkwithin=#{selector}|#{link_text}")
wait_for_result(options[:wait])
end
alias_method :clicks_link_within, :click_link_within
def wait_for_result(wait_type)
if wait_type == :ajax
wait_for_ajax
@ -79,25 +90,43 @@ module Webrat
@selenium.select(select_locator, option_text)
end
def chooses(label_text)
def choose(label_text)
@selenium.click("webrat=#{label_text}")
end
def checks(label_text)
alias_method :chooses, :choose
def check(label_text)
@selenium.check("webrat=#{label_text}")
end
def is_ordered(*args)
alias_method :checks, :check
def is_ordered(*args) #:nodoc:
@selenium.is_ordered(*args)
end
def dragdrop(*args)
def dragdrop(*args) #:nodoc:
@selenium.dragdrop(*args)
end
protected
def define_location_strategies
def adjust_if_regexp(text_or_regexp) #:nodoc:
if text_or_regexp.is_a?(Regexp)
"evalregex:#{text_or_regexp.inspect}"
else
text_or_regexp
end
end
def extend_selenium #:nodoc:
extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js")
extenions_js = File.read(extensions_file)
@selenium.get_eval(extenions_js)
end
def define_location_strategies #:nodoc:
Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
strategy_js = File.read(file)
strategy_name = File.basename(file, '.js')

View File

@ -1,9 +1,9 @@
require 'webrat/rack/rack_session'
require 'webrat/rack'
require 'sinatra'
require 'sinatra/test/methods'
module Webrat
class SinatraSession < RackSession
class SinatraSession < RackSession #:nodoc:
include Sinatra::Test::Methods
%w(get head post put delete).each do |verb|

View File

@ -6,9 +6,9 @@ describe "Basic Auth HTTP headers" do
@session.basic_auth('user', 'secret')
end
it "should be present in visits" do
it "should be present in visit" do
@session.should_receive(:get).with("/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
@session.visits("/")
@session.visit("/")
end
it "should be present in form submits" do
@ -18,6 +18,6 @@ describe "Basic Auth HTTP headers" do
</form>
EOS
@session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
@session.clicks_button
@session.click_button
end
end

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "checks" do
describe "check" do
before do
@session = Webrat::TestSession.new
end
@ -11,7 +11,7 @@ describe "checks" do
</form>
EOS
lambda { @session.checks "remember_me" }.should raise_error
lambda { @session.check "remember_me" }.should raise_error
end
it "should fail if input is not a checkbox" do
@ -21,7 +21,7 @@ describe "checks" do
</form>
EOS
lambda { @session.checks "remember_me" }.should raise_error
lambda { @session.check "remember_me" }.should raise_error
end
it "should check rails style checkboxes" do
@ -34,8 +34,8 @@ describe "checks" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
@session.checks "TOS"
@session.clicks_button
@session.check "TOS"
@session.click_button
end
it "should result in the value on being posted if not specified" do
@ -46,8 +46,8 @@ describe "checks" do
</form>
EOS
@session.should_receive(:post).with("/login", "remember_me" => "on")
@session.checks "remember_me"
@session.clicks_button
@session.check "remember_me"
@session.click_button
end
it "should fail if the checkbox is disabled" do
@ -57,7 +57,7 @@ describe "checks" do
<input type="submit" />
</form>
EOS
lambda { @session.checks "remember_me" }.should raise_error
lambda { @session.check "remember_me" }.should raise_error
end
it "should result in a custom value being posted" do
@ -68,12 +68,12 @@ describe "checks" do
</form>
EOS
@session.should_receive(:post).with("/login", "remember_me" => "yes")
@session.checks "remember_me"
@session.clicks_button
@session.check "remember_me"
@session.click_button
end
end
describe "unchecks" do
describe "uncheck" do
before do
@session = Webrat::TestSession.new
end
@ -84,7 +84,7 @@ describe "unchecks" do
</form>
EOS
lambda { @session.unchecks "remember_me" }.should raise_error
lambda { @session.uncheck "remember_me" }.should raise_error
end
it "should fail if input is not a checkbox" do
@ -94,7 +94,7 @@ describe "unchecks" do
</form>
EOS
lambda { @session.unchecks "remember_me" }.should raise_error
lambda { @session.uncheck "remember_me" }.should raise_error
end
it "should fail if the checkbox is disabled" do
@ -104,7 +104,7 @@ describe "unchecks" do
<input type="submit" />
</form>
EOS
lambda { @session.unchecks "remember_me" }.should raise_error
lambda { @session.uncheck "remember_me" }.should raise_error
end
it "should uncheck rails style checkboxes" do
@ -117,9 +117,9 @@ describe "unchecks" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
@session.checks "TOS"
@session.unchecks "TOS"
@session.clicks_button
@session.check "TOS"
@session.uncheck "TOS"
@session.click_button
end
it "should result in value not being posted" do
@ -130,7 +130,7 @@ describe "unchecks" do
</form>
EOS
@session.should_receive(:post).with("/login", {})
@session.unchecks "remember_me"
@session.clicks_button
@session.uncheck "remember_me"
@session.click_button
end
end

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "chooses" do
describe "choose" do
before do
@session = Webrat::TestSession.new
end
@ -11,7 +11,7 @@ describe "chooses" do
</form>
EOS
lambda { @session.chooses "first option" }.should raise_error
lambda { @session.choose "first option" }.should raise_error
end
it "should fail if input is not a radio button" do
@ -21,7 +21,7 @@ describe "chooses" do
</form>
EOS
lambda { @session.chooses "first_option" }.should raise_error
lambda { @session.choose "first_option" }.should raise_error
end
it "should check rails style radio buttons" do
@ -35,8 +35,8 @@ describe "chooses" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"gender" => "M"})
@session.chooses "Male"
@session.clicks_button
@session.choose "Male"
@session.click_button
end
it "should only submit last chosen value" do
@ -50,9 +50,9 @@ describe "chooses" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"gender" => "M"})
@session.chooses "Female"
@session.chooses "Male"
@session.clicks_button
@session.choose "Female"
@session.choose "Male"
@session.click_button
end
it "should fail if the radio button is disabled" do
@ -63,7 +63,7 @@ describe "chooses" do
</form>
EOS
lambda { @session.chooses "first_option" }.should raise_error
lambda { @session.choose "first_option" }.should raise_error
end
it "should result in the value on being posted if not specified" do
@ -74,8 +74,8 @@ describe "chooses" do
</form>
EOS
@session.should_receive(:post).with("/login", "first_option" => "on")
@session.chooses "first_option"
@session.clicks_button
@session.choose "first_option"
@session.click_button
end
it "should result in the value on being posted if not specified and checked by default" do
@ -86,7 +86,7 @@ describe "chooses" do
</form>
EOS
@session.should_receive(:post).with("/login", "first_option" => "on")
@session.clicks_button
@session.click_button
end
it "should result in the value of the selected radio button being posted when a subsequent one is checked by default" do
@ -100,7 +100,7 @@ describe "chooses" do
</form>
EOS
@session.should_receive(:post).with("/login", "user" => {"gender" => "M"})
@session.chooses "Male"
@session.clicks_button
@session.choose "Male"
@session.click_button
end
end

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "clicks_area" do
describe "click_area" do
before do
@session = Webrat::TestSession.new
end
@ -12,7 +12,7 @@ describe "clicks_area" do
</map>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_area "Berlin"
@session.click_area "Berlin"
end
it "should assert valid response" do
@ -22,7 +22,7 @@ describe "clicks_area" do
</map>
EOS
@session.response_code = 501
lambda { @session.clicks_area "Berlin" }.should raise_error
lambda { @session.click_area "Berlin" }.should raise_error
end
[200, 300, 400, 499].each do |status|
@ -33,7 +33,7 @@ describe "clicks_area" do
</map>
EOS
@session.response_code = status
lambda { @session.clicks_area "Berlin" }.should_not raise_error
lambda { @session.click_area "Berlin" }.should_not raise_error
end
end
@ -45,7 +45,7 @@ describe "clicks_area" do
EOS
lambda {
@session.clicks_area "Missing area"
@session.click_area "Missing area"
}.should raise_error
end
@ -56,7 +56,7 @@ describe "clicks_area" do
</map>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_area "berlin"
@session.click_area "berlin"
end
@ -68,7 +68,7 @@ describe "clicks_area" do
</map>
EOS
@session.should_receive(:get).with("/page/sub", {})
@session.clicks_area "Berlin"
@session.click_area "Berlin"
end
it "should follow fully qualified local links" do
@ -78,7 +78,7 @@ describe "clicks_area" do
</map>
EOS
@session.should_receive(:get).with("http://www.example.com/page", {})
@session.clicks_area "Berlin"
@session.click_area "Berlin"
end
it "should follow query parameters" do
@ -88,6 +88,6 @@ describe "clicks_area" do
</map>
EOS
@session.should_receive(:get).with("/page?foo=bar", {})
@session.clicks_area "Berlin"
@session.click_area "Berlin"
end
end

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "clicks_button" do
describe "click_button" do
before do
@session = Webrat::TestSession.new
end
@ -10,7 +10,7 @@ describe "clicks_button" do
<form method="get" action="/login"></form>
EOS
lambda { @session.clicks_button }.should raise_error
lambda { @session.click_button }.should raise_error
end
it "should fail if input is not a submit button" do
@ -20,7 +20,7 @@ describe "clicks_button" do
</form>
EOS
lambda { @session.clicks_button }.should raise_error
lambda { @session.click_button }.should raise_error
end
@ -31,7 +31,7 @@ describe "clicks_button" do
</form>
EOS
lambda { @session.clicks_button }.should raise_error
lambda { @session.click_button }.should raise_error
end
it "should default to get method" do
@ -41,7 +41,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get)
@session.clicks_button
@session.click_button
end
it "should assert valid response" do
@ -51,7 +51,7 @@ describe "clicks_button" do
</form>
EOS
@session.response_code = 501
lambda { @session.clicks_button }.should raise_error
lambda { @session.click_button }.should raise_error
end
[200, 300, 400, 499].each do |status|
@ -62,7 +62,7 @@ describe "clicks_button" do
</form>
EOS
@session.response_code = status
lambda { @session.clicks_button }.should_not raise_error
lambda { @session.click_button }.should_not raise_error
end
end
@ -76,7 +76,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/form1", {})
@session.clicks_button
@session.click_button
end
it "should not explode on file fields" do
@ -86,7 +86,7 @@ describe "clicks_button" do
<input type="submit" />
</form>
EOS
@session.clicks_button
@session.click_button
end
it "should submit the form with the specified button" do
@ -99,7 +99,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/form2", {})
@session.clicks_button "Form2"
@session.click_button "Form2"
end
it "should use action from form" do
@ -109,7 +109,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", {})
@session.clicks_button
@session.click_button
end
it "should use method from form" do
@ -119,7 +119,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:post)
@session.clicks_button
@session.click_button
end
it "should send button as param if it has a name" do
@ -130,7 +130,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:post).with("/login", "login" => "Login")
@session.clicks_button("Login")
@session.click_button("Login")
end
it "should not send button as param if it has no name" do
@ -141,7 +141,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:post).with("/login", {})
@session.clicks_button("Login")
@session.click_button("Login")
end
it "should send default password field values" do
@ -152,7 +152,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"password" => "mypass"})
@session.clicks_button
@session.click_button
end
it "should send default hidden field values" do
@ -163,7 +163,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
@session.clicks_button
@session.click_button
end
it "should send default text field values" do
@ -174,7 +174,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
@session.clicks_button
@session.click_button
end
it "should not send disabled field values" do
@ -189,7 +189,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", {})
@session.clicks_button
@session.click_button
end
it "should send default checked fields" do
@ -200,7 +200,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
@session.clicks_button
@session.click_button
end
it "should send default radio options" do
@ -214,7 +214,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"gender" => "F"})
@session.clicks_button
@session.click_button
end
it "should send correct data for rails style unchecked fields" do
@ -226,7 +226,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
@session.clicks_button
@session.click_button
end
it "should send correct data for rails style checked fields" do
@ -238,7 +238,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
@session.clicks_button
@session.click_button
end
it "should send default collection fields" do
@ -260,7 +260,7 @@ describe "clicks_button" do
@session.should_receive(:post).with("/login",
"options" => ["burger", "fries", "soda", "soda", "dessert"],
"response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]})
@session.clicks_button
@session.click_button
end
it "should not send default unchecked fields" do
@ -271,7 +271,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", {})
@session.clicks_button
@session.click_button
end
it "should send default textarea values" do
@ -282,7 +282,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:post).with("/posts", "post" => {"body" => "Post body here!"})
@session.clicks_button
@session.click_button
end
it "should send default selected option value from select" do
@ -296,7 +296,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "month" => "2")
@session.clicks_button
@session.click_button
end
it "should send default selected option inner html from select when no value attribute" do
@ -310,7 +310,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "month" => "February")
@session.clicks_button
@session.click_button
end
it "should send first select option value when no option selected" do
@ -324,7 +324,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "month" => "1")
@session.clicks_button
@session.click_button
end
it "should handle nested properties" do
@ -336,7 +336,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:post).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}})
@session.clicks_button
@session.click_button
end
it "should send default empty text field values" do
@ -347,7 +347,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"email" => ""})
@session.clicks_button
@session.click_button
end
it "should recognize button tags" do
@ -358,7 +358,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"email" => ""})
@session.clicks_button
@session.click_button
end
it "should recognize image button tags" do
@ -368,7 +368,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get)
@session.clicks_button
@session.click_button
end
it "should find buttons by their IDs" do
@ -378,7 +378,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get)
@session.clicks_button "my_button"
@session.click_button "my_button"
end
it "should find image buttons by their alt text" do
@ -388,7 +388,7 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get)
@session.clicks_button "Go"
@session.click_button "Go"
end
it "should recognize button tags by content" do
@ -399,6 +399,6 @@ describe "clicks_button" do
</form>
EOS
@session.should_receive(:get).with("/login", "user" => {"email" => ""})
@session.clicks_button "Login"
@session.click_button "Login"
end
end

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "clicks_link" do
describe "click_link" do
before do
@session = Webrat::TestSession.new
end
@ -10,7 +10,7 @@ describe "clicks_link" do
<a href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "Link text"
@session.click_link "Link text"
end
it "should click get links" do
@ -18,7 +18,7 @@ describe "clicks_link" do
<a href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "Link text", :method => :get
@session.click_link "Link text", :method => :get
end
it "should click delete links" do
@ -26,7 +26,7 @@ describe "clicks_link" do
<a href="/page">Link text</a>
EOS
@session.should_receive(:delete).with("/page", {})
@session.clicks_link "Link text", :method => :delete
@session.click_link "Link text", :method => :delete
end
@ -35,7 +35,7 @@ describe "clicks_link" do
<a href="/page">Link text</a>
EOS
@session.should_receive(:post).with("/page", {})
@session.clicks_link "Link text", :method => :post
@session.click_link "Link text", :method => :post
end
it "should click put links" do
@ -43,7 +43,15 @@ describe "clicks_link" do
<a href="/page">Link text</a>
EOS
@session.should_receive(:put).with("/page", {})
@session.clicks_link "Link text", :method => :put
@session.click_link "Link text", :method => :put
end
it "should click links by regexp" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.click_link /link [a-z]/i
end
it "should click rails javascript links with authenticity tokens" do
@ -62,7 +70,7 @@ describe "clicks_link" do
return false;">Posts</a>
EOS
@session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
@session.clicks_link "Posts"
@session.click_link "Posts"
end
it "should click rails javascript delete links" do
@ -81,7 +89,7 @@ describe "clicks_link" do
return false;">Delete</a>
EOS
@session.should_receive(:delete).with("/posts/1", {})
@session.clicks_link "Delete"
@session.click_link "Delete"
end
it "should click rails javascript post links" do
@ -95,7 +103,7 @@ describe "clicks_link" do
return false;">Posts</a>
EOS
@session.should_receive(:post).with("/posts", {})
@session.clicks_link "Posts"
@session.click_link "Posts"
end
it "should click rails javascript post links without javascript" do
@ -109,7 +117,7 @@ describe "clicks_link" do
return false;">Posts</a>
EOS
@session.should_receive(:get).with("/posts", {})
@session.clicks_link "Posts", :javascript => false
@session.click_link "Posts", :javascript => false
end
it "should click rails javascript put links" do
@ -128,7 +136,7 @@ describe "clicks_link" do
return false;">Put</a></h2>
EOS
@session.should_receive(:put).with("/posts", {})
@session.clicks_link "Put"
@session.click_link "Put"
end
it "should fail if the javascript link doesn't have a value for the _method input" do
@ -147,7 +155,7 @@ describe "clicks_link" do
EOS
lambda {
@session.clicks_link "Link"
@session.click_link "Link"
}.should raise_error
end
@ -156,7 +164,7 @@ describe "clicks_link" do
<a href="/page">Link text</a>
EOS
@session.response_code = 501
lambda { @session.clicks_link "Link text" }.should raise_error
lambda { @session.click_link "Link text" }.should raise_error
end
[200, 300, 400, 499].each do |status|
@ -165,7 +173,7 @@ describe "clicks_link" do
<a href="/page">Link text</a>
EOS
@session.response_code = status
lambda { @session.clicks_link "Link text" }.should_not raise_error
lambda { @session.click_link "Link text" }.should_not raise_error
end
end
@ -175,7 +183,7 @@ describe "clicks_link" do
EOS
lambda {
@session.clicks_link "Missing link"
@session.click_link "Missing link"
}.should raise_error
end
@ -184,7 +192,7 @@ describe "clicks_link" do
<a href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "LINK TEXT"
@session.click_link "LINK TEXT"
end
it "should match link substrings" do
@ -192,7 +200,7 @@ describe "clicks_link" do
<a href="/page">This is some cool link text, isn't it?</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "Link text"
@session.click_link "Link text"
end
it "should work with elements in the link" do
@ -200,7 +208,7 @@ describe "clicks_link" do
<a href="/page"><span>Link text</span></a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "Link text"
@session.click_link "Link text"
end
it "should match the first matching link" do
@ -209,7 +217,7 @@ describe "clicks_link" do
<a href="/page2">Link text</a>
EOS
@session.should_receive(:get).with("/page1", {})
@session.clicks_link "Link text"
@session.click_link "Link text"
end
it "should choose the shortest link text match" do
@ -219,7 +227,7 @@ describe "clicks_link" do
EOS
@session.should_receive(:get).with("/page2", {})
@session.clicks_link "Link"
@session.click_link "Link"
end
it "should treat non-breaking spaces as spaces" do
@ -228,7 +236,7 @@ describe "clicks_link" do
EOS
@session.should_receive(:get).with("/page1", {})
@session.clicks_link "This is a link"
@session.click_link "This is a link"
end
it "should click link within a selector" do
@ -240,7 +248,7 @@ describe "clicks_link" do
EOS
@session.should_receive(:get).with("/page2", {})
@session.clicks_link_within "#container", "Link"
@session.click_link_within "#container", "Link"
end
it "should not make request when link is local anchor" do
@ -249,7 +257,7 @@ describe "clicks_link" do
EOS
# Don't know why @session.should_receive(:get).never doesn't work here
@session.should_receive(:send).with('get_via_redirect', '#section-1', {}).never
@session.clicks_link "Jump to Section 1"
@session.click_link "Jump to Section 1"
end
it "should follow relative links" do
@ -258,7 +266,7 @@ describe "clicks_link" do
<a href="sub">Jump to sub page</a>
EOS
@session.should_receive(:get).with("/page/sub", {})
@session.clicks_link "Jump to sub page"
@session.click_link "Jump to sub page"
end
it "should follow fully qualified local links" do
@ -266,7 +274,7 @@ describe "clicks_link" do
<a href="http://www.example.com/page/sub">Jump to sub page</a>
EOS
@session.should_receive(:get).with("http://www.example.com/page/sub", {})
@session.clicks_link "Jump to sub page"
@session.click_link "Jump to sub page"
end
it "should follow query parameters" do
@ -275,6 +283,6 @@ describe "clicks_link" do
<a href="?foo=bar">Jump to foo bar</a>
EOS
@session.should_receive(:get).with("/page?foo=bar", {})
@session.clicks_link "Jump to foo bar"
@session.click_link "Jump to foo bar"
end
end

View File

@ -44,25 +44,25 @@ describe "field_labeled" do
end
describe "finding a text field" do
using_this_html <<-EOS
using_this_html <<-HTML
<form>
<label for="element_42">The Label</label>
<input type="text" id="element_42">
</form>
EOS
HTML
should_return_a Webrat::TextField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label"
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
end
describe "finding a text field" do
using_this_html <<-EOS
describe "finding a hidden field" do
using_this_html <<-HTML
<form>
<label for="element_42">The Label</label>
<input type="hidden" id="element_42">
</form>
EOS
HTML
should_return_a Webrat::HiddenField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label"
@ -70,12 +70,12 @@ describe "field_labeled" do
end
describe "finding a checkbox" do
using_this_html <<-EOS
using_this_html <<-HTML
<form>
<label for="element_42">The Label</label>
<input type="checkbox" id="element_42">
</form>
EOS
HTML
should_return_a Webrat::CheckboxField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label"
@ -83,12 +83,12 @@ describe "field_labeled" do
end
describe "finding a radio button" do
using_this_html <<-EOS
using_this_html <<-HTML
<form>
<label for="element_42">The Label</label>
<input type="radio" id="element_42">
</form>
EOS
HTML
should_return_a Webrat::RadioField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label"
@ -97,12 +97,12 @@ describe "field_labeled" do
describe "finding a text area" do
using_this_html <<-EOS
using_this_html <<-HTML
<form>
<label for="element_42">The Label</label>
<textarea id="element_42"></textarea>
</form>
EOS
HTML
should_return_a Webrat::TextareaField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label"

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "fills_in" do
describe "fill_in" do
before do
@session = Webrat::TestSession.new
end
@ -14,8 +14,8 @@ describe "fills_in" do
</form>
EOS
@session.should_receive(:post).with("/login", "user" => {"text" => "filling text area"})
@session.fills_in "User Text", :with => "filling text area"
@session.clicks_button
@session.fill_in "User Text", :with => "filling text area"
@session.click_button
end
it "should work with password fields" do
@ -26,8 +26,8 @@ describe "fills_in" do
</form>
EOS
@session.should_receive(:post).with("/login", "user" => {"text" => "pass"})
@session.fills_in "user_text", :with => "pass"
@session.clicks_button
@session.fill_in "user_text", :with => "pass"
@session.click_button
end
it "should fail if input not found" do
@ -36,7 +36,7 @@ describe "fills_in" do
</form>
EOS
lambda { @session.fills_in "Email", :with => "foo@example.com" }.should raise_error
lambda { @session.fill_in "Email", :with => "foo@example.com" }.should raise_error
end
it "should fail if input is disabled" do
@ -48,7 +48,7 @@ describe "fills_in" do
</form>
EOS
lambda { @session.fills_in "Email", :with => "foo@example.com" }.should raise_error
lambda { @session.fill_in "Email", :with => "foo@example.com" }.should raise_error
end
it "should allow overriding default form values" do
@ -60,8 +60,8 @@ describe "fills_in" do
</form>
EOS
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in "user[email]", :with => "foo@example.com"
@session.clicks_button
@session.fill_in "user[email]", :with => "foo@example.com"
@session.click_button
end
it "should choose the shortest label match" do
@ -76,8 +76,8 @@ describe "fills_in" do
EOS
@session.should_receive(:post).with("/login", "user" => {"mail1" => "", "mail2" => "value"})
@session.fills_in "Some", :with => "value"
@session.clicks_button
@session.fill_in "Some", :with => "value"
@session.click_button
end
it "should choose the first label match if closest is a tie" do
@ -92,8 +92,8 @@ describe "fills_in" do
EOS
@session.should_receive(:post).with("/login", "user" => {"mail1" => "value", "mail2" => ""})
@session.fills_in "Some mail", :with => "value"
@session.clicks_button
@session.fill_in "Some mail", :with => "value"
@session.click_button
end
it "should anchor label matches to start of label" do
@ -104,7 +104,7 @@ describe "fills_in" do
</form>
EOS
lambda { @session.fills_in "mail", :with => "value" }.should raise_error
lambda { @session.fill_in "mail", :with => "value" }.should raise_error
end
it "should anchor label matches to word boundaries" do
@ -115,7 +115,7 @@ describe "fills_in" do
</form>
EOS
lambda { @session.fills_in "Email", :with => "value" }.should raise_error
lambda { @session.fill_in "Email", :with => "value" }.should raise_error
end
it "should work with inputs nested in labels" do
@ -129,8 +129,8 @@ describe "fills_in" do
</form>
EOS
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in "Email", :with => "foo@example.com"
@session.clicks_button
@session.fill_in "Email", :with => "foo@example.com"
@session.click_button
end
it "should work with full input names" do
@ -141,8 +141,8 @@ describe "fills_in" do
</form>
EOS
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in "user[email]", :with => "foo@example.com"
@session.clicks_button
@session.fill_in "user[email]", :with => "foo@example.com"
@session.click_button
end
it "should work if the input type is not set" do
@ -153,8 +153,8 @@ describe "fills_in" do
</form>
EOS
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in "user[email]", :with => "foo@example.com"
@session.clicks_button
@session.fill_in "user[email]", :with => "foo@example.com"
@session.click_button
end
it "should work with symbols" do
@ -166,8 +166,8 @@ describe "fills_in" do
</form>
EOS
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in :email, :with => "foo@example.com"
@session.clicks_button
@session.fill_in :email, :with => "foo@example.com"
@session.click_button
end
it "should escape field values" do
@ -179,7 +179,7 @@ describe "fills_in" do
</form>
EOS
@session.should_receive(:post).with("/users", "user" => {"phone" => "+1 22 33"})
@session.fills_in 'Phone', :with => "+1 22 33"
@session.clicks_button
@session.fill_in 'Phone', :with => "+1 22 33"
@session.click_button
end
end

178
spec/api/matchers_spec.rb Normal file
View File

@ -0,0 +1,178 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe Webrat::Matchers do
include Webrat::Matchers
include Webrat::HaveTagMatcher
before(:each) do
@body = <<-EOF
<div id='main'>
<div class='inner'>hello, world!</div>
<ul>
<li>First</li>
<li>Second</li>
</ul>
</div>
EOF
end
describe "#have_xpath" do
it "should be able to match an XPATH" do
@body.should have_xpath("//div")
end
it "should not match a XPATH that does not exist" do
@body.should_not have_xpath("//p")
end
it "should be able to loop over all the matched elements" do
@body.should have_xpath("//div") { |node| node.first.name.should == "div" }
end
it "should not match of any of the matchers in the block fail" do
lambda {
@body.should have_xpath("//div") { |node| node.first.name.should == "p" }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should be able to use #have_xpath in the block" do
@body.should have_xpath("//div[@id='main']") { |node| node.should have_xpath("./div[@class='inner']") }
end
it "should convert absolute paths to relative in the block" do
@body.should have_xpath("//div[@id='main']") { |node| node.should have_xpath("//div[@class='inner']") }
end
it "should not match any parent tags in the block" do
lambda {
@body.should have_xpath("//div[@class='inner']") { |node| node.should have_xpath("//div[@id='main']") }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
end
describe "#have_selector" do
it "should be able to match a CSS selector" do
@body.should have_selector("div")
end
it "should not match a CSS selector that does not exist" do
@body.should_not have_selector("p")
end
it "should be able to loop over all the matched elements" do
@body.should have_selector("div") { |node| node.first.name.should == "div" }
end
it "should not match of any of the matchers in the block fail" do
lambda {
@body.should have_selector("div") { |node| node.first.name.should == "p" }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should be able to use #have_selector in the block" do
@body.should have_selector("#main") { |node| node.should have_selector(".inner") }
end
it "should not match any parent tags in the block" do
lambda {
@body.should have_selector(".inner") { |node| node.should have_selector("#main") }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
end
describe "#have_tag" do
it "should be able to match a tag" do
@body.should have_tag("div")
end
it "should not match the tag when it should not match" do
@body.should_not have_tag("p")
end
it "should be able to specify the content of the tag" do
@body.should have_tag("div", :content => "hello, world!")
end
it "should be able to specify the attributes of the tag" do
@body.should have_tag("div", :class => "inner")
end
it "should be able to loop over all the matched elements" do
@body.should have_tag("div") { |node| node.first.name.should == "div" }
end
it "should not match of any of the matchers in the block fail" do
lambda {
@body.should have_tag("div") { |node| node.first.name.should == "p" }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should be able to use #have_tag in the block" do
@body.should have_tag("div", :id => "main") { |node| node.should have_tag("div", :class => "inner") }
end
it "should not match any parent tags in the block" do
lambda {
@body.should have_tag("div", :class => "inner") { |node| node.should have_tag("div", :id => "main") }
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
end
it "should work with items that have multiple child nodes" do
@body.should have_tag("ul") { |n|
n.should have_tag("li", :content => "First")
n.should have_tag("li", :content => "Second")
}
end
end
describe Webrat::Matchers::HasContent do
include Webrat::Matchers
before(:each) do
@body = <<-EOF
<div id='main'>
<div class='inner'>hello, world!</div>
</div>
EOF
end
describe "#matches?" do
it "should call element#contains? when the argument is a string" do
@body.should contain("hello, world!")
end
it "should call element#matches? when the argument is a regular expression" do
@body.should contain(/hello, world/)
end
end
describe "#failure_message" do
it "should include the content string" do
hc = Webrat::Matchers::HasContent.new("hello, world!")
hc.matches?(@body)
hc.failure_message.should include("\"hello, world!\"")
end
it "should include the content regular expresson" do
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
hc.matches?(@body)
hc.failure_message.should include("/hello,\\sworld!/")
end
it "should include the element's inner content" do
hc = Webrat::Matchers::HasContent.new(/hello,\sworld!/)
hc.matches?(@body)
hc.failure_message.should include("hello, world!")
end
end
end
end

View File

@ -9,7 +9,7 @@ describe "reloads" do
it "should reload the page with http referer" do
@session.should_receive(:get).with("/", {})
@session.should_receive(:get).with("/", {}, {"HTTP_REFERER"=>"/"})
@session.visits("/")
@session.visit("/")
@session.reloads
end
end

View File

@ -57,7 +57,7 @@ describe "selects" do
EOS
@session.should_receive(:post).with("/login", "month" => "1")
@session.selects "January", :from => "month"
@session.clicks_button
@session.click_button
end
it "should send values with HTML encoded ampersands" do
@ -69,7 +69,7 @@ describe "selects" do
EOS
@session.should_receive(:post).with("/login", "encoded" => "A & B")
@session.selects "Encoded", :from => "encoded"
@session.clicks_button
@session.click_button
end
it "should work with empty select lists" do
@ -80,7 +80,7 @@ describe "selects" do
</form>
EOS
@session.should_receive(:post).with("/login", 'month' => '')
@session.clicks_button
@session.click_button
end
it "should work without specifying the field name or label" do
@ -92,7 +92,7 @@ describe "selects" do
EOS
@session.should_receive(:post).with("/login", "month" => "1")
@session.selects "January"
@session.clicks_button
@session.click_button
end
it "should send value from option in list specified by name" do
@ -105,7 +105,7 @@ describe "selects" do
EOS
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "end_month"
@session.clicks_button
@session.click_button
end
it "should send value from option in list specified by label" do
@ -120,7 +120,7 @@ describe "selects" do
EOS
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "End Month"
@session.clicks_button
@session.click_button
end
it "should use option text if no value" do
@ -132,7 +132,7 @@ describe "selects" do
EOS
@session.should_receive(:post).with("/login", "month" => "January")
@session.selects "January", :from => "month"
@session.clicks_button
@session.click_button
end
it "should find option by regexp" do
@ -144,7 +144,7 @@ describe "selects" do
EOS
@session.should_receive(:post).with("/login", "month" => "January")
@session.selects(/jan/i)
@session.clicks_button
@session.click_button
end
it "should fail if no option matching the regexp exists" do
@ -172,6 +172,6 @@ describe "selects" do
EOS
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects(/jan/i, :from => "End Month")
@session.clicks_button
@session.click_button
end
end

View File

@ -1,73 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "should_not_see" do
before do
@session = Webrat::TestSession.new
end
it "should fail if the string is in the HTML" do
@session.response_body = <<-EOS
<a href="/page2">Link</a>
EOS
lambda {
@session.should_not_see "Link"
}.should raise_error
end
it "should fail if the regexp is in the HTML" do
@session.response_body = <<-EOS
<a href="/page2">Link</a>
EOS
lambda {
@session.should_not_see /Li(n)[ck]/
}.should raise_error
end
it "should fail if the string is in the HTML scope" do
@session.response_body = <<-EOS
<div id="first">
<a href="/page2">Link</a>
</div>
<div id="second">
</div>
EOS
lambda {
@session.within "#first" do |scope|
scope.should_not_see "Link"
end
}.should raise_error
end
it "should pass if the string is not in the HTML scope" do
@session.response_body = <<-EOS
<div id="first">
<a href="/page2">Link</a>
</div>
<div id="second">
</div>
EOS
@session.within "#second" do |scope|
scope.should_not_see "Link"
end
end
it "should pass if the string is not in the HTML" do
@session.response_body = <<-EOS
<a href="/page2">Link</a>
EOS
@session.should_not_see "Missing"
end
it "should pass if the regexp is not in the HTML" do
@session.response_body = <<-EOS
<a href="/page2">Different</a>
EOS
@session.should_not_see /Li(n)[ck]/
end
end

View File

@ -1,73 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "should_see" do
before do
@session = Webrat::TestSession.new
end
it "should pass if the string is in the HTML" do
@session.response_body = <<-EOS
<a href="/page2">Link</a>
EOS
@session.should_see "Link"
end
it "should pass if the regexp is in the HTML" do
@session.response_body = <<-EOS
<a href="/page2">Link</a>
EOS
@session.should_see /Li(n)[ck]/
end
it "should pass if the string is in the HTML scope" do
@session.response_body = <<-EOS
<div id="first">
<a href="/page2">Link</a>
</div>
<div id="second">
</div>
EOS
@session.within "#first" do |scope|
scope.should_see "Link"
end
end
it "should fail if the string is not in the HTML scope" do
@session.response_body = <<-EOS
<div id="first">
<a href="/page2">Link</a>
</div>
<div id="second">
</div>
EOS
lambda {
@session.within "#second" do |scope|
scope.should_see "Link"
end
}.should raise_error
end
it "should fail if the string is not in the HTML" do
@session.response_body = <<-EOS
<a href="/page2">Link</a>
EOS
lambda {
@session.should_see "Missing"
}.should raise_error
end
it "should fail if the regexp is not in the HTML" do
@session.response_body = <<-EOS
<a href="/page2">Different</a>
EOS
lambda {
@session.should_see /Li(n)[ck]/
}.should raise_error
end
end

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "visits" do
describe "visit" do
before do
@session = Webrat::TestSession.new
@session.response_body = "Hello world"
@ -8,27 +8,27 @@ describe "visits" do
it "should use get" do
@session.should_receive(:get).with("/", {})
@session.visits("/")
@session.visit("/")
end
it "should assert valid response" do
@session.response_code = 501
lambda { @session.visits("/") }.should raise_error
lambda { @session.visit("/") }.should raise_error
end
[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
@session.response_code = status
lambda { @session.visits("/") }.should_not raise_error
lambda { @session.visit("/") }.should_not raise_error
end
end
it "should require a visit before manipulating page" do
lambda { @session.fills_in "foo", :with => "blah" }.should raise_error
lambda { @session.fill_in "foo", :with => "blah" }.should raise_error
end
end
describe "visits with referer" do
describe "visit with referer" do
before do
@session = Webrat::TestSession.new
@session.instance_variable_set(:@current_url, "/old_url")
@ -37,7 +37,7 @@ describe "visits with referer" do
it "should use get with referer header" do
@session.should_receive(:get).with("/", {}, {"HTTP_REFERER" => "/old_url"})
@session.visits("/")
@session.visit("/")
end
end

View File

@ -5,6 +5,24 @@ describe "within" do
@session = Webrat::TestSession.new
end
it "should work when nested" do
@session.response_body = <<-EOS
<div>
<a href="/page1">Link</a>
</div>
<div id="container">
<div><a href="/page2">Link</a></div>
</div>
EOS
@session.should_receive(:get).with("/page2", {})
@session.within "#container" do
@session.within "div" do
@session.click_link "Link"
end
end
end
it "should click links within a scope" do
@session.response_body = <<-EOS
<a href="/page1">Link</a>
@ -14,8 +32,8 @@ describe "within" do
EOS
@session.should_receive(:get).with("/page2", {})
@session.within "#container" do |scope|
scope.clicks_link "Link"
@session.within "#container" do
@session.click_link "Link"
end
end
@ -32,9 +50,9 @@ describe "within" do
EOS
@session.should_receive(:get).with("/form2", "email" => "test@example.com")
@session.within "#form2" do |scope|
scope.fill_in "Email", :with => "test@example.com"
scope.clicks_button
@session.within "#form2" do
@session.fill_in "Email", :with => "test@example.com"
@session.click_button
end
end
@ -47,9 +65,9 @@ describe "within" do
</form>
EOS
@session.within "#form2" do |scope|
@session.within "#form2" do
lambda {
scope.clicks_button
@session.click_button
}.should raise_error
end
end

View File

@ -7,6 +7,10 @@ module Webrat
File.expand_path(File.join(".", "public"))
end
def response
@response ||= Object.new
end
def response_code
@response_code || 200
end

View File

@ -1,6 +1,5 @@
require "rubygems"
require "spec"
require "spec/interop/test"
# gem install redgreen for colored test output
begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end

View File

@ -3,12 +3,12 @@ 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, (Hpricot("<input type='checkbox' checked='checked'>")/'input').first)
checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("<input type='checkbox' checked='checked'>")/'input').first)
checkbox.should be_checked
end
it "should say it is not checked if it is not" do
checkbox = CheckboxField.new(nil, (Hpricot("<input type='checkbox'>")/'input').first)
checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("<input type='checkbox'>")/'input').first)
checkbox.should_not be_checked
end
end

View File

@ -10,11 +10,15 @@ describe Webrat::Session do
it "should expose the current_dom" do
session = Webrat::Session.new
def session.response
Object.new
end
def session.response_body
"<html></html>"
end
session.current_dom.should be_an_instance_of(Hpricot::Doc)
session.current_dom.should be_an_instance_of(Nokogiri::HTML::Document)
end
it "should open the page in the browser" do
@ -66,4 +70,19 @@ describe Webrat::Session do
end
end
describe "#request_page" do
before(:each) do
@session = Webrat::Session.new
end
it "should raise an error if the request is not a success" do
@session.stub!(:get)
@session.stub!(:response_body).and_return("Exception caught")
@session.stub!(:response_code).and_return(500)
@session.stub!(:formatted_error).and_return("application error")
@session.stub!(:save_and_open_page)
lambda { @session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError)
end
end
end

View File

@ -4,7 +4,7 @@ require File.expand_path(File.dirname(__FILE__) + "/helper")
describe Webrat::Session do
it "should not pass empty params if data is and empty hash" do
session = Webrat::Session.new
session = Webrat::MerbSession.new
response = OpenStruct.new
response.status = 200
session.should_receive(:request).with('url', {:params=> nil, :method=>"GET", :headers=>nil}).and_return(response)
@ -13,7 +13,7 @@ describe Webrat::Session do
%w{post put delete}.each do |request_method|
it "should call do request with method #{request_method.upcase} for a #{request_method} call" do
session = Webrat::Session.new
session = Webrat::MerbSession.new
response = OpenStruct.new
response.status = 200
@ -25,7 +25,7 @@ describe Webrat::Session do
context "a session with a response" do
setup do
@session = Webrat::Session.new
@session = Webrat::MerbSession.new
@response = OpenStruct.new
@response.status = 200
@response.body = 'test response'

View File

@ -0,0 +1,75 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "Nokogiri Extension" do
include Webrat::Matchers
def fail
raise_error(Spec::Expectations::ExpectationNotMetError)
end
before(:each) do
@text_and_password = <<-HTML
<div>
<input type="text"/>
<input type="password"/>
<span type="text"/>
</div>
HTML
@text_only = <<-HTML
<div>
<input type="text" disabled="disabled" />
</div>
HTML
@password_only = <<-HTML
<div>
<input type="password"/>
<div>
HTML
end
describe ":text" do
it "passes have_selector(:text) if a node with type=text exists" do
@text_and_password.should have_selector(":text")
end
it "passes not have_selector(:text) if no node with text=text exists" do
@password_only.should_not have_selector(":text")
end
it "fails have_selector(:text) if no node with type=text exists" do
lambda { @password_only.should have_selector(":text") }.should fail
end
it "fails not have_selector(:text) if a node with type=text exists" do
lambda { @text_only.should_not have_selector(":text") }.should fail
end
it "works together with other selectors" do
@text_and_password.should have_selector("input:text[type*='te']")
end
end
describe ":password" do
it "passes have_selector(:password) if a node with type=password exists" do
@text_and_password.should have_selector(":password")
end
it "passes not have_selector(:text) if no node with text=text exists" do
@text_only.should_not have_selector(":password")
end
it "fails have_selector(:password) if no node with type=password exists" do
lambda { @text_only.should have_selector(":password") }.should fail
end
it "fails not have_selector(:password) if a node with type=password exists" do
lambda { @password_only.should_not have_selector(":password") }.should fail
end
it "works together with other selectors" do
@text_and_password.should have_selector("input:password[type*='pa']")
end
end
end

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/helper')
describe "attaches_file" do
describe "attach_file" do
before do
@session = Webrat::TestSession.new
@ -14,7 +14,7 @@ describe "attaches_file" do
<form method="post" action="/widgets">
</form>
EOS
lambda { @session.attaches_file("Doc", "/some/path") }.should raise_error
lambda { @session.attach_file("Doc", "/some/path") }.should raise_error
end
it "should submit empty strings for blank file fields" do
@ -25,7 +25,7 @@ describe "attaches_file" do
</form>
EOS
@session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } })
@session.clicks_button
@session.click_button
end
it "should submit the attached file" do
@ -37,8 +37,8 @@ describe "attaches_file" do
</form>
EOS
@session.should_receive(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } })
@session.attaches_file "Document", @filename
@session.clicks_button
@session.attach_file "Document", @filename
@session.click_button
end
it "should support collections" do
@ -52,9 +52,9 @@ describe "attaches_file" do
</form>
EOS
@session.should_receive(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
@session.attaches_file "Document", @filename
@session.attaches_file "Spreadsheet", @filename
@session.clicks_button
@session.attach_file "Document", @filename
@session.attach_file "Spreadsheet", @filename
@session.click_button
end
it "should allow the content type to be specified" do
@ -66,7 +66,7 @@ describe "attaches_file" do
</form>
EOS
ActionController::TestUploadedFile.should_receive(:new).with(@filename, "image/png").any_number_of_times
@session.attaches_file "Picture", @filename, "image/png"
@session.clicks_button
@session.attach_file "Picture", @filename, "image/png"
@session.click_button
end
end

View File

@ -19,31 +19,31 @@ describe Webrat::RailsSession do
Webrat::RailsSession.new(integration_session).response_code.should == 42
end
it "should delegate get to get_via_redirect on the integration session" do
it "should delegate get to request_via_redirect on the integration session" do
integration_session = mock("integration session")
rails_session = Webrat::RailsSession.new(integration_session)
integration_session.should_receive(:get_via_redirect).with("url", "data", "headers")
integration_session.should_receive(:request_via_redirect).with(:get, "url", "data", "headers")
rails_session.get("url", "data", "headers")
end
it "should delegate post to post_via_redirect on the integration session" do
it "should delegate post to request_via_redirect on the integration session" do
integration_session = mock("integration session")
rails_session = Webrat::RailsSession.new(integration_session)
integration_session.should_receive(:post_via_redirect).with("url", "data", "headers")
integration_session.should_receive(:request_via_redirect).with(:post, "url", "data", "headers")
rails_session.post("url", "data", "headers")
end
it "should delegate put to put_via_redirect on the integration session" do
it "should delegate put to request_via_redirect on the integration session" do
integration_session = mock("integration session")
rails_session = Webrat::RailsSession.new(integration_session)
integration_session.should_receive(:put_via_redirect).with("url", "data", "headers")
integration_session.should_receive(:request_via_redirect).with(:put, "url", "data", "headers")
rails_session.put("url", "data", "headers")
end
it "should delegate delete to delete_via_redirect on the integration session" do
it "should delegate delete to request_via_redirect on the integration session" do
integration_session = mock("integration session")
rails_session = Webrat::RailsSession.new(integration_session)
integration_session.should_receive(:delete_via_redirect).with("url", "data", "headers")
integration_session.should_receive(:request_via_redirect).with(:delete, "url", "data", "headers")
rails_session.delete("url", "data", "headers")
end
@ -51,14 +51,14 @@ describe Webrat::RailsSession do
it "should just pass on the path" do
integration_session = mock("integration session", :https! => nil)
rails_session = Webrat::RailsSession.new(integration_session)
integration_session.should_receive(:get_via_redirect).with("/url", "data", "headers")
integration_session.should_receive(:request_via_redirect).with(:get, "/url", "data", "headers")
rails_session.get("http://www.example.com/url", "data", "headers")
end
end
context "the URL is https://" do
it "should call #https! with true before the request" do
integration_session = mock("integration session", :get_via_redirect => nil)
integration_session = mock("integration session", :request_via_redirect => nil)
rails_session = Webrat::RailsSession.new(integration_session)
integration_session.should_receive(:https!).with(true)
rails_session.get("https://www.example.com/url", "data", "headers")
@ -67,7 +67,7 @@ describe Webrat::RailsSession do
context "the URL is http://" do
it "should call #https! with true before the request" do
integration_session = mock("integration session", :get_via_redirect => nil)
integration_session = mock("integration session", :request_via_redirect => nil)
rails_session = Webrat::RailsSession.new(integration_session)
integration_session.should_receive(:https!).with(false)
rails_session.get("http://www.example.com/url", "data", "headers")

View File

@ -7,7 +7,7 @@
# EOS
# @page.stub!(:url).and_return("/current")
# @session.should_receive(:get).with("/current", {})
# @session.clicks_button
# @session.click_button
# end
#
# it "should follow fully qualified secure local links" do
@ -16,5 +16,5 @@
# EOS
# @session.should_receive(:https!).with(true)
# @session.should_receive(:get).with("/page/sub", {})
# @session.clicks_link "Jump to sub page"
# @session.click_link "Jump to sub page"
# end

View File

@ -1,35 +0,0 @@
Gem::Specification.new do |s|
s.name = %q{webrat}
s.version = "0.2.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Bryan Helmkamp", "Seth Fitzsimmons"]
s.date = %q{2008-10-13}
s.description = %q{When comparing Webrat with an in-browser testing solution like Watir or Selenium, the primary consideration should be how much JavaScript the application uses. In-browser testing is currently the only way to test JS, and that may make it a requirement for your project. If JavaScript is not central to your application, Webrat is a simpler, effective solution that will let you run your tests much faster and more frequently. Initial development was sponsored by [EastMedia](http://www.eastmedia.com). Synopsis --------}
s.email = ["bryan@brynary.com", "seth@mojodna.net"]
s.extra_rdoc_files = ["History.txt", "MIT-LICENSE.txt", "Manifest.txt", "README.txt", "TODO.txt"]
s.files = ["History.txt", "MIT-LICENSE.txt", "Manifest.txt", "README.txt", "Rakefile", "TODO.txt", "init.rb", "install.rb", "lib/webrat.rb", "lib/webrat/core.rb", "lib/webrat/core/assertions.rb", "lib/webrat/core/field.rb", "lib/webrat/core/flunk.rb", "lib/webrat/core/form.rb", "lib/webrat/core/label.rb", "lib/webrat/core/link.rb", "lib/webrat/core/logging.rb", "lib/webrat/core/scope.rb", "lib/webrat/core/select_option.rb", "lib/webrat/core/session.rb", "lib/webrat/mechanize.rb", "lib/webrat/mechanize/mechanize_session.rb", "lib/webrat/rails.rb", "lib/webrat/rails/rails_session.rb", "lib/webrat/rails/redirect_actions.rb", "lib/webrat/rails/session.rb", "lib/webrat/selenium.rb", "lib/webrat/selenium/selenium_session.rb", "mechanize_spike.rb", "selenium_spike.rb", "spec/api/attaches_file_spec.rb", "spec/api/checks_spec.rb", "spec/api/chooses_spec.rb", "spec/api/clicks_button_spec.rb", "spec/api/clicks_link_spec.rb", "spec/api/fills_in_spec.rb", "spec/api/reloads_spec.rb", "spec/api/save_and_open_spec.rb", "spec/api/selects_spec.rb", "spec/api/should_not_see_spec.rb", "spec/api/should_see_spec.rb", "spec/api/visits_spec.rb", "spec/api/within_spec.rb", "spec/fakes/test_session.rb", "spec/integration/rails_spec.rb", "spec/rcov.opts", "spec/spec.opts", "spec/spec_helper.rb", "spec/webrat/core/logging_spec.rb", "spec/webrat/core/session_spec.rb", "spec/webrat/rails/rails_session_spec.rb", "webrat.gemspec"]
s.has_rdoc = true
s.homepage = %q{- [Code on GitHub](http://github.com/brynary/webrat)}
s.rdoc_options = ["--main", "README.txt"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{webrat}
s.rubygems_version = %q{1.2.0}
s.summary = %q{Ruby Acceptance Testing for Web applications}
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 2
if current_version >= 3 then
s.add_runtime_dependency(%q<hpricot>, [">= 0.6"])
s.add_development_dependency(%q<hoe>, [">= 1.7.0"])
else
s.add_dependency(%q<hpricot>, [">= 0.6"])
s.add_dependency(%q<hoe>, [">= 1.7.0"])
end
else
s.add_dependency(%q<hpricot>, [">= 0.6"])
s.add_dependency(%q<hoe>, [">= 1.7.0"])
end
end