Merged to master again

This commit is contained in:
Rob Kaufman 2008-10-18 00:54:46 -07:00
commit 1a5db346c9
25 changed files with 406 additions and 171 deletions

View File

@ -3,12 +3,14 @@
* Major enhancements * Major enhancements
* Added #within for manipulating the current page within a selector scope * Added #within for manipulating the current page within a selector scope
* Add should_see and should_not_see for verifying HTML response bodys
* Add support for simulating SSL requests (Luke Melia) * Add support for simulating SSL requests (Luke Melia)
* Add support for file fields via #attaches_file method (Patch from Kyle Hargraves) * Add support for file fields via #attaches_file method (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)
* Separated Rails-specific code from the Webrat core to make it easier to use Webrat with other environments * Separated Rails-specific code from the Webrat core to make it easier to use Webrat with other environments
* Alias visits as visit, clicks_link as click_link, etc. for better readability * Alias visits as visit, clicks_link as click_link, etc. for better readability
* Raise error when trying to interact with a disabled form element (Luke Melia) * Raise error when trying to interact with a disabled form element (Luke Melia)
* Don't send disabled form elements to the server (Patch from Nicholas A. Evans)
* Minor enhancements * Minor enhancements
@ -25,6 +27,8 @@
* Support clicking links by title (Patch from Dan Barry) * Support clicking links by title (Patch from Dan Barry)
* Added missing spec for clicking image buttons (Patch from Tim Harper) * Added missing spec for clicking image buttons (Patch from Tim Harper)
* Switched tests to specs, and from Mocha to RSpec's mocking library * Switched tests to specs, and from Mocha to RSpec's mocking library
* Add support to click_button for IDs (Patch from Gwyn Morfey)
* Miscellaneous core refactorings (Patch from Jan Suchal)
* Bug fixes * Bug fixes
@ -34,6 +38,7 @@
* 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) * Don't explode if encountering inputs with no type attribute (assume text)
* Fix bug where choosing a radio button in a series with a default submitted the incorrect field value (Luke Melia)
== 0.2.0 / 2008-04-04 == 0.2.0 / 2008-04-04

View File

@ -8,6 +8,7 @@ init.rb
install.rb install.rb
lib/webrat.rb lib/webrat.rb
lib/webrat/core.rb lib/webrat/core.rb
lib/webrat/core/assertions.rb
lib/webrat/core/field.rb lib/webrat/core/field.rb
lib/webrat/core/flunk.rb lib/webrat/core/flunk.rb
lib/webrat/core/form.rb lib/webrat/core/form.rb
@ -24,13 +25,20 @@ lib/webrat/merb/indifferent_access.rb
lib/webrat/merb/param_parser.rb lib/webrat/merb/param_parser.rb
lib/webrat/merb/support.rb lib/webrat/merb/support.rb
lib/webrat/merb/url_encoded_pair_parser.rb lib/webrat/merb/url_encoded_pair_parser.rb
lib/webrat/rack/rack_session.rb
lib/webrat/rails.rb lib/webrat/rails.rb
lib/webrat/rails/rails_session.rb lib/webrat/rails/rails_session.rb
lib/webrat/rails/redirect_actions.rb lib/webrat/rails/redirect_actions.rb
lib/webrat/rails/session.rb lib/webrat/rails/session.rb
lib/webrat/selenium.rb lib/webrat/selenium.rb
lib/webrat/selenium/location_strategy_javascript/button.js
lib/webrat/selenium/location_strategy_javascript/label.js
lib/webrat/selenium/location_strategy_javascript/webrat.js
lib/webrat/selenium/location_strategy_javascript/webratlink.js
lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js
lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js
lib/webrat/selenium/selenium_session.rb lib/webrat/selenium/selenium_session.rb
mechanize_spike.rb lib/webrat/sinatra/sinatra_session.rbmechanize_spike.rb
selenium_spike.rb selenium_spike.rb
spec/api/attaches_file_spec.rb spec/api/attaches_file_spec.rb
spec/api/checks_spec.rb spec/api/checks_spec.rb
@ -41,6 +49,8 @@ spec/api/fills_in_spec.rb
spec/api/reloads_spec.rb spec/api/reloads_spec.rb
spec/api/save_and_open_spec.rb spec/api/save_and_open_spec.rb
spec/api/selects_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/visits_spec.rb
spec/api/within_spec.rb spec/api/within_spec.rb
spec/fakes/test_session.rb spec/fakes/test_session.rb
@ -53,3 +63,4 @@ spec/webrat/core/session_spec.rb
spec/webrat/merb/helper.rb spec/webrat/merb/helper.rb
spec/webrat/rails/helper.rb spec/webrat/rails/helper.rb
spec/webrat/rails/rails_session_spec.rb spec/webrat/rails/rails_session_spec.rb
webrat.gemspec

