merged in current webrat master

This commit is contained in:
gaffo 2008-11-14 00:01:27 -06:00
commit 3fbf5ae930
31 changed files with 396 additions and 150 deletions

4
.gitignore vendored
View File

@ -4,3 +4,7 @@ pkg
doc
ri
email.txt
.svn
log
.project
.loadpath

View File

@ -2,6 +2,7 @@
* Minor enhancements
* Allow clicking links by id and id regexp (gaffo)
* Raise Webrat::PageLoadError when a failure occurs so that application exceptions can be more accurately tested (Ryan Briones)
== 0.3.2 / 2008-11-08

View File

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

View File

@ -1 +1 @@
puts IO.read(File.join(File.dirname(__FILE__), 'README'))
puts IO.read(File.join(File.dirname(__FILE__), 'README.txt'))

View File

@ -13,16 +13,16 @@ module Webrat
end
end
# We need Nokogiri's CSS to XPath support, even if using REXML
# We need Nokogiri's CSS to XPath support, even if using REXML and Hpricot for parsing and searching
require "nokogiri/css"
# Require nokogiri and fall back on rexml
# Require nokogiri and fall back on rexml+Hpricot
begin
require "nokogiri"
require "webrat/core/nokogiri"
rescue LoadError => e
require "hpricot"
require "rexml/document"
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
end
require "webrat/core"

View File

@ -1,4 +1,5 @@
require "webrat/core/configuration"
require "webrat/core/xml"
require "webrat/core/nokogiri"
require "webrat/core/logging"
require "webrat/core/flunk"

View File

@ -111,7 +111,7 @@ module Webrat
end
unless id.blank?
@label_elements += @form.element.search("label[@for='#{id}']")
@label_elements += Webrat::XML.css_search(@form.element, "label[@for='#{id}']")
end
@label_elements
@ -311,8 +311,8 @@ module Webrat
protected
def default_value
selected_options = @element.search(".//option[@selected='selected']")
selected_options = @element.search(".//option[position() = 1]") if selected_options.empty?
selected_options = Webrat::XML.css_search(@element, "option[@selected='selected']")
selected_options = Webrat::XML.css_search(@element, "option:first") if selected_options.empty?
selected_options.map do |option|
return "" if option.nil?
@ -325,7 +325,7 @@ module Webrat
end
def option_elements
@element.search(".//option")
Webrat::XML.css_search(@element, "option")
end
end

View File

@ -39,7 +39,7 @@ module Webrat
def fields
return @fields if @fields
@fields = (@element.search(".//button", ".//input", ".//textarea", ".//select")).collect do |field_element|
@fields = Webrat::XML.css_search(@element, "button", "input", "textarea", "select").collect do |field_element|
Field.class_for_element(field_element).new(self, field_element)
end
end

View File

View File

@ -11,7 +11,11 @@ module Webrat
end
def text
@element.inner_text
str = @element.inner_text
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")
str
end
end

View File

