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

This commit is contained in:
Bryan Helmkamp 2008-06-20 11:09:56 -04:00
commit 856b6cb17e
38 changed files with 749 additions and 373 deletions

View File

@ -6,11 +6,14 @@
* Support button elements (Patch from Nick Sieger) * Support button elements (Patch from Nick Sieger)
* Support matching select options by regexp (Patch from Kyle Hargraves) * Support matching select options by regexp (Patch from Kyle Hargraves)
* Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves) * Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves)
* Support links to fully qualified URLs starting with http:// or https:// (Luke Melia)
* save_and_open_page rewrites css and image references to provide a friendlier debugging experience (Luke Melia)
* Bug fixes * Bug fixes
* Fix bug with empty select list option (Patch from Kyle Hargraves) * Fix bug with empty select list option (Patch from Kyle Hargraves)
* Fix regression of not sending default values in password fields * Fix regression of not sending default values in password fields
* Don't explode if encountering inputs with no type attribute (assume text)
== 0.2.0 / 2008-04-04 == 0.2.0 / 2008-04-04
@ -27,7 +30,7 @@
* Added reloads method to reload the page (Patch from Kamal Fariz Mahyuddi) * Added reloads method to reload the page (Patch from Kamal Fariz Mahyuddi)
* Prevent making a request if clicking on local anchor link (Patch from Kamal Fariz Mahyuddi) * Prevent making a request if clicking on local anchor link (Patch from Kamal Fariz Mahyuddi)
* Added clicks_link_within(selector, link_text), allowing restricting link search * Added clicks_link_within(selector, link_text), allowing restricting link search
to within a given css selector (Path from Luke Melia) to within a given css selector (Patch from Luke Melia)
* Allow specifying the input name/label when doing a select (Patch from David Chelimsky) * Allow specifying the input name/label when doing a select (Patch from David Chelimsky)
* Raise a specific exception if the developer tries to manipulate form elements before loading a page (Patch from James Deville) * Raise a specific exception if the developer tries to manipulate form elements before loading a page (Patch from James Deville)
* Add support for alternate POST, PUT and DELETE link clicking (Patch from Kyle Hargraves) * Add support for alternate POST, PUT and DELETE link clicking (Patch from Kyle Hargraves)

View File

@ -1,25 +1,3 @@
.git/HEAD
.git/config
.git/description
.git/hooks/applypatch-msg
.git/hooks/commit-msg
.git/hooks/post-commit
.git/hooks/post-receive
.git/hooks/post-update
.git/hooks/pre-applypatch
.git/hooks/pre-commit
.git/hooks/pre-rebase
.git/hooks/update
.git/index
.git/info/exclude
.git/logs/HEAD
.git/logs/refs/heads/master
.git/logs/refs/remotes/origin/master
.git/objects/pack/pack-3243faf8e8f6f0e226adf39a2c2e8c19c18da6c4.idx
.git/objects/pack/pack-3243faf8e8f6f0e226adf39a2c2e8c19c18da6c4.pack
.git/refs/heads/master
.git/refs/remotes/origin/HEAD
.git/refs/remotes/origin/master
.gitignore .gitignore
History.txt History.txt
MIT-LICENSE.txt MIT-LICENSE.txt
@ -27,6 +5,16 @@ Manifest.txt
README.txt README.txt
Rakefile Rakefile
TODO.txt TODO.txt
coverage/index.html
coverage/lib-webrat-field_rb.html
coverage/lib-webrat-form_rb.html
coverage/lib-webrat-label_rb.html
coverage/lib-webrat-link_rb.html
coverage/lib-webrat-logging_rb.html
coverage/lib-webrat-page_rb.html
coverage/lib-webrat-redirect_actions_rb.html
coverage/lib-webrat-select_option_rb.html
coverage/lib-webrat_rb.html
init.rb init.rb
install.rb install.rb
lib/webrat.rb lib/webrat.rb
@ -38,12 +26,16 @@ lib/webrat/logging.rb
lib/webrat/page.rb lib/webrat/page.rb
lib/webrat/redirect_actions.rb lib/webrat/redirect_actions.rb
lib/webrat/select_option.rb lib/webrat/select_option.rb
test/checks_test.rb spec/attaches_file_spec.rb
test/chooses_test.rb spec/checks_spec.rb
test/clicks_button_test.rb spec/chooses_spec.rb
test/clicks_link_test.rb spec/clicks_button_spec.rb
test/fills_in_test.rb spec/clicks_link_spec.rb
test/helper.rb spec/fills_in_spec.rb
test/reloads_test.rb spec/rcov.opts
test/selects_test.rb spec/reloads_spec.rb
test/visits_test.rb spec/save_and_open_page_spec.rb
spec/selects_spec.rb
spec/spec.opts
spec/spec_helper.rb
spec/visits_spec.rb

View File

@ -56,7 +56,7 @@ end
require 'spec/rake/verify_rcov' require 'spec/rake/verify_rcov'
RCov::VerifyTask.new(:verify_rcov => :rcov) do |t| RCov::VerifyTask.new(:verify_rcov => :rcov) do |t|
t.threshold = 95.5 # Make sure you have rcov 0.7 or higher! t.threshold = 96.9 # Make sure you have rcov 0.7 or higher!
end end
remove_task "default" remove_task "default"

View File

@ -1,47 +1,9 @@
Dir[File.join(File.dirname(__FILE__), "webrat", "*.rb")].each do |file|
require File.expand_path(file)
end
module Webrat module Webrat
VERSION = '0.2.1' VERSION = '0.2.1'
end end
module ActionController require "rubygems"
module Integration require "active_support"
class Session
unless instance_methods.include?("put_via_redirect")
include Webrat::RedirectActions
end
def current_page
@current_page ||= Webrat::Page.new(self)
end
def current_page=(new_page)
@current_page = new_page
end
# Issues a GET request for a page, follows any redirects, and verifies the final page
# load was successful.
#
# Example:
# visits "/"
def visits(*args)
@current_page = Webrat::Page.new(self, *args)
end
def save_and_open_page
current_page.save_and_open
end
[:reloads, :fills_in, :clicks_button, :selects, :attaches_file, :chooses, :checks, :unchecks, :clicks_link, :clicks_link_within, :clicks_put_link, :clicks_get_link, :clicks_post_link, :clicks_delete_link].each do |method_name|
define_method(method_name) do |*args|
current_page.send(method_name, *args)
end
end
end
end
end
require File.dirname(__FILE__) + "/webrat/core"
require File.dirname(__FILE__) + "/webrat/rails" if defined?(RAILS_ENV)

3
lib/webrat/core.rb Normal file
View File

@ -0,0 +1,3 @@
Dir[File.join(File.dirname(__FILE__), "core", "*.rb")].each do |file|
require File.expand_path(file)
end

View File

@ -6,7 +6,7 @@ module Webrat
if %w[submit image].include?(element["type"]) if %w[submit image].include?(element["type"])
field_class = "button" field_class = "button"
else else
field_class = element["type"] field_class = element["type"] || "text"
end end
else else
field_class = element.name field_class = element.name
@ -93,6 +93,8 @@ module Webrat
if defined?(CGIMethods) if defined?(CGIMethods)
CGIMethods CGIMethods
else else
require "action_controller"
require "action_controller/integration"
ActionController::AbstractRequest ActionController::AbstractRequest
end end
end end