View File

@ -1,11 +1,9 @@
Webrat === Webrat
======
- [Code on GitHub](http://github.com/brynary/webrat) - [Code on GitHub](http://github.com/brynary/webrat)
- [Tickets on Lighthouse](http://webrat.lighthouseapp.com/) - [Tickets on Lighthouse](http://webrat.lighthouseapp.com/)
Description === Description
-----------
Webrat (_Ruby Acceptance Testing for Web applications_) Webrat (_Ruby Acceptance Testing for Web applications_)
lets you quickly write robust and thorough acceptance tests for a Ruby lets you quickly write robust and thorough acceptance tests for a Ruby
@ -23,8 +21,7 @@ run your tests much faster and more frequently.
Initial development was sponsored by [EastMedia](http://www.eastmedia.com). Initial development was sponsored by [EastMedia](http://www.eastmedia.com).
Synopsis === Synopsis
--------
def test_sign_up def test_sign_up
visits "/" visits "/"
@ -62,13 +59,9 @@ tests to break unnecessarily as your application evolves:
A test written with Webrat can handle these changes to these without any modifications. A test written with Webrat can handle these changes to these without any modifications.
Install === Install
-------
* Rails >= 1.2.6 or Merb edge To install the latest release:
* Hpricot >= 0.6
* Rails integration tests in Test::Unit _or_
* RSpec stories (using an RSpec version >= revision 2997)
sudo gem install webrat sudo gem install webrat
@ -85,23 +78,20 @@ Merb::Config.use do |c|
end end
== HISTORY: == HISTORY:
Requirements === Requirements
------------
- Rails >= 1.2.6 - Rails >= 1.2.6
- Hpricot >= 0.6 - Hpricot >= 0.6
- Rails integration tests in Test::Unit _or_ - Rails integration tests in Test::Unit _or_
- RSpec stories (using an RSpec version >= revision 2997) - RSpec stories (using an RSpec version >= revision 2997)
Authors === Authors
-------
- Maintained by [Bryan Helmkamp](mailto:bryan@brynary.com) - Maintained by [Bryan Helmkamp](mailto:bryan@brynary.com)
- Original code written by [Seth Fitzsimmons](mailto:seth@mojodna.net) - Original code written by [Seth Fitzsimmons](mailto:seth@mojodna.net)
- Many other contributors. See attributions in History.txt - Many other contributors. See attributions in History.txt
License === License
-------
Copyright (c) 2007 Bryan Helmkamp, Seth Fitzsimmons. Copyright (c) 2007 Bryan Helmkamp, Seth Fitzsimmons.
See MIT-LICENSE.txt in this directory. See MIT-LICENSE.txt in this directory.

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 = 97.1 # Make sure you have rcov 0.7 or higher! t.threshold = 97.2 # Make sure you have rcov 0.7 or higher!
end end
remove_task "default" remove_task "default"

View File

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

View File

@ -0,0 +1,27 @@
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

@ -46,7 +46,7 @@ module Webrat
end end
def disabled? def disabled?
!@element["disabled"].nil? && @element["disabled"] != 'false' @element.attributes.has_key?("disabled") && @element["disabled"] != 'false'
end end
def raise_error_if_disabled def raise_error_if_disabled
@ -54,6 +54,7 @@ module Webrat
end end
def to_param def to_param
return nil if disabled?
value = @value.to_s.gsub('&', '%26') value = @value.to_s.gsub('&', '%26')
param_parser.parse_query_parameters("#{name}=#{value}") param_parser.parse_query_parameters("#{name}=#{value}")
end end
@ -137,6 +138,10 @@ module Webrat
@element.innerHTML =~ /#{Regexp.escape(text.to_s)}/i @element.innerHTML =~ /#{Regexp.escape(text.to_s)}/i
end end
# def matches_id?(id)
# @element["id"] =~ /^\W*#{Regexp.escape(id.to_s)}/i
# end
def matches_value?(value) def matches_value?(value)
@element["value"] =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value) || matches_alt?(value) @element["value"] =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value) || matches_alt?(value)
end end

View File

@ -30,35 +30,17 @@ module Webrat
def find_button(value = nil) def find_button(value = nil)
return fields_by_type([ButtonField]).first if value.nil? return fields_by_type([ButtonField]).first if value.nil?
possible_buttons = fields_by_type([ButtonField]) possible_buttons = fields_by_type([ButtonField])
possible_buttons.detect { |possible_button| possible_button.matches_id?(value) } ||
possible_buttons.each do |possible_button| possible_buttons.detect { |possible_button| possible_button.matches_value?(value) }
return possible_button if possible_button.matches_id?(value)
end
possible_buttons.each do |possible_button|
return possible_button if possible_button.matches_value?(value)
end
#If nothing matched on value, try by name.
possible_buttons.each do |possible_button|
return possible_button if possible_button.matches_caption?(value)
end
nil
end end
def fields def fields
return @fields if @fields return @fields if @fields
@fields = [] @fields = (@element / "button, input, textarea, select").collect do |field_element|
Field.class_for_element(field_element).new(self, field_element)
(@element / "input, textarea, select, button").each do |field_element|
@fields << Field.class_for_element(field_element).new(self, field_element)
end end
@fields
end end
def submit def submit
@ -68,29 +50,18 @@ module Webrat
protected protected
def find_field_by_id(possible_fields, id) def find_field_by_id(possible_fields, id)
possible_fields.each do |possible_field| possible_fields.detect { |possible_field| possible_field.matches_id?(id) }
return possible_field if possible_field.matches_id?(id)
end
nil
end end
def find_field_by_name(possible_fields, name) def find_field_by_name(possible_fields, name)
possible_fields.each do |possible_field| possible_fields.detect { |possible_field| possible_field.matches_name?(name) }
return possible_field if possible_field.matches_name?(name)
end
nil
end end
def find_field_by_label(possible_fields, label) def find_field_by_label(possible_fields, label)
matching_fields = [] matching_fields = possible_fields.select do |possible_field|
possible_field.matches_label?(label)
possible_fields.each do |possible_field|
matching_fields << possible_field if possible_field.matches_label?(label)
end end
matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length }
matching_fields.sort_by { |f| f.label_text.length }.first
end end
def fields_by_type(field_types) def fields_by_type(field_types)
@ -132,7 +103,7 @@ module Webrat
def merge_hash_values(a, b) # :nodoc: def merge_hash_values(a, b) # :nodoc:
a.keys.each do |k| a.keys.each do |k|
if b.has_key?(k) if b.has_key?(k)
case [a[k].class, b[k].class] case [a[k], b[k]].map(&:class)
when [Hash, Hash] when [Hash, Hash]
a[k] = merge_hash_values(a[k], b[k]) a[k] = merge_hash_values(a[k], b[k])
b.delete(k) b.delete(k)