@ -22,7 +22,7 @@ module Webrat
end
def matches_text?(link_text)
html = text.gsub(' ',' ')
html = text.gsub(' ',' ').gsub(' ', ' ')
if link_text.is_a?(Regexp)
matcher = link_text
@ -33,11 +33,22 @@ module Webrat
html =~ matcher || title =~ matcher
end
def matches_id?(id_or_regexp)
if id_or_regexp.is_a?(Regexp)
(id =~ id_or_regexp) ? true : false
else
(id == id_or_regexp) ? true : false
end
end
def text
@element.inner_html
end
protected
def id
@element['id']
end
def data
authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}
@ -54,7 +65,7 @@ module Webrat
def absolute_href
if href =~ /^\?/
"#{@session.current_url}#{href}"
elsif href !~ %r{^https?://www.example.com(/.*)} && (href !~ /^\//)
elsif href !~ %r{^https?://} && (href !~ /^\//)
"#{@session.current_url}/#{href}"
else
href

View File

@ -76,15 +76,15 @@ module Webrat
flunk("Could not find area with name #{area_name}")
end
def find_link(text) #:nodoc:
def find_link(text_or_title_or_id) #:nodoc:
matching_links = links.select do |possible_link|
possible_link.matches_text?(text)
possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id)
end
if matching_links.any?
matching_links.min { |a, b| a.text.length <=> b.text.length }
else
flunk("Could not find link with text #{text.inspect}")
flunk("Could not find link with text or title or id #{text_or_title_or_id.inspect}")
end
end

View File

@ -7,15 +7,7 @@ module Webrat
end
def matches?(stringlike)
if defined?(Nokogiri::XML)
matches_nokogiri?(stringlike)
else
matches_rexml?(stringlike)
end
end
def matches_rexml?(stringlike)
@document = rexml_document(stringlike)
@document = Webrat::XML.document(stringlike)
@element = @document.inner_text
case @content
@ -25,39 +17,6 @@ module Webrat
@element.match(@content)
end
end
def matches_nokogiri?(stringlike)
@document = Webrat.nokogiri_document(stringlike)
@element = @document.inner_text
case @content
when String
@element.include?(@content)
when Regexp
@element.match(@content)
end
end
def rexml_document(stringlike)
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
case stringlike
when REXML::Document
stringlike.root
when REXML::Node
stringlike
when StringIO, String
begin
REXML::Document.new(stringlike.to_s).root
rescue REXML::ParseException => e
if e.message.include?("second root element")
REXML::Document.new("<fake-root-element>#{stringlike}</fake-root-element>").root
else
raise e
end
end
end
end
# ==== Returns
# String:: The failure message.

View File

@ -45,7 +45,7 @@ module Webrat
@query = query
end
@document = Webrat.nokogiri_document(stringlike)
@document = Webrat::XML.document(stringlike)
matched = @document.xpath(*@query)
matched.any? && (!@block || @block.call(matched))
end

View File

@ -23,21 +23,21 @@ module Webrat
:within,
:header, :http_accept, :basic_auth,
:save_and_open_page,
:fill_in,
:check,
:uncheck,
:choose,
:select,
:attach_file,
:fills_in, :fill_in,
:checks, :check,
:unchecks, :uncheck,
:chooses, :choose,
:selects, :select,
:attaches_file, :attach_file,
:cookies,
:response,
:current_page,
:current_url,
:click_link,
:click_area,
:click_button,
:clicks_link, :click_link,
:clicks_area, :click_area,
:clicks_button, :click_button,
:reload, :reloads,
:clicks_link_within,
:clicks_link_within, :click_link_within,
:field_labeled
end

View File

@ -123,14 +123,22 @@ module Webrat
# Passing a :method in the options hash overrides the HTTP method used
# for making the link request
#
# It will try to find links by (in order of precedence):
# innerHTML, with simple &nbsp; handling
# title
# id
#
# innerHTML and title are matchable by text subtring or Regexp
# id is matchable by full text equality or Regexp
#
# Example:
# click_link "Sign up"
#
# click_link "Sign up", :javascript => false
#
# click_link "Sign up", :method => :put
def click_link(link_text, options = {})
find_link(link_text).click(options)
def click_link(text_or_title_or_id, options = {})
find_link(text_or_title_or_id).click(options)
end
alias_method :clicks_link, :click_link
@ -166,13 +174,13 @@ module Webrat
def page_dom #:nodoc:
return @response.dom if @response.respond_to?(:dom)
dom = Webrat.nokogiri_document(@response_body)
dom = Webrat::XML.document(@response_body)
Webrat.define_dom_method(@response, dom)
return dom
end
def scoped_dom #:nodoc:
Webrat.nokogiri_document(@scope.dom.search(@selector).first.to_html)
Webrat::XML.document(@scope.dom.search(@selector).first.to_html)
end
def locate_field(field_locator, *field_types) #:nodoc:
@ -184,13 +192,13 @@ module Webrat
end
def areas #:nodoc:
dom.search("area").map do |element|
Webrat::XML.css_search(dom, "area").map do |element|
Area.new(@session, element)
end
end
def links #:nodoc:
dom.search("a[@href]").map do |link_element|
Webrat::XML.css_search(dom, "a[@href]").map do |link_element|
Link.new(@session, link_element)
end
end
@ -198,7 +206,7 @@ module Webrat
def forms #:nodoc:
return @forms if @forms
@forms = dom.search("form").map do |form_element|
@forms = Webrat::XML.css_search(dom, "form").map do |form_element|
Form.new(@session, form_element)
end
end

View File

@ -152,8 +152,13 @@ module Webrat
alias_method :visits, :visit
def open_in_browser(path) #:nodoc
`open #{path}`
def open_in_browser(path) # :nodoc
platform = ruby_platform
if platform =~ /cygwin/ || platform =~ /win32/
`rundll32 url.dll,FileProtocolHandler #{path.gsub("/", "\\\\")}`
elsif platform =~ /darwin/
`open #{path}`
end
end
def rewrite_css_and_image_references(response_html) #:nodoc
@ -186,5 +191,11 @@ module Webrat
def_delegators :current_scope, :should_see
def_delegators :current_scope, :should_not_see
def_delegators :current_scope, :field_labeled
private
# accessor for testing
def ruby_platform
RUBY_PLATFORM
end
end
end

35
lib/webrat/core/xml.rb Normal file
View File

@ -0,0 +1,35 @@
module Webrat
module XML
def self.document(stringlike)
if defined?(Nokogiri::XML)
Webrat.nokogiri_document(stringlike)
else
return stringlike.dom if stringlike.respond_to?(:dom)
if Hpricot::Doc === stringlike
stringlike
elsif Hpricot::Elements === stringlike
stringlike
elsif StringIO === stringlike
Hpricot(stringlike.string)
elsif stringlike.respond_to?(:body)
Hpricot(stringlike.body.to_s)
else
Hpricot(stringlike.to_s)
end
end
end
def self.css_search(element, *searches)
if defined?(Nokogiri::XML)
element.css(*searches)
else
searches.map do |search|
element.search(search)
end.flatten.compact
end
end
end
end

View File

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

13
lib/webrat/rspec-rails.rb Normal file
View File

@ -0,0 +1,13 @@
# Supports using the matchers in controller, helper, and view specs if you're
# using rspec-rails. Just add a require statement to spec/spec_helper.rb:
#
# require 'webrat/rspec-rails'
#
require "webrat/core/matchers"
Spec::Runner.configure do |config|
# rspec should support :type => [:controller, :helper, :view] - but until it does ...
config.include(Webrat::Matchers, :type => :controller)
config.include(Webrat::Matchers, :type => :helper)
config.include(Webrat::Matchers, :type => :view)
end

View File

@ -109,7 +109,22 @@ module Webrat
def dragdrop(*args) #:nodoc:
@selenium.dragdrop(*args)
end
def fire_event(field_identifier, event)
locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.fire_event(locator, "#{event}")
end
def key_down(field_identifier, key_code)
locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.key_down(locator, key_code)
end
def key_up(field_identifier, key_code)
locator = "webrat=#{Regexp.escape(field_identifier)}"
@selenium.key_up(locator, key_code)
end
protected
def adjust_if_regexp(text_or_regexp) #:nodoc:

View File

@ -21,6 +21,14 @@ describe "click_link" do
@session.click_link "Link text", :method => :get
end
it "should click link on substring" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "ink tex", :method => :get
end
it "should click delete links" do
@session.response_body = <<-EOS
<a href="/page">Link text</a>
@ -54,6 +62,22 @@ describe "click_link" do
@session.click_link /link [a-z]/i
end
it "should click links by id" do
@session.response_body = <<-EOS
<a id="link_text_link" href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link "link_text_link"
end
it "should click links by id regexp" do
@session.response_body = <<-EOS
<a id="link_text_link" href="/page">Link text</a>
EOS
@session.should_receive(:get).with("/page", {})
@session.clicks_link /_text_/
end
it "should click rails javascript links with authenticity tokens" do
@session.response_body = <<-EOS
<a href="/posts" onclick="var f = document.createElement('form');
@ -270,6 +294,15 @@ describe "click_link" do
end
it "should follow fully qualified local links" do
@session.stub!(:current_url).and_return("/page")
@session.response_body = <<-EOS
<a href="http://subdomain.example.com/page/sub">Jump to sub page</a>
EOS
@session.should_receive(:get).with("http://subdomain.example.com/page/sub", {})
@session.click_link "Jump to sub page"
end
it "should follow fully qualified local links to example.com" do
@session.response_body = <<-EOS
<a href="http://www.example.com/page/sub">Jump to sub page</a>
EOS

View File

@ -108,4 +108,21 @@ describe "field_labeled" do
with_an_id_of "element_42", :for => "The Label"
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
end
describe "finding a field with it's label containing newlines" do
using_this_html <<-HTML
<form>
<label for="element_42">
A label with
<a>a link on it's own line</a>
</label>
<input type="text" id="element_42">
</form>
HTML
should_return_a Webrat::TextField, :for => "A label with a link on it's own line"
with_an_id_of "element_42", :for => "A label with a link on it's own line"
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
end
end

View File

@ -1,5 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
require "merb"
describe "selects" do
before do
@session = Webrat::TestSession.new

View File

@ -0,0 +1,2 @@
--diff
--color

View File

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

79
spec/webrat/core/link_spec.rb Executable file
View File

@ -0,0 +1,79 @@
require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe Webrat::Link do
# include Webrat::Link
before do
@session = mock(Webrat::TestSession)
end
it "should pass through relative urls" do
link = Webrat::Link.new(@session, {"href" => "/path"})
@session.should_receive(:request_page).with("/path", :get, {})
link.click
end
it "shouldnt put base url onto " do
url = "https://www.example.com/path"
@session.should_receive(:request_page).with(url, :get, {})
link = Webrat::Link.new(@session, {"href" => url})
link.click
end
it "should matches_text? on regexp" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.matches_text?(/link/i).should == 0
end
it "should matches_text? on link_text" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.matches_text?("Link Text").should == 0
end
it "should matches_text? on substring" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link Text")
link.should_receive(:title).and_return(nil)
link.matches_text?("link_text").should == false
end
it "should match text including &nbsp;" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Link&nbsp;Text")
link.matches_text?("Link Text").should == 0
end
it "should not matches_text? on wrong text" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:text).and_return("Some Other Link")
link.should_receive(:title).and_return(nil)
link.matches_text?("Link Text").should == false
end
it "should matches_id? on exact matching id" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:id).and_return("some_id")
link.matches_id?("some_id").should == true
end
it "should not matches_id? on incorrect id" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:id).and_return("other_id")
link.matches_id?("some_id").should == false
end
it "should matches_id? on matching id by regexp" do
link = Webrat::Link.new(@session, nil)
link.should_receive(:id).and_return("some_id")
link.matches_id?(/some/).should == true
end
end