View File

@ -9,8 +9,8 @@ module Webrat
def click(method = nil) def click(method = nil)
method ||= http_method method ||= http_method
return if href =~ /^#/ && method == :get return if href =~ /^#/ && method == :get
Page.new(@page.session, absolute_href, method, authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}) Page.new(@page.session, absolute_href, method, data)
end end
def matches_text?(link_text) def matches_text?(link_text)
@ -22,13 +22,19 @@ module Webrat
end end
protected protected
def data
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
end
def href def href
@element["href"] @element["href"]
end end
def absolute_href def absolute_href
if href =~ /^\?/ if href =~ %r{^https?://www.example.com(/.*)}
$LAST_MATCH_INFO.captures.first
elsif href =~ /^\?/
"#{@page.url}#{href}" "#{@page.url}#{href}"
elsif href !~ /^\// elsif href !~ /^\//
"#{@page.url}/#{href}" "#{@page.url}/#{href}"

View File

@ -1,3 +1,4 @@
require "rubygems"
require "hpricot" require "hpricot"
require "English" require "English"
@ -99,19 +100,25 @@ module Webrat
field.set(path) field.set(path)
end end
# Saves the currently loaded page out to RAILS_ROOT/tmp/ and opens it in the default # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
# web browser if on OS X. Useful for debugging. # web browser if on OS X. Useful for debugging.
# #
# Example: # Example:
# save_and_open # save_and_open
def save_and_open def save_and_open
return unless File.exist?(RAILS_ROOT + "/tmp") return unless File.exist?(session.saved_page_dir)
filename = "webrat-#{Time.now.to_i}.html" filename = "#{session.saved_page_dir}/webrat-#{Time.now.to_i}.html"
File.open(RAILS_ROOT + "/tmp/#{filename}", "w") do |f|
f.write response.body File.open(filename, "w") do |f|
f.write rewrite_css_and_image_references(session.response_body)
end end
`open tmp/#{filename}`
open_in_browser(filename)
end
def open_in_browser(path) # :nodoc
`open #{path}`
end end
# Issues a request for the URL pointed to by a link on the current page, # Issues a request for the URL pointed to by a link on the current page,
@ -244,30 +251,21 @@ module Webrat
def request_page(url, method, data) def request_page(url, method, data)
debug_log "REQUESTING PAGE: #{method.to_s.upcase} #{url} with #{data.inspect}" debug_log "REQUESTING PAGE: #{method.to_s.upcase} #{url} with #{data.inspect}"
session.send "#{method}_via_redirect", url, data || {} session.send "#{method}", url, data || {}
if response.body =~ /Exception caught/ || response.body.blank? if session.response_body =~ /Exception caught/ || session.response_body.blank?
save_and_open save_and_open
end end
session.assert_response :success flunk("Page load was not successful (Code: #{session.response_code.inspect})") unless (200..299).include?(session.response_code)
reset_dom reset_dom
end end
def response
session.response
end
def reset_dom def reset_dom
@dom = nil @dom = nil
@links = nil
@forms = nil @forms = nil
end end
def links
@links ||= links_within(nil)
end
def links_within(selector) def links_within(selector)
(dom / selector / "a[@href]").map do |link_element| (dom / selector / "a[@href]").map do |link_element|
Link.new(self, link_element) Link.new(self, link_element)
@ -284,13 +282,18 @@ module Webrat
def dom # :nodoc: def dom # :nodoc:
return @dom if defined?(@dom) && @dom return @dom if defined?(@dom) && @dom
flunk("You must visit a path before working with the page.") unless @session.response flunk("You must visit a path before working with the page.") unless @session.response_code
@dom = Hpricot(@session.response.body) @dom = Hpricot(@session.response_body)
end end
def flunk(message) def flunk(message)
raise message raise message
end end
def rewrite_css_and_image_references(response_html) # :nodoc
return response_html unless session.doc_root
response_html.gsub(/"\/(stylesheets|images)/, session.doc_root + '/\1')
end
end end
end end

View File

@ -0,0 +1,41 @@
module Webrat
class Session
def doc_root
nil
end
def saved_page_dir
File.expand_path(".")
end
def current_page
@current_page ||= Page.new(self)
end
def current_page=(new_page)
@current_page = new_page
end
def visits(*args)
Page.new(self, *args)
end
def respond_to?(name)
super || current_page.respond_to?(name)
end
def save_and_open_page
current_page.save_and_open
end
def method_missing(name, *args)
if current_page.respond_to?(name)
current_page.send(name, *args)
else
super
end
end
end
end

5
lib/webrat/mechanize.rb Normal file
View File

@ -0,0 +1,5 @@
require "rubygems"
require "mechanize"
require File.dirname(__FILE__) + "/mechanize/mechanize_session"

View File

@ -0,0 +1,25 @@
module Webrat
class MechanizeSession < Session
def initialize(mechanize = WWW::Mechanize.new)
@mechanize = mechanize
end
def get(url, data)
@mechanize_page = @mechanize.get(url, data)
end
def post(url, data)
@mechanize_page = @mechanize.post(url, data)
end
def response_body
@mechanize_page.content
end
def response_code
@mechanize_page.code.to_i
end
end
end

3
lib/webrat/rails.rb Normal file
View File

@ -0,0 +1,3 @@
require File.dirname(__FILE__) + "/rails/redirect_actions"
require File.dirname(__FILE__) + "/rails/rails_session"
require File.dirname(__FILE__) + "/rails/session"

View File

@ -0,0 +1,55 @@
module Webrat
class RailsSession < Session
def initialize(integration_session)
@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)
@integration_session.get_via_redirect(url, data)
end
def post(url, data)
@integration_session.post_via_redirect(url, data)
end
def put(url, data)
@integration_session.put_via_redirect(url, data)
end
def delete(url, data)
@integration_session.delete_via_redirect(url, data)
end
def response_body
response.body
end
def response_code
response.code.to_i
end
protected
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

@ -0,0 +1,37 @@
module ActionController
module Integration
class Session
unless instance_methods.include?("put_via_redirect")
include Webrat::RedirectActions
end
# Issues a GET request for a page, follows any redirects, and verifies the final page
# load was successful.
#
# Example:
# visits "/"
def visits(*args)
Webrat::Page.new(webrat_session, *args)
end
def respond_to?(name)
super || webrat_session.respond_to?(name)
end
def method_missing(name, *args)
if webrat_session.respond_to?(name)
webrat_session.send(name, *args)
else
super
end
end
protected
def webrat_session
@webrat_session ||= Webrat::RailsSession.new(self)
end
end
end
end

5
lib/webrat/selenium.rb Normal file
View File

@ -0,0 +1,5 @@
require "rubygems"
require "selenium"
require File.dirname(__FILE__) + "/selenium/selenium_session"

View File

@ -0,0 +1,143 @@
module Webrat
class SeleniumSession < Session
def initialize(selenium_driver)
@selenium = selenium_driver
define_location_strategies
end
def visits(url)
@selenium.open(url)
end
def fills_in(label_text, options)
@selenium.type("webrat=#{Regexp.escape(label_text)}", "#{options[:with]}")
end
def response_body
@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}")
wait_for_result(options[:wait])
end
def clicks_link(link_text, options = {})
@selenium.click("webratlink=#{link_text}")
wait_for_result(options[:wait])
end
def wait_for_result(wait_type)
if wait_type == :ajax
wait_for_ajax
elsif wait_type == :effects
wait_for_effects
else
wait_for_page_to_load
end
end
def wait_for_page_to_load(timeout = 15000)
@selenium.wait_for_page_to_load(timeout)
end
def wait_for_ajax(timeout = 15000)
@selenium.wait_for_condition "window.Ajax.activeRequestCount == 0", timeout
end
def wait_for_effects(timeout = 15000)
@selenium.wait_for_condition "window.Effect.Queue.size() == 0", timeout
end
def wait_for_ajax_and_effects
wait_for_ajax
wait_for_effects
end
def selects(option_text, options = {})
id_or_name_or_label = options[:from]
if id_or_name_or_label
select_locator = "webrat=#{id_or_name_or_label}"
else
select_locator = "webratselectwithoption=#{option_text}"
end
@selenium.select(select_locator, option_text)
end
def chooses(label_text)
@selenium.click("webrat=#{label_text}")
end
def checks(label_text)
@selenium.check("webrat=#{label_text}")
end
protected
def define_location_strategies
@selenium.add_location_strategy('label', <<-JS)
var allLabels = inDocument.getElementsByTagName("label");
var candidateLabels = $A(allLabels).select(function(candidateLabel){
var regExp = new RegExp('^' + locator + '\\\\b', 'i');
var labelText = getText(candidateLabel).strip();
return (labelText.search(regExp) >= 0);
});
if (candidateLabels.length == 0) {
return null;
}
candidateLabels = candidateLabels.sortBy(function(s) { return s.length * -1; }); //reverse length sort
var locatedLabel = candidateLabels.first();
var labelFor = locatedLabel.getAttribute('for');
return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow);
JS
@selenium.add_location_strategy('webrat', <<-JS)
var locationStrategies = selenium.browserbot.locationStrategies;
return locationStrategies['id'].call(this, locator, inDocument, inWindow)
|| locationStrategies['name'].call(this, locator, inDocument, inWindow)
|| locationStrategies['label'].call(this, locator, inDocument, inWindow)
|| null;
JS
@selenium.add_location_strategy('button', <<-JS)
if (locator == '*') {
return selenium.browserbot.locationStrategies['xpath'].call(this, "//input[@type='submit']", inDocument, inWindow)
}
var inputs = inDocument.getElementsByTagName('input');
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 false;
});
JS
@selenium.add_location_strategy('webratlink', <<-JS)
var links = inDocument.getElementsByTagName('a');
var candidateLinks = $A(links).select(function(candidateLink) {
return PatternMatcher.matches(locator, getText(candidateLink));
});
if (candidateLinks.length == 0) {
return null;
}
candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort
return candidateLinks.first();
JS
@selenium.add_location_strategy('webratselectwithoption', <<-JS)
var optionElements = inDocument.getElementsByTagName('option');
var locatedOption = $A(optionElements).find(function(candidate){
return (PatternMatcher.matches(locator, getText(candidate)));
});
return locatedOption ? locatedOption.parentNode : null;
JS
end
end
end