View File

@ -4,6 +4,7 @@ module Webrat
class Scope class Scope
include Logging include Logging
include Flunk include Flunk
include Assertions
def initialize(session, html, selector = nil) def initialize(session, html, selector = nil)
@session = session @session = session
@ -175,19 +176,21 @@ module Webrat
alias_method :click_button, :clicks_button alias_method :click_button, :clicks_button
def dom # :nodoc: def dom # :nodoc:
return @dom if defined?(@dom) && @dom @dom ||= Hpricot(scoped_html)
@dom = Hpricot(@html)
if @selector
html = (@dom / @selector).first.to_html
@dom = Hpricot(html)
end
return @dom
end end
protected protected
def scoped_html
@scoped_html ||= begin
if @selector
(Hpricot(@html) / @selector).first.to_html
else
@html
end
end
end
def find_select_option(option_text, id_or_name_or_label) def find_select_option(option_text, id_or_name_or_label)
if id_or_name_or_label if id_or_name_or_label
field = find_field(id_or_name_or_label, SelectField) field = find_field(id_or_name_or_label, SelectField)
@ -212,14 +215,12 @@ module Webrat
end end
def find_link(text, selector = nil) def find_link(text, selector = nil)
matching_links = [] matching_links = links_within(selector).select do |possible_link|
possible_link.matches_text?(text)
links_within(selector).each do |possible_link|
matching_links << possible_link if possible_link.matches_text?(text)
end end
if matching_links.any? if matching_links.any?
matching_links.sort_by { |l| l.text.length }.first matching_links.min { |a, b| a.text.length <=> b.text.length }
else else
flunk("Could not find link with text #{text.inspect}") flunk("Could not find link with text #{text.inspect}")
end end

