Merge commit 'gaffo/master'

Conflicts:
	.gitignore
	History.txt
	lib/webrat/core/scope.rb
	spec/webrat/rails/rails_session_spec.rb
This commit is contained in:
Bryan Helmkamp 2008-11-14 00:04:53 -05:00
commit f29cb53239
8 changed files with 134 additions and 8 deletions

2
.gitignore vendored
View File

@ -6,3 +6,5 @@ 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

@ -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}

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

@ -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

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');

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

@ -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