10
mechanize_spike.rb Normal file
View File

@ -0,0 +1,10 @@
require "lib/webrat"
require "lib/webrat/mechanize"
include Webrat
sess = MechanizeSession.new
sess.visits "http://www.google.com/"
sess.fills_in "q", :with => "Webrat"
sess.clicks_button
sess.save_and_open_page

34
selenium_spike.rb Normal file
View File

@ -0,0 +1,34 @@
require 'rubygems'
require "lib/webrat"
require "lib/webrat/selenium"
require 'selenium'
include Webrat
# To try it out:
#
# Install the required gem
# > sudo gem install Selenium
#
# Fire up the Selenium proxy server
# > selenium
#
# Run this script
# > ruby selenium_spike.rb
@sel = Selenium::SeleniumDriver.new("localhost", 4444, "*chrome", "http://localhost", 15000)
@sel.start
sess = SeleniumSession.new(@sel)
sess.visits "http://www.google.com/"
sess.fills_in "q", :with => "Webrat"
sess.clicks_link 'Images'
sess.clicks_button 'Search'
sess.selects 'Small images', :from => 'imagesize'
sess.clicks_link 'Preferences'
sess.chooses 'Do not filter'
sess.checks 'Open search results in a new browser window'
sess.clicks_button
sess.save_and_open_page
@sel.stop

View File