View File

@ -18,15 +18,30 @@ describe Webrat::Session do
"<html></html>"
end
session.current_dom.should be_an_instance_of(Nokogiri::HTML::Document)
session.current_dom.should respond_to(:search)
end
it "should open the page in the browser" do
it "should open the page in the browser in MacOSX" do
session = Webrat::Session.new
session.should_receive(:ruby_platform).and_return 'darwin'
session.should_receive(:`).with("open path")
session.open_in_browser("path")
end
it "should open the page in the browser in cygwin" do
session = Webrat::Session.new
session.should_receive(:ruby_platform).and_return 'i386-cygwin'
session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file")
session.open_in_browser("path/to/file")
end
it "should open the page in the browser in Win32" do
session = Webrat::Session.new
session.should_receive(:ruby_platform).and_return 'win32'
session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file")
session.open_in_browser("path/to/file")
end
it "should provide a current_page for backwards compatibility" do
session = Webrat::Session.new
current_page = session.current_page

View File

@ -12,4 +12,25 @@ describe Webrat::MechanizeSession do
@mech.headers.should == {}
end
end
describe "post" do
def url
'http://test.host/users'
end
def data
{:user => {:first_name => 'Nancy', :last_name => 'Callahan'}}
end
def flattened_data
{'user[first_name]' => 'Nancy', 'user[last_name]' => 'Callahan'}
end
it "should flatten model post data" do
mechanize = mock :mechanize
mechanize.should_receive(:post).with(url, flattened_data)
Webrat::MechanizeSession.new(mechanize).post(url, data)
end
end
end

View File

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

View File

@ -57,10 +57,11 @@ describe Webrat::RailsSession do
end
context "the URL is https://" do
it "should call #https! with true before the request" do
integration_session = mock("integration session", :request_via_redirect => nil)
it "should call #https! with true before the request and just pass on the path" do
integration_session = mock("integration session")
rails_session = Webrat::RailsSession.new(integration_session)
integration_session.should_receive(:https!).with(true)
integration_session.should_receive(:request_via_redirect).with(:get, "/url", "data", "headers")
rails_session.get("https://www.example.com/url", "data", "headers")
end
end