View File

@ -109,6 +109,11 @@ module Webrat
yield Scope.new(self, response_body, selector) yield Scope.new(self, response_body, selector)
end 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 = {}) def visits(url = nil, http_method = :get, data = {})
request_page(url, http_method, data) request_page(url, http_method, data)
end end
@ -136,6 +141,7 @@ module Webrat
def_delegators :current_scope, :click_post_link, :clicks_post_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_put_link, :clicks_put_link
def_delegators :current_scope, :click_button, :clicks_button def_delegators :current_scope, :click_button, :clicks_button
def_delegators :current_scope, :should_see
def_delegators :current_scope, :should_not_see
end end
end end

View File

@ -0,0 +1,13 @@
require 'webrat'
module Webrat
class RackSession < Session
def response_body
@response.body
end
def response_code
@response.status
end
end
end

View File

@ -6,15 +6,6 @@ module ActionController
include Webrat::RedirectActions include Webrat::RedirectActions
end 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_session.visits(*args)
end
def respond_to?(name) def respond_to?(name)
super || webrat_session.respond_to?(name) super || webrat_session.respond_to?(name)
end end
@ -32,6 +23,7 @@ module ActionController
def webrat_session def webrat_session
@webrat_session ||= Webrat::RailsSession.new(self) @webrat_session ||= Webrat::RailsSession.new(self)
end end
end end
end end
end end

View File

@ -0,0 +1,12 @@
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;
});

View File

@ -0,0 +1,16 @@
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');
if ((labelFor == null) && (locatedLabel.hasChildNodes())) {
return locatedLabel.firstChild; //TODO: should find the first form field, not just any node
}
return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow);

View File

@ -0,0 +1,5 @@
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;

View File

@ -0,0 +1,9 @@
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();

View File

@ -0,0 +1,15 @@
var locatorParts = locator.split('|');
var cssAncestor = locatorParts[0];
var linkText = locatorParts[1];
var matchingElements = cssQuery(cssAncestor, inDocument);
var candidateLinks = matchingElements.collect(function(ancestor){
var links = ancestor.getElementsByTagName('a');
return $A(links).select(function(candidateLink) {
return PatternMatcher.matches(linkText, getText(candidateLink));
});
}).flatten().compact();
if (candidateLinks.length == 0) {
return null;
}
candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort
return candidateLinks.first();

View File

@ -0,0 +1,5 @@
var optionElements = inDocument.getElementsByTagName('option');
var locatedOption = $A(optionElements).find(function(candidate){
return (PatternMatcher.matches(locator, getText(candidate)));
});
return locatedOption ? locatedOption.parentNode : null;

View File