@ -1,11 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "attaches_file" do describe "attaches_file" do
before do before do
@session = ActionController::Integration::Session.new @session = Webrat::TestSession.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
@filename = __FILE__ @filename = __FILE__
@uploaded_file = mock @uploaded_file = mock
@ -13,7 +10,7 @@ describe "attaches_file" do
end end
it "should fail if no file field found" do it "should fail if no file field found" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/widgets"> <form method="post" action="/widgets">
</form> </form>
EOS EOS
@ -21,31 +18,31 @@ describe "attaches_file" do
end end
it "should submit empty strings for blank file fields" do it "should submit empty strings for blank file fields" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/widgets"> <form method="post" action="/widgets">
<input type="file" id="widget_file" name="widget[file]" /> <input type="file" id="widget_file" name="widget[file]" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/widgets", { "widget" => { "file" => "" } }) @session.expects(:post).with("/widgets", { "widget" => { "file" => "" } })
@session.clicks_button @session.clicks_button
end end
it "should submit the attached file" do it "should submit the attached file" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/widgets"> <form method="post" action="/widgets">
<label for="widget_file">Document</label> <label for="widget_file">Document</label>
<input type="file" id="widget_file" name="widget[file]" /> <input type="file" id="widget_file" name="widget[file]" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/widgets", { "widget" => { "file" => @uploaded_file } }) @session.expects(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } })
@session.attaches_file "Document", @filename @session.attaches_file "Document", @filename
@session.clicks_button @session.clicks_button
end end
it "should support collections" do it "should support collections" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/widgets"> <form method="post" action="/widgets">
<label for="widget_file1">Document</label> <label for="widget_file1">Document</label>
<input type="file" id="widget_file1" name="widget[files][]" /> <input type="file" id="widget_file1" name="widget[files][]" />
@ -54,7 +51,7 @@ describe "attaches_file" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } }) @session.expects(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
@session.attaches_file "Document", @filename @session.attaches_file "Document", @filename
@session.attaches_file "Spreadsheet", @filename @session.attaches_file "Spreadsheet", @filename
@session.clicks_button @session.clicks_button

View File

@ -1,15 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "checks" do describe "checks" do
before do before do
@session = ActionController::Integration::Session.new @session = Webrat::TestSession.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
end end
it "should fail if no checkbox found" do it "should fail if no checkbox found" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
</form> </form>
EOS EOS
@ -18,7 +15,7 @@ describe "checks" do
end end
it "should fail if input is not a checkbox" do it "should fail if input is not a checkbox" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="text" name="remember_me" /> <input type="text" name="remember_me" />
</form> </form>
@ -28,7 +25,7 @@ describe "checks" do
end end
it "should check rails style checkboxes" do it "should check rails style checkboxes" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" /> <input id="user_tos" name="user[tos]" type="checkbox" value="1" />
<input name="user[tos]" type="hidden" value="0" /> <input name="user[tos]" type="hidden" value="0" />
@ -36,31 +33,31 @@ describe "checks" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "1"}) @session.expects(:get).with("/login", "user" => {"tos" => "1"})
@session.checks "TOS" @session.checks "TOS"
@session.clicks_button @session.clicks_button
end end
it "should result in the value on being posted if not specified" do it "should result in the value on being posted if not specified" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="checkbox" name="remember_me" /> <input type="checkbox" name="remember_me" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "remember_me" => "on") @session.expects(:post).with("/login", "remember_me" => "on")
@session.checks "remember_me" @session.checks "remember_me"
@session.clicks_button @session.clicks_button
end end
it "should result in a custom value being posted" do it "should result in a custom value being posted" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="checkbox" name="remember_me" value="yes" /> <input type="checkbox" name="remember_me" value="yes" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "remember_me" => "yes") @session.expects(:post).with("/login", "remember_me" => "yes")
@session.checks "remember_me" @session.checks "remember_me"
@session.clicks_button @session.clicks_button
end end
@ -68,14 +65,11 @@ end
describe "unchecks" do describe "unchecks" do
before do before do
@session = ActionController::Integration::Session.new @session = Webrat::TestSession.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
end end
it "should fail if no checkbox found" do it "should fail if no checkbox found" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
</form> </form>
EOS EOS
@ -84,7 +78,7 @@ describe "unchecks" do
end end
it "should fail if input is not a checkbox" do it "should fail if input is not a checkbox" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="text" name="remember_me" /> <input type="text" name="remember_me" />
</form> </form>
@ -94,7 +88,7 @@ describe "unchecks" do
end end
it "should uncheck rails style checkboxes" do it "should uncheck rails style checkboxes" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" /> <input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
<input name="user[tos]" type="hidden" value="0" /> <input name="user[tos]" type="hidden" value="0" />
@ -102,20 +96,20 @@ describe "unchecks" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "0"}) @session.expects(:get).with("/login", "user" => {"tos" => "0"})
@session.checks "TOS" @session.checks "TOS"
@session.unchecks "TOS" @session.unchecks "TOS"
@session.clicks_button @session.clicks_button
end end
it "should result in value not being posted" do it "should result in value not being posted" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="checkbox" name="remember_me" value="yes" checked="checked" /> <input type="checkbox" name="remember_me" value="yes" checked="checked" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", {}) @session.expects(:post).with("/login", {})
@session.unchecks "remember_me" @session.unchecks "remember_me"
@session.clicks_button @session.clicks_button
end end

View File

@ -1,15 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "chooses" do describe "chooses" do
before do before do
@session = ActionController::Integration::Session.new @session = Webrat::TestSession.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
end end
it "should fail if no radio buttons found" do it "should fail if no radio buttons found" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
</form> </form>
EOS EOS
@ -18,7 +15,7 @@ describe "chooses" do
end end
it "should fail if input is not a radio button" do it "should fail if input is not a radio button" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="text" name="first_option" /> <input type="text" name="first_option" />
</form> </form>
@ -28,7 +25,7 @@ describe "chooses" do
end end
it "should check rails style radio buttons" do it "should check rails style radio buttons" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" /> <input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label> <label for="user_gender_male">Male</label>
@ -37,13 +34,13 @@ describe "chooses" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"}) @session.expects(:get).with("/login", "user" => {"gender" => "M"})
@session.chooses "Male" @session.chooses "Male"
@session.clicks_button @session.clicks_button
end end
it "should only submit last chosen value" do it "should only submit last chosen value" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" /> <input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label> <label for="user_gender_male">Male</label>
@ -52,32 +49,32 @@ describe "chooses" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"}) @session.expects(:get).with("/login", "user" => {"gender" => "M"})
@session.chooses "Female" @session.chooses "Female"
@session.chooses "Male" @session.chooses "Male"
@session.clicks_button @session.clicks_button
end end
it "should result in the value on being posted if not specified" do it "should result in the value on being posted if not specified" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="radio" name="first_option" /> <input type="radio" name="first_option" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "first_option" => "on") @session.expects(:post).with("/login", "first_option" => "on")
@session.chooses "first_option" @session.chooses "first_option"
@session.clicks_button @session.clicks_button
end end
it "should result in the value on being posted if not specified and checked by default" do it "should result in the value on being posted if not specified and checked by default" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="radio" name="first_option" checked="checked"/> <input type="radio" name="first_option" checked="checked"/>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "first_option" => "on") @session.expects(:post).with("/login", "first_option" => "on")
@session.clicks_button @session.clicks_button
end end
end end

View File

@ -1,18 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "clicks_button" do describe "clicks_button" do
before do before do
@session = ActionController::Integration::Session.new @session = Webrat::TestSession.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@page = Webrat::Page.new(@session)
@session.stubs(:current_page).returns(@page)
@response = mock
@session.stubs(:response).returns(@response)
end end
it "should fail if no buttons" do it "should fail if no buttons" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"></form> <form method="get" action="/login"></form>
EOS EOS
@ -20,7 +14,7 @@ describe "clicks_button" do
end end
it "should fail if input is not a submit button" do it "should fail if input is not a submit button" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input type="reset" /> <input type="reset" />
</form> </form>
@ -30,38 +24,27 @@ describe "clicks_button" do
end end
it "should default to get method" do it "should default to get method" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form action="/login"> <form action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect) @session.expects(:get)
@session.clicks_button @session.clicks_button
end end
it "should assert valid response" do it "should assert valid response" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form action="/login"> <form action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:assert_response).with(:success) @session.response_code = 404
@session.clicks_button lambda { @session.clicks_button }.should raise_error
end
it "should default to current url" do
@response.stubs(:body).returns(<<-EOS)
<form method="get">
<input type="submit" />
</form>
EOS
@page.stubs(:url).returns("/current")
@session.expects(:get_via_redirect).with("/current", {})
@session.clicks_button
end end
it "should submit the first form by default" do it "should submit the first form by default" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/form1"> <form method="get" action="/form1">
<input type="submit" /> <input type="submit" />
</form> </form>
@ -69,12 +52,12 @@ describe "clicks_button" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/form1", {}) @session.expects(:get).with("/form1", {})
@session.clicks_button @session.clicks_button
end end
it "should not explode on file fields" do it "should not explode on file fields" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/form1"> <form method="get" action="/form1">
<input type="file" /> <input type="file" />
<input type="submit" /> <input type="submit" />
@ -84,7 +67,7 @@ describe "clicks_button" do
end end
it "should submit the form with the specified button" do it "should submit the form with the specified button" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/form1"> <form method="get" action="/form1">
<input type="submit" /> <input type="submit" />
</form> </form>
@ -92,98 +75,98 @@ describe "clicks_button" do
<input type="submit" value="Form2" /> <input type="submit" value="Form2" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/form2", {}) @session.expects(:get).with("/form2", {})
@session.clicks_button "Form2" @session.clicks_button "Form2"
end end
it "should use action from form" do it "should use action from form" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", {}) @session.expects(:get).with("/login", {})
@session.clicks_button @session.clicks_button
end end
it "should use method from form" do it "should use method from form" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect) @session.expects(:post)
@session.clicks_button @session.clicks_button
end end
it "should send button as param if it has a name" do it "should send button as param if it has a name" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="submit" name="cancel" value="Cancel" /> <input type="submit" name="cancel" value="Cancel" />
<input type="submit" name="login" value="Login" /> <input type="submit" name="login" value="Login" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "login" => "Login") @session.expects(:post).with("/login", "login" => "Login")
@session.clicks_button("Login") @session.clicks_button("Login")
end end
it "should not send button as param if it has no name" do it "should not send button as param if it has no name" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="submit" name="cancel" value="Cancel" /> <input type="submit" name="cancel" value="Cancel" />
<input type="submit" value="Login" /> <input type="submit" value="Login" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", {}) @session.expects(:post).with("/login", {})
@session.clicks_button("Login") @session.clicks_button("Login")
end end
it "should send default password field values" do it "should send default password field values" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_password" name="user[password]" value="mypass" type="password" /> <input id="user_password" name="user[password]" value="mypass" type="password" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"password" => "mypass"}) @session.expects(:get).with("/login", "user" => {"password" => "mypass"})
@session.clicks_button @session.clicks_button
end end
it "should send default hidden field values" do it "should send default hidden field values" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="hidden" /> <input id="user_email" name="user[email]" value="test@example.com" type="hidden" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => "test@example.com"}) @session.expects(:get).with("/login", "user" => {"email" => "test@example.com"})
@session.clicks_button @session.clicks_button
end end
it "should send default text field values" do it "should send default text field values" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="text" /> <input id="user_email" name="user[email]" value="test@example.com" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => "test@example.com"}) @session.expects(:get).with("/login", "user" => {"email" => "test@example.com"})
@session.clicks_button @session.clicks_button
end end
it "should send default checked fields" do it "should send default checked fields" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_tos" name="user[tos]" value="1" type="checkbox" checked="checked" /> <input id="user_tos" name="user[tos]" value="1" type="checkbox" checked="checked" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "1"}) @session.expects(:get).with("/login", "user" => {"tos" => "1"})
@session.clicks_button @session.clicks_button
end end
it "should send default radio options" do it "should send default radio options" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" /> <input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label> <label for="user_gender_male">Male</label>
@ -192,36 +175,36 @@ describe "clicks_button" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "F"}) @session.expects(:get).with("/login", "user" => {"gender" => "F"})
@session.clicks_button @session.clicks_button
end end
it "should send correct data for rails style unchecked fields" do it "should send correct data for rails style unchecked fields" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" /> <input id="user_tos" name="user[tos]" type="checkbox" value="1" />
<input name="user[tos]" type="hidden" value="0" /> TOS <input name="user[tos]" type="hidden" value="0" /> TOS
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "0"}) @session.expects(:get).with("/login", "user" => {"tos" => "0"})
@session.clicks_button @session.clicks_button
end end
it "should send correct data for rails style checked fields" do it "should send correct data for rails style checked fields" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" /> <input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
<input name="user[tos]" type="hidden" value="0" /> TOS <input name="user[tos]" type="hidden" value="0" /> TOS
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "1"}) @session.expects(:get).with("/login", "user" => {"tos" => "1"})
@session.clicks_button @session.clicks_button
end end
it "should send default collection fields" do it "should send default collection fields" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="checkbox" name="options[]" value="burger" checked="checked" /> <input type="checkbox" name="options[]" value="burger" checked="checked" />
<input type="radio" name="options[]" value="fries" checked="checked" /> <input type="radio" name="options[]" value="fries" checked="checked" />
@ -236,36 +219,36 @@ describe "clicks_button" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", @session.expects(:post).with("/login",
"options" => ["burger", "fries", "soda", "soda", "dessert"], "options" => ["burger", "fries", "soda", "soda", "dessert"],
"response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]}) "response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]})
@session.clicks_button @session.clicks_button
end end
it "should not send default unchecked fields" do it "should not send default unchecked fields" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_tos" name="user[tos]" value="1" type="checkbox" /> <input id="user_tos" name="user[tos]" value="1" type="checkbox" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", {}) @session.expects(:get).with("/login", {})
@session.clicks_button @session.clicks_button
end end
it "should send default textarea values" do it "should send default textarea values" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/posts"> <form method="post" action="/posts">
<textarea name="post[body]">Post body here!</textarea> <textarea name="post[body]">Post body here!</textarea>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/posts", "post" => {"body" => "Post body here!"}) @session.expects(:post).with("/posts", "post" => {"body" => "Post body here!"})
@session.clicks_button @session.clicks_button
end end
it "should send default selected option value from select" do it "should send default selected option value from select" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<select name="month"> <select name="month">
<option value="1">January</option> <option value="1">January</option>
@ -274,12 +257,12 @@ describe "clicks_button" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "month" => "2") @session.expects(:get).with("/login", "month" => "2")
@session.clicks_button @session.clicks_button
end end
it "should send default selected option inner html from select when no value attribute" do it "should send default selected option inner html from select when no value attribute" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<select name="month"> <select name="month">
<option>January</option> <option>January</option>
@ -288,12 +271,12 @@ describe "clicks_button" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "month" => "February") @session.expects(:get).with("/login", "month" => "February")
@session.clicks_button @session.clicks_button
end end
it "should send first select option value when no option selected" do it "should send first select option value when no option selected" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<select name="month"> <select name="month">
<option value="1">January</option> <option value="1">January</option>
@ -302,52 +285,52 @@ describe "clicks_button" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "month" => "1") @session.expects(:get).with("/login", "month" => "1")
@session.clicks_button @session.clicks_button
end end
it "should handle nested properties" do it "should handle nested properties" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input type="text" id="contestant_scores_12" name="contestant[scores][1]" value="2"/> <input type="text" id="contestant_scores_12" name="contestant[scores][1]" value="2"/>
<input type="text" id="contestant_scores_13" name="contestant[scores][3]" value="4"/> <input type="text" id="contestant_scores_13" name="contestant[scores][3]" value="4"/>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}}) @session.expects(:post).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}})
@session.clicks_button @session.clicks_button
end end
it "should send default empty text field values" do it "should send default empty text field values" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" /> <input id="user_email" name="user[email]" value="" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""}) @session.expects(:get).with("/login", "user" => {"email" => ""})
@session.clicks_button @session.clicks_button
end end
it "should recognize button tags" do it "should recognize button tags" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" /> <input id="user_email" name="user[email]" value="" type="text" />
<button type="submit" /> <button type="submit" />
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""}) @session.expects(:get).with("/login", "user" => {"email" => ""})
@session.clicks_button @session.clicks_button
end end
it "should recognize button tags by content" do it "should recognize button tags by content" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" /> <input id="user_email" name="user[email]" value="" type="text" />
<button type="submit">Login</button> <button type="submit">Login</button>
</form> </form>
EOS EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""}) @session.expects(:get).with("/login", "user" => {"email" => ""})
@session.clicks_button "Login" @session.clicks_button "Login"
end end
end end