@ -2,7 +2,7 @@ module Webrat
class SeleniumSession < Session class SeleniumSession < Session
def initialize(selenium_driver) def initialize(selenium_driver)
super super()
@selenium = selenium_driver @selenium = selenium_driver
define_location_strategies define_location_strategies
end end
@ -11,6 +11,8 @@ module Webrat
@selenium.open(url) @selenium.open(url)
end end
alias_method :visit, :visits
def fills_in(field_identifier, options) def fills_in(field_identifier, options)
locator = "webrat=#{Regexp.escape(field_identifier)}" locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.type(locator, "#{options[:with]}") @selenium.type(locator, "#{options[:with]}")
@ -26,11 +28,13 @@ module Webrat
@selenium.click("button=#{button_text}") @selenium.click("button=#{button_text}")
wait_for_result(options[:wait]) wait_for_result(options[:wait])
end end
alias_method :click_button, :clicks_button
def clicks_link(link_text, options = {}) def clicks_link(link_text, options = {})
@selenium.click("webratlink=#{link_text}") @selenium.click("webratlink=#{link_text}")
wait_for_result(options[:wait]) wait_for_result(options[:wait])
end end
alias_method :click_link, :clicks_link
def clicks_link_within(selector, link_text, options = {}) def clicks_link_within(selector, link_text, options = {})
@selenium.click("webratlinkwithin=#{selector}|#{link_text}") @selenium.click("webratlinkwithin=#{selector}|#{link_text}")
@ -52,7 +56,7 @@ module Webrat
end end
def wait_for_ajax(timeout = 15000) def wait_for_ajax(timeout = 15000)
@selenium.wait_for_condition "window.Ajax.activeRequestCount == 0", timeout @selenium.wait_for_condition "Ajax.activeRequestCount == 0", timeout
end end
def wait_for_effects(timeout = 15000) def wait_for_effects(timeout = 15000)
@ -83,86 +87,14 @@ module Webrat
@selenium.check("webrat=#{label_text}") @selenium.check("webrat=#{label_text}")
end end
protected protected
def define_location_strategies def define_location_strategies
@selenium.add_location_strategy('label', <<-JS) Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
var allLabels = inDocument.getElementsByTagName("label"); strategy_js = File.read(file)
var candidateLabels = $A(allLabels).select(function(candidateLabel){ strategy_name = File.basename(file, '.js')
var regExp = new RegExp('^' + locator + '\\\\b', 'i'); @selenium.add_location_strategy(strategy_name, strategy_js)
var labelText = getText(candidateLabel).strip(); end
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('webratlinkwithin', <<-JS)
var locatorParts = locator.split('|');
var cssAncestor = locatorParts[0];
var linkText = locatorParts[1];
var matchingElements = cssQuery(cssAncestor, inDocument);
var candidateLinks = matchingElements.collect(function(ancestor){
var links = ancestor.getElementsByTagName('a');
return $A(links).select(function(candidateLink) {
return PatternMatcher.matches(linkText, getText(candidateLink));
});
}).flatten().compact();
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 end
end end

View File

@ -0,0 +1,13 @@
require 'webrat/rack/rack_session'
require 'sinatra'
require 'sinatra/test/methods'
module Webrat
class SinatraSession < RackSession
include Sinatra::Test::Methods
def get(*args)
get_it(*args)
end
end
end

View File

@ -165,6 +165,21 @@ describe "clicks_button" do
@session.clicks_button @session.clicks_button
end end
it "should not send disabled field values" do
@session.response_body = <<-EOS
<form method="get" action="/login">
<input disabled id="user_email" name="user[email]" value="test@example.com" type="text" />
<input disabled id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input disabled id="user_gender_female" name="user[gender]" type="radio" value="F" checked="checked" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
EOS
@session.should_receive(:get).with("/login", {})
@session.clicks_button
end
it "should send default checked fields" do it "should send default checked fields" do
@session.response_body = <<-EOS @session.response_body = <<-EOS
<form method="get" action="/login"> <form method="get" action="/login">
@ -344,6 +359,16 @@ describe "clicks_button" do
@session.clicks_button @session.clicks_button
end end
it "should find buttons by their IDs" do
@session.response_body = <<-EOS
<form action="/">
<input type="submit" id="my_button" />
</form>
EOS
@session.should_receive(:get)
@session.clicks_button "my_button"
end
it "should find image buttons by their alt text" do it "should find image buttons by their alt text" do
@session.response_body = <<-EOS @session.response_body = <<-EOS
<form action="/"> <form action="/">

View File

@ -0,0 +1,73 @@
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

@ -0,0 +1,73 @@
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,5 +1,6 @@
require "rubygems" require "rubygems"
require "spec" require "spec"
require "spec/interop/test"
# gem install redgreen for colored test output # gem install redgreen for colored test output
begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end

35
webrat.gemspec Normal file
View File

@ -0,0 +1,35 @@
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