View File

@ -1,55 +1,52 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "clicks_link" do describe "clicks_link" do
before do before do
@session = ActionController::Integration::Session.new @session = Webrat::TestSession.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
end end
it "should use get by default" do it "should use get by default" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS EOS
@session.expects(:get_via_redirect).with("/page", {}) @session.expects(:get).with("/page", {})
@session.clicks_link "Link text" @session.clicks_link "Link text"
end end
it "should click get links" do it "should click get links" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS EOS
@session.expects(:get_via_redirect).with("/page", {}) @session.expects(:get).with("/page", {})
@session.clicks_get_link "Link text" @session.clicks_get_link "Link text"
end end
it "should click delete links" do it "should click delete links" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS EOS
@session.expects(:delete_via_redirect).with("/page", {}) @session.expects(:delete).with("/page", {})
@session.clicks_delete_link "Link text" @session.clicks_delete_link "Link text"
end end
it "should click post links" do it "should click post links" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS EOS
@session.expects(:post_via_redirect).with("/page", {}) @session.expects(:post).with("/page", {})
@session.clicks_post_link "Link text" @session.clicks_post_link "Link text"
end end
it "should click put links" do it "should click put links" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS EOS
@session.expects(:put_via_redirect).with("/page", {}) @session.expects(:put).with("/page", {})
@session.clicks_put_link "Link text" @session.clicks_put_link "Link text"
end end
it "should click rails javascript links with authenticity tokens" do it "should click rails javascript links with authenticity tokens" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form'); <a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none'; f.style.display = 'none';
this.parentNode.appendChild(f); this.parentNode.appendChild(f);
@ -63,12 +60,12 @@ describe "clicks_link" do
f.submit(); f.submit();
return false;">Posts</a> return false;">Posts</a>
EOS EOS
@session.expects(:post_via_redirect).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62") @session.expects(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
@session.clicks_link "Posts" @session.clicks_link "Posts"
end end
it "should click rails javascript delete links" do it "should click rails javascript delete links" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/posts/1" onclick="var f = document.createElement('form'); <a href="/posts/1" onclick="var f = document.createElement('form');
f.style.display = 'none'; f.style.display = 'none';
this.parentNode.appendChild(f); this.parentNode.appendChild(f);
@ -82,12 +79,12 @@ describe "clicks_link" do
f.submit(); f.submit();
return false;">Delete</a> return false;">Delete</a>
EOS EOS
@session.expects(:delete_via_redirect).with("/posts/1", {}) @session.expects(:delete).with("/posts/1", {})
@session.clicks_link "Delete" @session.clicks_link "Delete"
end end
it "should click rails javascript post links" do it "should click rails javascript post links" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form'); <a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none'; f.style.display = 'none';
this.parentNode.appendChild(f); this.parentNode.appendChild(f);
@ -96,12 +93,12 @@ describe "clicks_link" do
f.submit(); f.submit();
return false;">Posts</a> return false;">Posts</a>
EOS EOS
@session.expects(:post_via_redirect).with("/posts", {}) @session.expects(:post).with("/posts", {})
@session.clicks_link "Posts" @session.clicks_link "Posts"
end end
it "should click rails javascript put links" do it "should click rails javascript put links" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form'); <a href="/posts" onclick="var f = document.createElement('form');
f.style.display = 'none'; f.style.display = 'none';
this.parentNode.appendChild(f); this.parentNode.appendChild(f);
@ -115,97 +112,105 @@ describe "clicks_link" do
f.submit(); f.submit();
return false;">Put</a></h2> return false;">Put</a></h2>
EOS EOS
@session.expects(:put_via_redirect).with("/posts", {}) @session.expects(:put).with("/posts", {})
@session.clicks_link "Put" @session.clicks_link "Put"
end end
it "should assert valid response" do it "should assert valid response" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS EOS
@session.expects(:assert_response).with(:success) @session.response_code = 404
@session.clicks_link "Link text" lambda { @session.clicks_link "Link text" }.should raise_error
end end
it "should not be case sensitive" do it "should not be case sensitive" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS EOS
@session.expects(:get_via_redirect).with("/page", {}) @session.expects(:get).with("/page", {})
@session.clicks_link "LINK TEXT" @session.clicks_link "LINK TEXT"
end end
it "should match link substrings" do it "should match link substrings" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page">This is some cool link text, isn't it?</a> <a href="/page">This is some cool link text, isn't it?</a>
EOS EOS
@session.expects(:get_via_redirect).with("/page", {}) @session.expects(:get).with("/page", {})
@session.clicks_link "Link text" @session.clicks_link "Link text"
end end
it "should work with elements in the link" do it "should work with elements in the link" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page"><span>Link text</span></a> <a href="/page"><span>Link text</span></a>
EOS EOS
@session.expects(:get_via_redirect).with("/page", {}) @session.expects(:get).with("/page", {})
@session.clicks_link "Link text" @session.clicks_link "Link text"
end end
it "should match the first matching link" do it "should match the first matching link" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page1">Link text</a> <a href="/page1">Link text</a>
<a href="/page2">Link text</a> <a href="/page2">Link text</a>
EOS EOS
@session.expects(:get_via_redirect).with("/page1", {}) @session.expects(:get).with("/page1", {})
@session.clicks_link "Link text" @session.clicks_link "Link text"
end end
it "should choose the shortest link text match" do it "should choose the shortest link text match" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page1">Linkerama</a> <a href="/page1">Linkerama</a>
<a href="/page2">Link</a> <a href="/page2">Link</a>
EOS EOS
@session.expects(:get_via_redirect).with("/page2", {}) @session.expects(:get).with("/page2", {})
@session.clicks_link "Link" @session.clicks_link "Link"
end end
it "should click link within a selector" do it "should click link within a selector" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="/page1">Link</a> <a href="/page1">Link</a>
<div id="container"> <div id="container">
<a href="/page2">Link</a> <a href="/page2">Link</a>
</div> </div>
EOS EOS
@session.expects(:get_via_redirect).with("/page2", {}) @session.expects(:get).with("/page2", {})
@session.clicks_link_within "#container", "Link" @session.clicks_link_within "#container", "Link"
end end
it "should not make request when link is local anchor" do it "should not make request when link is local anchor" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="#section-1">Jump to Section 1</a> <a href="#section-1">Jump to Section 1</a>
EOS EOS
# Don't know why @session.expects(:get_via_redirect).never doesn't work here # Don't know why @session.expects(:get).never doesn't work here
@session.expects(:send).with('get_via_redirect', '#section-1', {}).never @session.expects(:send).with('get_via_redirect', '#section-1', {}).never
@session.clicks_link "Jump to Section 1" @session.clicks_link "Jump to Section 1"
end end
it "should follow relative links" do it "should follow relative links" do
@session.current_page.stubs(:url).returns("/page") @session.current_page.stubs(:url).returns("/page")
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="sub">Jump to sub page</a> <a href="sub">Jump to sub page</a>
EOS EOS
@session.expects(:get_via_redirect).with("/page/sub", {}) @session.expects(:get).with("/page/sub", {})
@session.clicks_link "Jump to sub page"
end
it "should follow fully qualified local links" do
@session.response_body = <<-EOS
<a href="http://www.example.com/page/sub">Jump to sub page</a>
EOS
@session.expects(:get).with("/page/sub", {})
@session.clicks_link "Jump to sub page" @session.clicks_link "Jump to sub page"
end end
it "should follow query parameters" do it "should follow query parameters" do
@session.current_page.stubs(:url).returns("/page") @session.current_page.stubs(:url).returns("/page")
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<a href="?foo=bar">Jump to foo bar</a> <a href="?foo=bar">Jump to foo bar</a>
EOS EOS
@session.expects(:get_via_redirect).with("/page?foo=bar", {}) @session.expects(:get).with("/page?foo=bar", {})
@session.clicks_link "Jump to foo bar" @session.clicks_link "Jump to foo bar"
end end
end end

View File

@ -1,40 +1,37 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "fills_in" do describe "fills_in" do
before do before do
@session = ActionController::Integration::Session.new @session = Webrat::TestSession.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
end end
it "should work with textareas" do it "should work with textareas" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="user_text">User Text</label> <label for="user_text">User Text</label>
<textarea id="user_text" name="user[text]"></textarea> <textarea id="user_text" name="user[text]"></textarea>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "user" => {"text" => "filling text area"}) @session.expects(:post).with("/login", "user" => {"text" => "filling text area"})
@session.fills_in "User Text", :with => "filling text area" @session.fills_in "User Text", :with => "filling text area"
@session.clicks_button @session.clicks_button
end end
it "should work with password fields" do it "should work with password fields" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input id="user_text" name="user[text]" type="password" /> <input id="user_text" name="user[text]" type="password" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "user" => {"text" => "pass"}) @session.expects(:post).with("/login", "user" => {"text" => "pass"})
@session.fills_in "user_text", :with => "pass" @session.fills_in "user_text", :with => "pass"
@session.clicks_button @session.clicks_button
end end
it "should fail if input not found" do it "should fail if input not found" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
</form> </form>
EOS EOS
@ -43,20 +40,20 @@ describe "fills_in" do
end end
it "should allow overriding default form values" do it "should allow overriding default form values" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="user_email">Email</label> <label for="user_email">Email</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" /> <input id="user_email" name="user[email]" value="test@example.com" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "user" => {"email" => "foo@example.com"}) @session.expects(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in "user[email]", :with => "foo@example.com" @session.fills_in "user[email]", :with => "foo@example.com"
@session.clicks_button @session.clicks_button
end end
it "should choose the shortest label match" do it "should choose the shortest label match" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="user_mail1">Some other mail</label> <label for="user_mail1">Some other mail</label>
<input id="user_mail1" name="user[mail1]" type="text" /> <input id="user_mail1" name="user[mail1]" type="text" />
@ -66,13 +63,13 @@ describe "fills_in" do
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "user" => {"mail1" => "", "mail2" => "value"}) @session.expects(:post).with("/login", "user" => {"mail1" => "", "mail2" => "value"})
@session.fills_in "Some", :with => "value" @session.fills_in "Some", :with => "value"
@session.clicks_button @session.clicks_button
end end
it "should choose the first label match if closest is a tie" do it "should choose the first label match if closest is a tie" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="user_mail1">Some mail one</label> <label for="user_mail1">Some mail one</label>
<input id="user_mail1" name="user[mail1]" type="text" /> <input id="user_mail1" name="user[mail1]" type="text" />
@ -82,13 +79,13 @@ describe "fills_in" do
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "user" => {"mail1" => "value", "mail2" => ""}) @session.expects(:post).with("/login", "user" => {"mail1" => "value", "mail2" => ""})
@session.fills_in "Some mail", :with => "value" @session.fills_in "Some mail", :with => "value"
@session.clicks_button @session.clicks_button
end end
it "should anchor label matches to start of label" do it "should anchor label matches to start of label" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="user_email">Some mail</label> <label for="user_email">Some mail</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" /> <input id="user_email" name="user[email]" value="test@example.com" type="text" />
@ -99,7 +96,7 @@ describe "fills_in" do
end end
it "should anchor label matches to word boundaries" do it "should anchor label matches to word boundaries" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="user_email">Emailtastic</label> <label for="user_email">Emailtastic</label>
<input id="user_email" name="user[email]" value="test@example.com" type="text" /> <input id="user_email" name="user[email]" value="test@example.com" type="text" />
@ -110,7 +107,7 @@ describe "fills_in" do
end end
it "should work with inputs nested in labels" do it "should work with inputs nested in labels" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label> <label>
Email Email
@ -119,32 +116,32 @@ describe "fills_in" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "user" => {"email" => "foo@example.com"}) @session.expects(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in "Email", :with => "foo@example.com" @session.fills_in "Email", :with => "foo@example.com"
@session.clicks_button @session.clicks_button
end end
it "should work with full input names" do it "should work with full input names" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<input id="user_email" name="user[email]" type="text" /> <input id="user_email" name="user[email]" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "user" => {"email" => "foo@example.com"}) @session.expects(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in "user[email]", :with => "foo@example.com" @session.fills_in "user[email]", :with => "foo@example.com"
@session.clicks_button @session.clicks_button
end end
it "should work with symbols" do it "should work with symbols" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="user_email">Email</label> <label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="text" /> <input id="user_email" name="user[email]" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "user" => {"email" => "foo@example.com"}) @session.expects(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fills_in :email, :with => "foo@example.com" @session.fills_in :email, :with => "foo@example.com"
@session.clicks_button @session.clicks_button
end end

14
spec/api/reloads_spec.rb Normal file
View File

@ -0,0 +1,14 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "reloads" do
before do
@session = Webrat::TestSession.new
@session.response_body = "Hello world"
end
it "should reload the page" do
@session.expects(:get).with("/", {}).times(2)
@session.visits("/")
@session.reloads
end
end

View File

@ -0,0 +1,53 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "save_and_open_page" do
before do
@session = Webrat::TestSession.new
@session.response_body = <<-HTML
<html>
<head>
<link href="/stylesheets/foo.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Hello world</h1>
<img src="/images/bar.png" />
</body>
</html>
HTML
File.stubs(:exist?).returns(true)
Time.stubs(:now).returns(1234)
Webrat::Page.any_instance.stubs(:open_in_browser)
@file_handle = mock()
File.stubs(:open).with(filename, 'w').yields(@file_handle)
@file_handle.stubs(:write)
end
it "should rewrite css rules" do
@file_handle.expects(:write).with do |html|
html =~ %r|#{@session.doc_root}/stylesheets/foo.css|s
end
@session.save_and_open_page
end
it "should rewrite image paths" do
@file_handle.expects(:write).with do |html|
html =~ %r|#{@session.doc_root}/images/bar.png|s
end
@session.save_and_open_page
end
it "should open the temp file in a browser" do
Webrat::Page.any_instance.expects(:open_in_browser).with(filename)
@session.save_and_open_page
end
def filename
File.expand_path("./webrat-#{Time.now}.html")
end
end

View File

@ -1,15 +1,12 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "selects" do describe "selects" do
before do before do
@session = ActionController::Integration::Session.new @session = Webrat::TestSession.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
end end
it "should fail if option not found" do it "should fail if option not found" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<select name="month"><option value="1">January</option></select> <select name="month"><option value="1">January</option></select>
</form> </form>
@ -19,7 +16,7 @@ describe "selects" do
end end
it "should fail if option not found in list specified by element name" do it "should fail if option not found in list specified by element name" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<select name="month"><option value="1">January</option></select> <select name="month"><option value="1">January</option></select>
<select name="year"><option value="2008">2008</option></select> <select name="year"><option value="2008">2008</option></select>
@ -30,7 +27,7 @@ describe "selects" do
end end
it "should fail if specified list not found" do it "should fail if specified list not found" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
<select name="month"><option value="1">January</option></select> <select name="month"><option value="1">January</option></select>
</form> </form>
@ -40,55 +37,55 @@ describe "selects" do
end end
it "should send value from option" do it "should send value from option" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<select name="month"><option value="1">January</option></select> <select name="month"><option value="1">January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "month" => "1") @session.expects(:post).with("/login", "month" => "1")
@session.selects "January", :from => "month" @session.selects "January", :from => "month"
@session.clicks_button @session.clicks_button
end end
it "should work with empty select lists" do it "should work with empty select lists" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<select name="month"></select> <select name="month"></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", 'month' => '') @session.expects(:post).with("/login", 'month' => '')
@session.clicks_button @session.clicks_button
end end
it "should work without specifying the field name or label" do it "should work without specifying the field name or label" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<select name="month"><option value="1">January</option></select> <select name="month"><option value="1">January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "month" => "1") @session.expects(:post).with("/login", "month" => "1")
@session.selects "January" @session.selects "January"
@session.clicks_button @session.clicks_button
end end
it "should send value from option in list specified by name" do it "should send value from option in list specified by name" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<select name="start_month"><option value="s1">January</option></select> <select name="start_month"><option value="s1">January</option></select>
<select name="end_month"><option value="e1">January</option></select> <select name="end_month"><option value="e1">January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1") @session.expects(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "end_month" @session.selects "January", :from => "end_month"
@session.clicks_button @session.clicks_button
end end
it "should send value from option in list specified by label" do it "should send value from option in list specified by label" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="start_month">Start Month</label> <label for="start_month">Start Month</label>
<select id="start_month" name="start_month"><option value="s1">January</option></select> <select id="start_month" name="start_month"><option value="s1">January</option></select>
@ -97,37 +94,37 @@ describe "selects" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1") @session.expects(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "End Month" @session.selects "January", :from => "End Month"
@session.clicks_button @session.clicks_button
end end
it "should use option text if no value" do it "should use option text if no value" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<select name="month"><option>January</option></select> <select name="month"><option>January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "month" => "January") @session.expects(:post).with("/login", "month" => "January")
@session.selects "January", :from => "month" @session.selects "January", :from => "month"
@session.clicks_button @session.clicks_button
end end
it "should find option by regexp" do it "should find option by regexp" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<select name="month"><option>January</option></select> <select name="month"><option>January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "month" => "January") @session.expects(:post).with("/login", "month" => "January")
@session.selects(/jan/i) @session.selects(/jan/i)
@session.clicks_button @session.clicks_button
end end
it "should find option by regexp in list specified by label" do it "should find option by regexp in list specified by label" do
@response.stubs(:body).returns(<<-EOS) @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">
<label for="start_month">Start Month</label> <label for="start_month">Start Month</label>
<select id="start_month" name="start_month"><option value="s1">January</option></select> <select id="start_month" name="start_month"><option value="s1">January</option></select>
@ -136,7 +133,7 @@ describe "selects" do
<input type="submit" /> <input type="submit" />
</form> </form>
EOS EOS
@session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1") @session.expects(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects(/jan/i, :from => "End Month") @session.selects(/jan/i, :from => "End Month")
@session.clicks_button @session.clicks_button
end end

22
spec/api/visits_spec.rb Normal file
View File

@ -0,0 +1,22 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "visits" do
before do
@session = Webrat::TestSession.new
@session.response_body = "Hello world"
end
it "should use get" do
@session.expects(:get).with("/", {})
@session.visits("/")
end
it "should assert valid response" do
@session.response_code = 404
lambda { @session.visits("/") }.should raise_error
end
it "should require a visit before manipulating page" do
lambda { @session.fills_in "foo", :with => "blah" }.should raise_error
end
end

View File

@ -0,0 +1,26 @@
module Webrat
class TestSession < Session
attr_accessor :response_body
attr_writer :response_code
def doc_root
File.expand_path(File.join(".", "public"))
end
def response_code
@response_code || 200
end
def get(url, data)
end
def post(url, data)
end
def put(url, data)
end
def delete(url, data)
end
end
end

View File

@ -0,0 +1,20 @@
# it "should default to current url" do
# # @session.current_page.stubs(:url).returns("/page")
# @session.response_body = <<-EOS
# <form method="get">
# <input type="submit" />
# </form>
# EOS
# @page.stubs(:url).returns("/current")
# @session.expects(:get).with("/current", {})
# @session.clicks_button
# end
#
# it "should follow fully qualified secure local links" do
# @session.response_body = <<-EOS
# <a href="https://www.example.com/page/sub">Jump to sub page</a>
# EOS
# @session.expects(:https!).with(true)
# @session.expects(:get).with("/page/sub", {})
# @session.clicks_link "Jump to sub page"
# end

View File

@ -1,25 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
RAILS_ROOT = "." unless defined?(RAILS_ROOT)
describe "reloads" do
before do
@session = ActionController::Integration::Session.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@response = mock
@session.stubs(:response).returns(@response)
@response.stubs(:body).returns("")
end
it "should reload the page" do
@session.expects(:get_via_redirect).with("/", {}).times(2)
@session.visits("/")
@session.reloads
end
it "should not request page if not visited any page" do
@session.expects(:get_via_redirect).never
@session.reloads
end
end

View File

@ -12,12 +12,7 @@ silence_warnings do
end end
require File.expand_path(File.dirname(__FILE__) + "/../lib/webrat") require File.expand_path(File.dirname(__FILE__) + "/../lib/webrat")
require File.dirname(__FILE__) + "/fakes/test_session"
class ActionController::Integration::Session
def flunk(message)
raise message
end
end
Spec::Runner.configure do |config| Spec::Runner.configure do |config|
config.mock_with :mocha config.mock_with :mocha

View File

@ -1,28 +0,0 @@
require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
RAILS_ROOT = "." unless defined?(RAILS_ROOT)
describe "visits" do
before do
@session = ActionController::Integration::Session.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@response = mock
@session.stubs(:response).returns(@response)
@response.stubs(:body).returns("")
end
it "should use get" do
@session.expects(:get_via_redirect).with("/", {})
@session.visits("/")
end
it "should assert valid response" do
@session.expects(:assert_response).with(:success)
@session.visits("/")
end
it "should require a visit before manipulating page" do
lambda { @session.fills_in "foo", :with => "blah" }.should raise_error
end
end