Use Webrat::Methods in specs

This commit is contained in:
Bryan Helmkamp 2008-11-23 00:22:49 -05:00
parent adf68c2f8f
commit f03905e6b6
25 changed files with 825 additions and 854 deletions

View File

@ -3,6 +3,9 @@ require "webrat/core_extensions/blank"
require "webrat/core_extensions/nil_to_param" require "webrat/core_extensions/nil_to_param"
module Webrat module Webrat
class DisabledFieldError < WebratError
end
class Field #:nodoc: class Field #:nodoc:
def self.class_for_element(element) def self.class_for_element(element)
@ -64,7 +67,8 @@ module Webrat
end end
def raise_error_if_disabled def raise_error_if_disabled
raise "Cannot interact with disabled form element (#{self})" if disabled? return unless disabled?
raise DisabledFieldError.new("Cannot interact with disabled form element (#{self})")
end end
def to_param def to_param

View File

@ -5,8 +5,11 @@ module Webrat
meths.each do |meth| meths.each do |meth|
self.class_eval <<-RUBY self.class_eval <<-RUBY
def #{meth}(*args, &blk) def #{meth}(*args, &blk)
webrat_session.#{meth}(*args, &blk)
end
def webrat_session
@_webrat_session ||= ::Webrat.session_class.new(self) @_webrat_session ||= ::Webrat.session_class.new(self)
@_webrat_session.#{meth}(*args, &blk)
end end
RUBY RUBY
end end

View File

@ -2,22 +2,21 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "Basic Auth HTTP headers" do describe "Basic Auth HTTP headers" do
before do before do
@session = Webrat::TestSession.new basic_auth('user', 'secret')
@session.basic_auth('user', 'secret')
end end
it "should be present in visit" do it "should be present in visit" do
@session.should_receive(:get).with("/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) webrat_session.should_receive(:get).with("/", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
@session.visit("/") visit("/")
end end
it "should be present in form submits" do it "should be present in form submits" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/form1"> <form method="post" action="/form1">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) webrat_session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"})
@session.click_button click_button
end end
end end

View File

@ -1,136 +1,142 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "check" do describe "check" do
before do
@session = Webrat::TestSession.new
end
it "should fail if no checkbox found" do it "should fail if no checkbox found" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <html>
</form> <form method="post" action="/login">
EOS </form>
</html>
HTML
lambda { @session.check "remember_me" }.should raise_error lambda { check "remember_me" }.should raise_error(Webrat::NotFoundError)
end end
it "should fail if input is not a checkbox" do it "should fail if input is not a checkbox" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <html>
<input type="text" name="remember_me" /> <form method="post" action="/login">
</form> <input type="text" name="remember_me" />
EOS </form>
</html>
HTML
lambda { @session.check "remember_me" }.should raise_error lambda { check "remember_me" }.should raise_error(Webrat::NotFoundError)
end end
it "should check rails style checkboxes" do it "should check rails style checkboxes" do
@session.response_body = <<-EOS with_html <<-HTML
<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" />
<label for="user_tos">TOS</label> <label for="user_tos">TOS</label>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
@session.check "TOS" webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
@session.click_button check "TOS"
click_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
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <html>
<input type="checkbox" name="remember_me" /> <form method="post" action="/login">
<input type="submit" /> <input type="checkbox" name="remember_me" />
</form> <input type="submit" />
EOS </form>
@session.should_receive(:post).with("/login", "remember_me" => "on") </html>
@session.check "remember_me" HTML
@session.click_button
webrat_session.should_receive(:post).with("/login", "remember_me" => "on")
check "remember_me"
click_button
end end
it "should fail if the checkbox is disabled" do it "should fail if the checkbox is disabled" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <html>
<input type="checkbox" name="remember_me" disabled="disabled" /> <form method="post" action="/login">
<input type="submit" /> <input type="checkbox" name="remember_me" disabled="disabled" />
</form> <input type="submit" />
EOS </form>
lambda { @session.check "remember_me" }.should raise_error </html>
HTML
lambda { check "remember_me" }.should raise_error(Webrat::DisabledFieldError)
end end
it "should result in a custom value being posted" do it "should result in a custom value being posted" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <html>
<input type="checkbox" name="remember_me" value="yes" /> <form method="post" action="/login">
<input type="submit" /> <input type="checkbox" name="remember_me" value="yes" />
</form> <input type="submit" />
EOS </form>
@session.should_receive(:post).with("/login", "remember_me" => "yes") </html>
@session.check "remember_me" HTML
@session.click_button
webrat_session.should_receive(:post).with("/login", "remember_me" => "yes")
check "remember_me"
click_button
end end
end end
describe "uncheck" do describe "uncheck" do
before do
@session = Webrat::TestSession.new
end
it "should fail if no checkbox found" do it "should fail if no checkbox found" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
</form> </form>
EOS HTML
lambda { @session.uncheck "remember_me" }.should raise_error lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError)
end end
it "should fail if input is not a checkbox" do it "should fail if input is not a checkbox" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<input type="text" name="remember_me" /> <input type="text" name="remember_me" />
</form> </form>
EOS HTML
lambda { @session.uncheck "remember_me" }.should raise_error lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError)
end end
it "should fail if the checkbox is disabled" do it "should fail if the checkbox is disabled" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<input type="checkbox" name="remember_me" checked="checked" disabled="disabled" /> <input type="checkbox" name="remember_me" checked="checked" disabled="disabled" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
lambda { @session.uncheck "remember_me" }.should raise_error lambda { uncheck "remember_me" }.should raise_error(Webrat::DisabledFieldError)
end end
it "should uncheck rails style checkboxes" do it "should uncheck rails style checkboxes" do
@session.response_body = <<-EOS with_html <<-HTML
<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" />
<label for="user_tos">TOS</label> <label for="user_tos">TOS</label>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", "user" => {"tos" => "0"}) webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
@session.check "TOS" check "TOS"
@session.uncheck "TOS" uncheck "TOS"
@session.click_button click_button
end end
it "should result in value not being posted" do it "should result in value not being posted" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", {}) webrat_session.should_receive(:post).with("/login", {})
@session.uncheck "remember_me" uncheck "remember_me"
@session.click_button click_button
end end
end end

View File

@ -1,31 +1,27 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "choose" do describe "choose" do
before do
@session = Webrat::TestSession.new
end
it "should fail if no radio buttons found" do it "should fail if no radio buttons found" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
</form> </form>
EOS HTML
lambda { @session.choose "first option" }.should raise_error lambda { choose "first option" }.should raise_error(Webrat::NotFoundError)
end end
it "should fail if input is not a radio button" do it "should fail if input is not a radio button" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<input type="text" name="first_option" /> <input type="text" name="first_option" />
</form> </form>
EOS HTML
lambda { @session.choose "first_option" }.should raise_error lambda { choose "first_option" }.should raise_error(Webrat::NotFoundError)
end end
it "should check rails style radio buttons" do it "should check rails style radio buttons" do
@session.response_body = <<-EOS with_html <<-HTML
<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>
@ -33,14 +29,14 @@ describe "choose" do
<label for="user_gender_female">Female</label> <label for="user_gender_female">Female</label>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", "user" => {"gender" => "M"}) webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "M"})
@session.choose "Male" choose "Male"
@session.click_button click_button
end end
it "should only submit last chosen value" do it "should only submit last chosen value" do
@session.response_body = <<-EOS with_html <<-HTML
<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>
@ -48,49 +44,49 @@ describe "choose" do
<label for="user_gender_female">Female</label> <label for="user_gender_female">Female</label>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", "user" => {"gender" => "M"}) webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "M"})
@session.choose "Female" choose "Female"
@session.choose "Male" choose "Male"
@session.click_button click_button
end end
it "should fail if the radio button is disabled" do it "should fail if the radio button is disabled" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<input type="radio" name="first_option" disabled="disabled" /> <input type="radio" name="first_option" disabled="disabled" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
lambda { @session.choose "first_option" }.should raise_error lambda { choose "first_option" }.should raise_error(Webrat::DisabledFieldError)
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
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "first_option" => "on") webrat_session.should_receive(:post).with("/login", "first_option" => "on")
@session.choose "first_option" choose "first_option"
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "first_option" => "on") webrat_session.should_receive(:post).with("/login", "first_option" => "on")
@session.click_button click_button
end end
it "should result in the value of the selected radio button being posted when a subsequent one is checked by default" do it "should result in the value of the selected radio button being posted when a subsequent one is checked by default" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" 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>
@ -98,9 +94,9 @@ describe "choose" do
<label for="user_gender_female">Female</label> <label for="user_gender_female">Female</label>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", "user" => {"gender" => "M"}) webrat_session.should_receive(:post).with("/login", "user" => {"gender" => "M"})
@session.choose "Male" choose "Male"
@session.click_button click_button
end end
end end

View File

@ -1,93 +1,89 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "click_area" do describe "click_area" do
before do
@session = Webrat::TestSession.new
end
it "should use get by default" do it "should use get by default" do
@session.response_body = <<-EOS with_html <<-HTML
<map name="map_de" id="map_de"> <map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" /> <area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map> </map>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_area "Berlin" click_area "Berlin"
end end
it "should assert valid response" do it "should assert valid response" do
@session.response_body = <<-EOS with_html <<-HTML
<map name="map_de" id="map_de"> <map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" /> <area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map> </map>
EOS HTML
@session.response_code = 501 webrat_session.response_code = 501
lambda { @session.click_area "Berlin" }.should raise_error lambda { click_area "Berlin" }.should raise_error(Webrat::PageLoadError)
end end
[200, 300, 400, 499].each do |status| [200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do it "should consider the #{status} status code as success" do
@session.response_body = <<-EOS with_html <<-HTML
<map name="map_de" id="map_de"> <map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" /> <area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map> </map>
EOS HTML
@session.response_code = status webrat_session.response_code = status
lambda { @session.click_area "Berlin" }.should_not raise_error lambda { click_area "Berlin" }.should_not raise_error
end end
end end
it "should fail if the area doesn't exist" do it "should fail if the area doesn't exist" do
@session.response_body = <<-EOS with_html <<-HTML
<map name="map_de" id="map_de"> <map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" /> <area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map> </map>
EOS HTML
lambda { lambda {
@session.click_area "Missing area" click_area "Missing area"
}.should raise_error }.should raise_error(Webrat::NotFoundError)
end end
it "should not be case sensitive" do it "should not be case sensitive" do
@session.response_body = <<-EOS with_html <<-HTML
<map name="map_de" id="map_de"> <map name="map_de" id="map_de">
<area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" /> <area href="/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map> </map>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_area "berlin" click_area "berlin"
end end
it "should follow relative links" do it "should follow relative links" do
@session.stub!(:current_url).and_return("/page") webrat_session.stub!(:current_url => "/page")
@session.response_body = <<-EOS with_html <<-HTML
<map name="map_de" id="map_de"> <map name="map_de" id="map_de">
<area href="sub" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" /> <area href="sub" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map> </map>
EOS HTML
@session.should_receive(:get).with("/page/sub", {}) webrat_session.should_receive(:get).with("/page/sub", {})
@session.click_area "Berlin" click_area "Berlin"
end end
it "should follow fully qualified local links" do it "should follow fully qualified local links" do
@session.response_body = <<-EOS with_html <<-HTML
<map name="map_de" id="map_de"> <map name="map_de" id="map_de">
<area href="http://www.example.com/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" /> <area href="http://www.example.com/page" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map> </map>
EOS HTML
@session.should_receive(:get).with("http://www.example.com/page", {}) webrat_session.should_receive(:get).with("http://www.example.com/page", {})
@session.click_area "Berlin" click_area "Berlin"
end end
it "should follow query parameters" do it "should follow query parameters" do
@session.response_body = <<-EOS with_html <<-HTML
<map name="map_de" id="map_de"> <map name="map_de" id="map_de">
<area href="/page?foo=bar" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" /> <area href="/page?foo=bar" title="Berlin" id="berlin" shape="poly" alt="Berlin" coords="180,89,180" />
</map> </map>
EOS HTML
@session.should_receive(:get).with("/page?foo=bar", {}) webrat_session.should_receive(:get).with("/page?foo=bar", {})
@session.click_area "Berlin" click_area "Berlin"
end end
end end

View File

@ -1,96 +1,92 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "click_button" do describe "click_button" do
before do
@session = Webrat::TestSession.new
end
it "should fail if no buttons" do it "should fail if no buttons" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/login"></form> <form method="get" action="/login"></form>
EOS HTML
lambda { @session.click_button }.should raise_error lambda { click_button }.should raise_error(Webrat::NotFoundError)
end end
it "should fail if input is not a submit button" do it "should fail if input is not a submit button" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/login"> <form method="get" action="/login">
<input type="reset" /> <input type="reset" />
</form> </form>
EOS HTML
lambda { @session.click_button }.should raise_error lambda { click_button }.should raise_error(Webrat::NotFoundError)
end end
it "should fail if button is disabled" do it "should fail if button is disabled" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/login"> <form method="get" action="/login">
<input type="submit" disabled="disabled" /> <input type="submit" disabled="disabled" />
</form> </form>
EOS HTML
lambda { @session.click_button }.should raise_error lambda { click_button }.should raise_error(Webrat::DisabledFieldError)
end end
it "should default to get method" do it "should default to get method" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/login"> <form action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get) webrat_session.should_receive(:get)
@session.click_button click_button
end end
it "should assert valid response" do it "should assert valid response" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/login"> <form action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.response_code = 501 webrat_session.response_code = 501
lambda { @session.click_button }.should raise_error lambda { click_button }.should raise_error(Webrat::PageLoadError)
end end
[200, 300, 400, 499].each do |status| [200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do it "should consider the #{status} status code as success" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/login"> <form action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.response_code = status webrat_session.response_code = status
lambda { @session.click_button }.should_not raise_error lambda { click_button }.should_not raise_error
end end
end end
it "should submit the first form by default" do it "should submit the first form by default" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/form1"> <form method="get" action="/form1">
<input type="submit" /> <input type="submit" />
</form> </form>
<form method="get" action="/form2"> <form method="get" action="/form2">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/form1", {}) webrat_session.should_receive(:get).with("/form1", {})
@session.click_button click_button
end end
it "should not explode on file fields" do it "should not explode on file fields" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/form1"> <form method="get" action="/form1">
<input type="file" /> <input type="file" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.click_button click_button
end end
it "should submit the form with the specified button" do it "should submit the form with the specified button" do
@session.response_body = <<-EOS with_html <<-HTML
<html> <html>
<form method="get" action="/form1"> <form method="get" action="/form1">
<input type="submit" /> <input type="submit" />
@ -99,88 +95,88 @@ describe "click_button" do
<input type="submit" value="Form2" /> <input type="submit" value="Form2" />
</form> </form>
</html> </html>
EOS HTML
@session.should_receive(:get).with("/form2", {}) webrat_session.should_receive(:get).with("/form2", {})
@session.click_button "Form2" click_button "Form2"
end end
it "should use action from form" do it "should use action from form" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/login"> <form method="get" action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", {}) webrat_session.should_receive(:get).with("/login", {})
@session.click_button click_button
end end
it "should use method from form" do it "should use method from form" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post) webrat_session.should_receive(:post)
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "login" => "Login") webrat_session.should_receive(:post).with("/login", "login" => "Login")
@session.click_button("Login") click_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
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", {}) webrat_session.should_receive(:post).with("/login", {})
@session.click_button("Login") click_button("Login")
end end
it "should send default password field values" do it "should send default password field values" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"password" => "mypass"}) webrat_session.should_receive(:get).with("/login", "user" => {"password" => "mypass"})
@session.click_button click_button
end end
it "should send default hidden field values" do it "should send default hidden field values" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"}) webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
@session.click_button click_button
end end
it "should send default text field values" do it "should send default text field values" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"}) webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
@session.click_button click_button
end end
it "should not send disabled field values" do it "should not send disabled field values" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/login"> <form method="get" action="/login">
<input disabled id="user_email" name="user[email]" value="test@example.com" type="text" /> <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" /> <input disabled id="user_gender_male" name="user[gender]" type="radio" value="M" />
@ -189,24 +185,24 @@ describe "click_button" do
<label for="user_gender_female">Female</label> <label for="user_gender_female">Female</label>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", {}) webrat_session.should_receive(:get).with("/login", {})
@session.click_button click_button
end end
it "should send default checked fields" do it "should send default checked fields" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"tos" => "1"}) webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
@session.click_button click_button
end end
it "should send default radio options" do it "should send default radio options" do
@session.response_body = <<-EOS with_html <<-HTML
<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>
@ -214,37 +210,37 @@ describe "click_button" do
<label for="user_gender_female">Female</label> <label for="user_gender_female">Female</label>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", "user" => {"gender" => "F"}) webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "F"})
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"tos" => "0"}) webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"tos" => "1"}) webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
@session.click_button click_button
end end
it "should send default collection fields" do it "should send default collection fields" do
@session.response_body = <<-EOS with_html <<-HTML
<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" />
@ -258,37 +254,37 @@ describe "click_button" do
<input type="hidden" name="response[choices][][selected]" value="two" /> <input type="hidden" name="response[choices][][selected]" value="two" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", webrat_session.should_receive(: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.click_button click_button
end end
it "should not send default unchecked fields" do it "should not send default unchecked fields" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", {}) webrat_session.should_receive(:get).with("/login", {})
@session.click_button click_button
end end
it "should send default textarea values" do it "should send default textarea values" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/posts", "post" => {"body" => "Post body here!"}) webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Post body here!"})
@session.click_button click_button
end end
it "should send default selected option value from select" do it "should send default selected option value from select" do
@session.response_body = <<-EOS with_html <<-HTML
<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>
@ -296,13 +292,13 @@ describe "click_button" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", "month" => "2") webrat_session.should_receive(:get).with("/login", "month" => "2")
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/login"> <form method="get" action="/login">
<select name="month"> <select name="month">
<option>January</option> <option>January</option>
@ -310,13 +306,13 @@ describe "click_button" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", "month" => "February") webrat_session.should_receive(:get).with("/login", "month" => "February")
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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>
@ -324,83 +320,83 @@ describe "click_button" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:get).with("/login", "month" => "1") webrat_session.should_receive(:get).with("/login", "month" => "1")
@session.click_button click_button
end end
it "should handle nested properties" do it "should handle nested properties" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}}) webrat_session.should_receive(:post).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}})
@session.click_button click_button
end end
it "should send default empty text field values" do it "should send default empty text field values" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"email" => ""}) webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
@session.click_button click_button
end end
it "should recognize button tags" do it "should recognize button tags" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"email" => ""}) webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
@session.click_button click_button
end end
it "should recognize image button tags" do it "should recognize image button tags" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/"> <form action="/">
<input type="image" /> <input type="image" />
</form> </form>
EOS HTML
@session.should_receive(:get) webrat_session.should_receive(:get)
@session.click_button click_button
end end
it "should find buttons by their IDs" do it "should find buttons by their IDs" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/"> <form action="/">
<input type="submit" id="my_button" /> <input type="submit" id="my_button" />
</form> </form>
EOS HTML
@session.should_receive(:get) webrat_session.should_receive(:get)
@session.click_button "my_button" click_button "my_button"
end 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 with_html <<-HTML
<form action="/"> <form action="/">
<input type="image" alt="Go" /> <input type="image" alt="Go" />
</form> </form>
EOS HTML
@session.should_receive(:get) webrat_session.should_receive(:get)
@session.click_button "Go" click_button "Go"
end end
it "should recognize button tags by content" do it "should recognize button tags by content" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/login", "user" => {"email" => ""}) webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
@session.click_button "Login" click_button "Login"
end end
end end

View File

@ -1,93 +1,89 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "click_link" do describe "click_link" do
before do
@session = Webrat::TestSession.new
end
it "should click links with ampertands" do it "should click links with ampertands" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Save &amp; go back</a> <a href="/page">Save &amp; go back</a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_link "Save & go back" click_link "Save & go back"
end end
it "should use get by default" do it "should use get by default" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_link "Link text" click_link "Link text"
end end
it "should click get links" do it "should click get links" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_link "Link text", :method => :get click_link "Link text", :method => :get
end end
it "should click link on substring" do it "should click link on substring" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.clicks_link "ink tex", :method => :get clicks_link "ink tex", :method => :get
end end
it "should click delete links" do it "should click delete links" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.should_receive(:delete).with("/page", {}) webrat_session.should_receive(:delete).with("/page", {})
@session.click_link "Link text", :method => :delete click_link "Link text", :method => :delete
end end
it "should click post links" do it "should click post links" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.should_receive(:post).with("/page", {}) webrat_session.should_receive(:post).with("/page", {})
@session.click_link "Link text", :method => :post click_link "Link text", :method => :post
end end
it "should click put links" do it "should click put links" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.should_receive(:put).with("/page", {}) webrat_session.should_receive(:put).with("/page", {})
@session.click_link "Link text", :method => :put click_link "Link text", :method => :put
end end
it "should click links by regexp" do it "should click links by regexp" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_link /link [a-z]/i click_link /link [a-z]/i
end end
it "should click links by id" do it "should click links by id" do
@session.response_body = <<-EOS with_html <<-HTML
<a id="link_text_link" href="/page">Link text</a> <a id="link_text_link" href="/page">Link text</a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.clicks_link "link_text_link" clicks_link "link_text_link"
end end
it "should click links by id regexp" do it "should click links by id regexp" do
@session.response_body = <<-EOS with_html <<-HTML
<a id="link_text_link" href="/page">Link text</a> <a id="link_text_link" href="/page">Link text</a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.clicks_link /_text_/ clicks_link /_text_/
end end
it "should click rails javascript links with authenticity tokens" do it "should click rails javascript links with authenticity tokens" do
@session.response_body = <<-EOS with_html <<-HTML
<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);
@ -100,13 +96,13 @@ describe "click_link" do
f.appendChild(s); f.appendChild(s);
f.submit(); f.submit();
return false;">Posts</a> return false;">Posts</a>
EOS HTML
@session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62") webrat_session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
@session.click_link "Posts" click_link "Posts"
end end
it "should click rails javascript delete links" do it "should click rails javascript delete links" do
@session.response_body = <<-EOS with_html <<-HTML
<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);
@ -119,13 +115,13 @@ describe "click_link" do
f.appendChild(m); f.appendChild(m);
f.submit(); f.submit();
return false;">Delete</a> return false;">Delete</a>
EOS HTML
@session.should_receive(:delete).with("/posts/1", {}) webrat_session.should_receive(:delete).with("/posts/1", {})
@session.click_link "Delete" click_link "Delete"
end end
it "should click rails javascript post links" do it "should click rails javascript post links" do
@session.response_body = <<-EOS with_html <<-HTML
<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);
@ -133,13 +129,13 @@ describe "click_link" do
f.action = this.href; f.action = this.href;
f.submit(); f.submit();
return false;">Posts</a> return false;">Posts</a>
EOS HTML
@session.should_receive(:post).with("/posts", {}) webrat_session.should_receive(:post).with("/posts", {})
@session.click_link "Posts" click_link "Posts"
end end
it "should click rails javascript post links without javascript" do it "should click rails javascript post links without javascript" do
@session.response_body = <<-EOS with_html <<-HTML
<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);
@ -147,13 +143,13 @@ describe "click_link" do
f.action = this.href; f.action = this.href;
f.submit(); f.submit();
return false;">Posts</a> return false;">Posts</a>
EOS HTML
@session.should_receive(:get).with("/posts", {}) webrat_session.should_receive(:get).with("/posts", {})
@session.click_link "Posts", :javascript => false click_link "Posts", :javascript => false
end end
it "should click rails javascript put links" do it "should click rails javascript put links" do
@session.response_body = <<-EOS with_html <<-HTML
<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);
@ -166,13 +162,13 @@ describe "click_link" do
f.appendChild(m); f.appendChild(m);
f.submit(); f.submit();
return false;">Put</a></h2> return false;">Put</a></h2>
EOS HTML
@session.should_receive(:put).with("/posts", {}) webrat_session.should_receive(:put).with("/posts", {})
@session.click_link "Put" click_link "Put"
end end
it "should fail if the javascript link doesn't have a value for the _method input" do it "should fail if the javascript link doesn't have a value for the _method input" do
@session.response_body = <<-EOS with_html <<-HTML
<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);
@ -184,163 +180,164 @@ describe "click_link" do
f.appendChild(m); f.appendChild(m);
f.submit(); f.submit();
return false;">Link</a> return false;">Link</a>
EOS HTML
lambda { lambda {
@session.click_link "Link" click_link "Link"
}.should raise_error }.should raise_error
end end
it "should assert valid response" do it "should assert valid response" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.response_code = 501 webrat_session.response_code = 501
lambda { @session.click_link "Link text" }.should raise_error lambda { click_link "Link text" }.should raise_error(Webrat::PageLoadError)
end end
[200, 300, 400, 499].each do |status| [200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do it "should consider the #{status} status code as success" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.response_code = status webrat_session.response_code = status
lambda { @session.click_link "Link text" }.should_not raise_error lambda { click_link "Link text" }.should_not raise_error
end end
end end
it "should fail is the link doesn't exist" do it "should fail is the link doesn't exist" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
lambda { lambda {
@session.click_link "Missing link" click_link "Missing link"
}.should raise_error }.should raise_error
end end
it "should not be case sensitive" do it "should not be case sensitive" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page">Link text</a> <a href="/page">Link text</a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_link "LINK TEXT" click_link "LINK TEXT"
end end
it "should match link substrings" do it "should match link substrings" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_link "Link text" click_link "Link text"
end end
it "should work with elements in the link" do it "should work with elements in the link" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page"><span>Link text</span></a> <a href="/page"><span>Link text</span></a>
EOS HTML
@session.should_receive(:get).with("/page", {}) webrat_session.should_receive(:get).with("/page", {})
@session.click_link "Link text" click_link "Link text"
end end
it "should match the first matching link" do it "should match the first matching link" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:get).with("/page1", {}) webrat_session.should_receive(:get).with("/page1", {})
@session.click_link "Link text" click_link "Link text"
end end
it "should choose the shortest link text match" do it "should choose the shortest link text match" do
@session.response_body = <<-EOS with_html <<-HTML
<html> <html>
<a href="/page1">Linkerama</a> <a href="/page1">Linkerama</a>
<a href="/page2">Link</a> <a href="/page2">Link</a>
</html> </html>
EOS HTML
@session.should_receive(:get).with("/page2", {}) webrat_session.should_receive(:get).with("/page2", {})
@session.click_link "Link" click_link "Link"
end end
it "should treat non-breaking spaces as spaces" do it "should treat non-breaking spaces as spaces" do
@session.response_body = <<-EOS with_html <<-HTML
<html> <html>
<a href="/page1">This&nbsp;is&nbsp;a&nbsp;link</a> <a href="/page1">This&nbsp;is&nbsp;a&nbsp;link</a>
</html> </html>
EOS HTML
@session.should_receive(:get).with("/page1", {}) webrat_session.should_receive(:get).with("/page1", {})
@session.click_link "This is a link" click_link "This is a link"
end end
it "should not match on non-text contents" do it "should not match on non-text contents" do
pending "needs fix" pending "needs fix" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="/page1"><span class="location">My house</span></a> <a href="/page1"><span class="location">My house</span></a>
<a href="/page2">Location</a> <a href="/page2">Location</a>
EOS HTML
@session.should_receive(:get).with("/page2", {}) webrat_session.should_receive(:get).with("/page2", {})
@session.click_link "Location" click_link "Location"
end
end end
it "should click link within a selector" do it "should click link within a selector" do
@session.response_body = <<-EOS with_html <<-HTML
<html> <html>
<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>
</html> </html>
EOS HTML
@session.should_receive(:get).with("/page2", {}) webrat_session.should_receive(:get).with("/page2", {})
@session.click_link_within "#container", "Link" click_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
@session.response_body = <<-EOS with_html <<-HTML
<a href="#section-1">Jump to Section 1</a> <a href="#section-1">Jump to Section 1</a>
EOS HTML
# Don't know why @session.should_receive(:get).never doesn't work here # Don't know why webrat_session.should_receive(:get).never doesn't work here
@session.should_receive(:send).with('get_via_redirect', '#section-1', {}).never webrat_session.should_receive(:send).with('get_via_redirect', '#section-1', {}).never
@session.click_link "Jump to Section 1" click_link "Jump to Section 1"
end end
it "should follow relative links" do it "should follow relative links" do
@session.stub!(:current_url).and_return("/page") webrat_session.stub!(:current_url => "/page")
@session.response_body = <<-EOS with_html <<-HTML
<a href="sub">Jump to sub page</a> <a href="sub">Jump to sub page</a>
EOS HTML
@session.should_receive(:get).with("/page/sub", {}) webrat_session.should_receive(:get).with("/page/sub", {})
@session.click_link "Jump to sub page" click_link "Jump to sub page"
end end
it "should follow fully qualified local links" do it "should follow fully qualified local links" do
@session.stub!(:current_url).and_return("/page") webrat_session.stub!(:current_url => "/page")
@session.response_body = <<-EOS with_html <<-HTML
<a href="http://subdomain.example.com/page/sub">Jump to sub page</a> <a href="http://subdomain.example.com/page/sub">Jump to sub page</a>
EOS HTML
@session.should_receive(:get).with("http://subdomain.example.com/page/sub", {}) webrat_session.should_receive(:get).with("http://subdomain.example.com/page/sub", {})
@session.click_link "Jump to sub page" click_link "Jump to sub page"
end end
it "should follow fully qualified local links to example.com" do it "should follow fully qualified local links to example.com" do
@session.response_body = <<-EOS with_html <<-HTML
<a href="http://www.example.com/page/sub">Jump to sub page</a> <a href="http://www.example.com/page/sub">Jump to sub page</a>
EOS HTML
@session.should_receive(:get).with("http://www.example.com/page/sub", {}) webrat_session.should_receive(:get).with("http://www.example.com/page/sub", {})
@session.click_link "Jump to sub page" click_link "Jump to sub page"
end end
it "should follow query parameters" do it "should follow query parameters" do
@session.stub!(:current_url).and_return("/page") webrat_session.stub!(:current_url => "/page")
@session.response_body = <<-EOS with_html <<-HTML
<a href="?foo=bar">Jump to foo bar</a> <a href="?foo=bar">Jump to foo bar</a>
EOS HTML
@session.should_receive(:get).with("/page?foo=bar", {}) webrat_session.should_receive(:get).with("/page?foo=bar", {})
@session.click_link "Jump to foo bar" click_link "Jump to foo bar"
end end
end end

View File

@ -2,36 +2,34 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "field_labeled" do describe "field_labeled" do
class << self class << self
def using_this_html html def using_this_html html
before(:each) do before(:each) do
@session = Webrat::TestSession.new with_html(html)
@session.response_body = html
end end
end end
def field_labeled label def field_labeled(label)
@label = label @label = label
yield yield
end end
def should_return_a type, opts def should_return_a type, opts
it "should return a textfield" do it "should return a textfield" do
@session.field_labeled(opts[:for]).should be_an_instance_of(type) field_labeled(opts[:for]).should be_an_instance_of(type)
end end
end end
def with_an_id_of id, opts def with_an_id_of id, opts
it "should return an element with the correct id" do it "should return an element with the correct id" do
@session.field_labeled(opts[:for]).should match_id(id) field_labeled(opts[:for]).should match_id(id)
end end
end end
def should_raise_error_matching regexp, opts def should_raise_error_matching regexp, opts
it "should raise with wrong label" do it "should raise with wrong label" do
lambda { lambda {
@session.field_labeled(opts[:for]) field_labeled(opts[:for])
}.should raise_error(regexp) }.should raise_error(regexp)
end end
end end

View File

@ -1,71 +1,67 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "fill_in" do describe "fill_in" do
before do
@session = Webrat::TestSession.new
end
it "should work with textareas" do it "should work with textareas" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "user" => {"text" => "filling text area"}) webrat_session.should_receive(:post).with("/login", "user" => {"text" => "filling text area"})
@session.fill_in "User Text", :with => "filling text area" fill_in "User Text", :with => "filling text area"
@session.click_button click_button
end end
it "should work with password fields" do it "should work with password fields" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "user" => {"text" => "pass"}) webrat_session.should_receive(:post).with("/login", "user" => {"text" => "pass"})
@session.fill_in "user_text", :with => "pass" fill_in "user_text", :with => "pass"
@session.click_button click_button
end end
it "should fail if input not found" do it "should fail if input not found" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/login"> <form method="get" action="/login">
</form> </form>
EOS HTML
lambda { @session.fill_in "Email", :with => "foo@example.com" }.should raise_error lambda { fill_in "Email", :with => "foo@example.com" }.should raise_error(Webrat::NotFoundError)
end end
it "should fail if input is disabled" do it "should fail if input is disabled" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="get" action="/login"> <form method="get" action="/login">
<label for="user_email">Email</label> <label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="text" disabled="disabled" /> <input id="user_email" name="user[email]" type="text" disabled="disabled" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
lambda { @session.fill_in "Email", :with => "foo@example.com" }.should raise_error lambda { fill_in "Email", :with => "foo@example.com" }.should raise_error(Webrat::DisabledFieldError)
end end
it "should allow overriding default form values" do it "should allow overriding default form values" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fill_in "user[email]", :with => "foo@example.com" fill_in "user[email]", :with => "foo@example.com"
@session.click_button click_button
end end
it "should choose the shortest label match" do it "should choose the shortest label match" do
@session.response_body = <<-EOS with_html <<-HTML
<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" />
@ -73,15 +69,15 @@ describe "fill_in" do
<input id="user_mail2" name="user[mail2]" type="text" /> <input id="user_mail2" name="user[mail2]" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", "user" => {"mail1" => "", "mail2" => "value"}) webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "", "mail2" => "value"})
@session.fill_in "Some", :with => "value" fill_in "Some", :with => "value"
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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" />
@ -89,37 +85,37 @@ describe "fill_in" do
<input id="user_mail2" name="user[mail2]" type="text" /> <input id="user_mail2" name="user[mail2]" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", "user" => {"mail1" => "value", "mail2" => ""}) webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "value", "mail2" => ""})
@session.fill_in "Some mail", :with => "value" fill_in "Some mail", :with => "value"
@session.click_button click_button
end end
it "should anchor label matches to start of label" do it "should anchor label matches to start of label" do
@session.response_body = <<-EOS with_html <<-HTML
<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" />
</form> </form>
EOS HTML
lambda { @session.fill_in "mail", :with => "value" }.should raise_error lambda { fill_in "mail", :with => "value" }.should raise_error
end end
it "should anchor label matches to word boundaries" do it "should anchor label matches to word boundaries" do
@session.response_body = <<-EOS with_html <<-HTML
<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" />
</form> </form>
EOS HTML
lambda { @session.fill_in "Email", :with => "value" }.should raise_error lambda { fill_in "Email", :with => "value" }.should raise_error
end end
it "should work with inputs nested in labels" do it "should work with inputs nested in labels" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<label> <label>
Email Email
@ -127,59 +123,59 @@ describe "fill_in" do
</label> </label>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fill_in "Email", :with => "foo@example.com" fill_in "Email", :with => "foo@example.com"
@session.click_button click_button
end end
it "should work with full input names" do it "should work with full input names" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fill_in "user[email]", :with => "foo@example.com" fill_in "user[email]", :with => "foo@example.com"
@session.click_button click_button
end end
it "should work if the input type is not set" do it "should work if the input type is not set" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<input id="user_email" name="user[email]" /> <input id="user_email" name="user[email]" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fill_in "user[email]", :with => "foo@example.com" fill_in "user[email]", :with => "foo@example.com"
@session.click_button click_button
end end
it "should work with symbols" do it "should work with symbols" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"})
@session.fill_in :email, :with => "foo@example.com" fill_in :email, :with => "foo@example.com"
@session.click_button click_button
end end
it "should escape field values" do it "should escape field values" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/users"> <form method="post" action="/users">
<label for="user_phone">Phone</label> <label for="user_phone">Phone</label>
<input id="user_phone" name="user[phone]" type="text" /> <input id="user_phone" name="user[phone]" type="text" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/users", "user" => {"phone" => "+1 22 33"}) webrat_session.should_receive(:post).with("/users", "user" => {"phone" => "+1 22 33"})
@session.fill_in 'Phone', :with => "+1 22 33" fill_in 'Phone', :with => "+1 22 33"
@session.click_button click_button
end end
end end

View File

@ -1,15 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "reloads" do describe "reloads" do
before do
@session = Webrat::TestSession.new
@session.response_body = "Hello world"
end
it "should reload the page with http referer" do it "should reload the page with http referer" do
@session.should_receive(:get).with("/", {}) webrat_session.should_receive(:get).with("/", {})
@session.should_receive(:get).with("/", {}, {"HTTP_REFERER"=>"/"}) webrat_session.should_receive(:get).with("/", {}, {"HTTP_REFERER"=>"/"})
@session.visit("/") visit("/")
@session.reloads reloads
end end
end end

View File

@ -2,9 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "save_and_open_page" do describe "save_and_open_page" do
before do before do
@session = Webrat::TestSession.new with_html <<-HTML
@session.response_body = <<-HTML
<html> <html>
<head> <head>
<link href="/stylesheets/foo.css" media="screen" rel="stylesheet" type="text/css" /> <link href="/stylesheets/foo.css" media="screen" rel="stylesheet" type="text/css" />
@ -16,9 +14,9 @@ describe "save_and_open_page" do
</html> </html>
HTML HTML
File.stub!(:exist?).and_return(true) File.stub!(:exist? => true)
Time.stub!(:now).and_return(1234) Time.stub!(:now => 1234)
@session.stub!(:open_in_browser) webrat_session.stub!(:open_in_browser)
@file_handle = mock("file handle") @file_handle = mock("file handle")
File.stub!(:open).with(filename, 'w').and_yield(@file_handle) File.stub!(:open).with(filename, 'w').and_yield(@file_handle)
@ -27,23 +25,23 @@ describe "save_and_open_page" do
it "should rewrite css rules" do it "should rewrite css rules" do
@file_handle.should_receive(:write) do |html| @file_handle.should_receive(:write) do |html|
html.should =~ %r|#{@session.doc_root}/stylesheets/foo.css|s html.should =~ %r|#{webrat_session.doc_root}/stylesheets/foo.css|s
end end
@session.save_and_open_page save_and_open_page
end end
it "should rewrite image paths" do it "should rewrite image paths" do
@file_handle.should_receive(:write) do |html| @file_handle.should_receive(:write) do |html|
html.should =~ %r|#{@session.doc_root}/images/bar.png|s html.should =~ %r|#{webrat_session.doc_root}/images/bar.png|s
end end
@session.save_and_open_page save_and_open_page
end end
it "should open the temp file in a browser" do it "should open the temp file in a browser" do
@session.should_receive(:open_in_browser).with(filename) webrat_session.should_receive(:open_in_browser).with(filename)
@session.save_and_open_page save_and_open_page
end end
def filename def filename

View File

@ -1,12 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "selects_date" do describe "selects_date" do
before do
@session = Webrat::TestSession.new
end
it "should send the values for each individual date component" do it "should send the values for each individual date component" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<label for="appointment_date">Date</label><br /> <label for="appointment_date">Date</label><br />
<select id="appointment_date_1i" name="appointment[date(1i)]"> <select id="appointment_date_1i" name="appointment[date(1i)]">
@ -20,15 +16,15 @@ describe "selects_date" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"})
@session.selects_date "December 25, 2003", :from => "Date" selects_date "December 25, 2003", :from => "Date"
@session.click_button click_button
end end
it "should accept a date object" do it "should accept a date object" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<label for="appointment_date">date</label><br /> <label for="appointment_date">date</label><br />
<select id="appointment_date_1i" name="appointment[date(1i)]"> <select id="appointment_date_1i" name="appointment[date(1i)]">
@ -42,15 +38,15 @@ describe "selects_date" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"})
@session.selects_date Date.parse("December 25, 2003"), :from => "date" selects_date Date.parse("December 25, 2003"), :from => "date"
@session.click_button click_button
end end
it "should work when no label is specified" do it "should work when no label is specified" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<select id="appointment_date_1i" name="appointment[date(1i)]"> <select id="appointment_date_1i" name="appointment[date(1i)]">
<option value="2003">2003</option> <option value="2003">2003</option>
@ -63,22 +59,22 @@ describe "selects_date" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"})
@session.selects_date "December 25, 2003" selects_date "December 25, 2003"
@session.click_button click_button
end end
it "should fail if the specified label is not found" do it "should fail if the specified label is not found" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/appointments"> <form method="post" action="/appointments">
<select name="month"><option>January</option></select> <select name="month"><option>January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
lambda { @session.selects_date "December 25, 2003", :from => "date" }.should raise_error lambda { selects_date "December 25, 2003", :from => "date" }.should raise_error
end end
end end

View File

@ -1,12 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "selects_datetime" do describe "selects_datetime" do
before do
@session = Webrat::TestSession.new
end
it "should send the values for each individual date and time components" do it "should send the values for each individual date and time components" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<label for="appointment_time">Time</label><br /> <label for="appointment_time">Time</label><br />
<select id="appointment_time_1i" name="appointment[time(1i)]"> <select id="appointment_time_1i" name="appointment[time(1i)]">
@ -26,15 +22,15 @@ describe "selects_datetime" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"})
@session.selects_datetime "December 25, 2003 9:30", :from => "Time" selects_datetime "December 25, 2003 9:30", :from => "Time"
@session.click_button click_button
end end
it "should accept a time object" do it "should accept a time object" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<label for="appointment_time">Time</label><br /> <label for="appointment_time">Time</label><br />
<select id="appointment_time_1i" name="appointment[time(1i)]"> <select id="appointment_time_1i" name="appointment[time(1i)]">
@ -54,15 +50,15 @@ describe "selects_datetime" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"})
@session.select_datetime Time.parse("December 25, 2003 9:30"), :from => "Time" select_datetime Time.parse("December 25, 2003 9:30"), :from => "Time"
@session.click_button click_button
end end
it "should work when no label is specified" do it "should work when no label is specified" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<select id="appointment_time_1i" name="appointment[time(1i)]"> <select id="appointment_time_1i" name="appointment[time(1i)]">
<option value="2003">2003</option> <option value="2003">2003</option>
@ -81,22 +77,22 @@ describe "selects_datetime" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"})
@session.selects_datetime "December 25, 2003 9:30" selects_datetime "December 25, 2003 9:30"
@session.click_button click_button
end end
it "should fail if the specified label is not found" do it "should fail if the specified label is not found" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/appointments"> <form method="post" action="/appointments">
<select name="month"><option>January</option></select> <select name="month"><option>January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
lambda { @session.selects_datetime "December 25, 2003 9:30", :from => "Time" }.should raise_error lambda { selects_datetime "December 25, 2003 9:30", :from => "Time" }.should raise_error
end end
end end

View File

@ -1,116 +1,112 @@
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
@session = Webrat::TestSession.new
end
it "should fail with a helpful message when option not found" do it "should fail with a helpful message when option not found" do
@session.response_body = <<-EOS with_html <<-HTML
<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>
EOS HTML
lambda { @session.selects "February", :from => "month" }.should raise_error( lambda { selects "February", :from => "month" }.should raise_error(
Exception, "The 'February' option was not found in the 'month' select box") Exception, "The 'February' option was not found in the 'month' select box")
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
@session.response_body = <<-EOS with_html <<-HTML
<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>
</form> </form>
EOS HTML
lambda { @session.selects "February", :from => "year" }.should raise_error lambda { selects "February", :from => "year" }.should raise_error
end end
it "should fail if specified list not found" do it "should fail if specified list not found" do
@session.response_body = <<-EOS with_html <<-HTML
<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>
EOS HTML
lambda { @session.selects "February", :from => "year" }.should raise_error lambda { selects "February", :from => "year" }.should raise_error
end end
it "should fail if the select is disabled" do it "should fail if the select is disabled" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<select name="month" disabled="disabled"><option value="1">January</option></select> <select name="month" disabled="disabled"><option value="1">January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
lambda { @session.selects "January", :from => "month" }.should raise_error lambda { selects "January", :from => "month" }.should raise_error
end end
it "should send value from option" do it "should send value from option" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "month" => "1") webrat_session.should_receive(:post).with("/login", "month" => "1")
@session.selects "January", :from => "month" selects "January", :from => "month"
@session.click_button click_button
end end
it "should send values with HTML encoded ampersands" do it "should send values with HTML encoded ampersands" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/login"> <form method="post" action="/login">
<select name="encoded"><option value="A &amp; B">Encoded</option></select> <select name="encoded"><option value="A &amp; B">Encoded</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", "encoded" => "A & B") webrat_session.should_receive(:post).with("/login", "encoded" => "A & B")
@session.selects "Encoded", :from => "encoded" selects "Encoded", :from => "encoded"
@session.click_button click_button
end end
it "should work with empty select lists" do it "should work with empty select lists" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", 'month' => '') webrat_session.should_receive(:post).with("/login", 'month' => '')
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "month" => "1") webrat_session.should_receive(:post).with("/login", "month" => "1")
@session.selects "January" selects "January"
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "end_month" selects "January", :from => "end_month"
@session.click_button click_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
@session.response_body = <<-EOS with_html <<-HTML
<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>
@ -118,51 +114,51 @@ describe "selects" do
<select id="end_month" name="end_month"><option value="e1">January</option></select> <select id="end_month" name="end_month"><option value="e1">January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "End Month" selects "January", :from => "End Month"
@session.click_button click_button
end end
it "should use option text if no value" do it "should use option text if no value" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "month" => "January") webrat_session.should_receive(:post).with("/login", "month" => "January")
@session.selects "January", :from => "month" selects "January", :from => "month"
@session.click_button click_button
end end
it "should find option by regexp" do it "should find option by regexp" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/login", "month" => "January") webrat_session.should_receive(:post).with("/login", "month" => "January")
@session.selects(/jan/i) selects(/jan/i)
@session.click_button click_button
end end
it "should fail if no option matching the regexp exists" do it "should fail if no option matching the regexp exists" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
lambda { lambda {
@session.selects(/feb/i) selects(/feb/i)
}.should raise_error }.should raise_error
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
@session.response_body = <<-EOS with_html <<-HTML
<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>
@ -170,9 +166,9 @@ describe "selects" do
<select id="end_month" name="end_month"><option value="e1">January</option></select> <select id="end_month" name="end_month"><option value="e1">January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects(/jan/i, :from => "End Month") selects(/jan/i, :from => "End Month")
@session.click_button click_button
end end
end end

View File

@ -1,12 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "select_time" do describe "select_time" do
before do
@session = Webrat::TestSession.new
end
it "should send the values for each individual time component" do it "should send the values for each individual time component" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<label for="appointment_time">Time</label><br /> <label for="appointment_time">Time</label><br />
<select id="appointment_time_4i" name="appointment[time(4i)]"> <select id="appointment_time_4i" name="appointment[time(4i)]">
@ -17,15 +13,15 @@ describe "select_time" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) "appointment" => {"time(4i)" => "09", "time(5i)" => "30"})
@session.selects_time "9:30AM", :from => "Time" selects_time "9:30AM", :from => "Time"
@session.click_button click_button
end end
it "should accept a time object" do it "should accept a time object" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<label for="appointment_time">Time</label><br /> <label for="appointment_time">Time</label><br />
<select id="appointment_time_4i" name="appointment[time(4i)]"> <select id="appointment_time_4i" name="appointment[time(4i)]">
@ -36,15 +32,15 @@ describe "select_time" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) "appointment" => {"time(4i)" => "09", "time(5i)" => "30"})
@session.select_time Time.parse("9:30AM"), :from => "Time" select_time Time.parse("9:30AM"), :from => "Time"
@session.click_button click_button
end end
it "should work when no label is specified" do it "should work when no label is specified" do
@session.response_body = <<-EOS with_html <<-HTML
<form action="/appointments" method="post"> <form action="/appointments" method="post">
<select id="appointment_time_4i" name="appointment[time(4i)]"> <select id="appointment_time_4i" name="appointment[time(4i)]">
<option value="09">09</option> <option value="09">09</option>
@ -54,22 +50,22 @@ describe "select_time" do
</select> </select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/appointments", webrat_session.should_receive(:post).with("/appointments",
"appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) "appointment" => {"time(4i)" => "09", "time(5i)" => "30"})
@session.select_time "9:30" select_time "9:30"
@session.click_button click_button
end end
it "should fail if the specified label is not found" do it "should fail if the specified label is not found" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/appointments"> <form method="post" action="/appointments">
<select name="month"><option>January</option></select> <select name="month"><option>January</option></select>
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
lambda { @session.select_time "9:30", :from => "Time" }.should raise_error lambda { select_time "9:30", :from => "Time" }.should raise_error
end end
end end

View File

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

View File

@ -1,12 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "within" do describe "within" do
before do
@session = Webrat::TestSession.new
end
it "should work when nested" do it "should work when nested" do
@session.response_body = <<-EOS with_html <<-HTML
<html> <html>
<div> <div>
<a href="/page1">Link</a> <a href="/page1">Link</a>
@ -15,34 +11,34 @@ describe "within" do
<div><a href="/page2">Link</a></div> <div><a href="/page2">Link</a></div>
</div> </div>
</html> </html>
EOS HTML
@session.should_receive(:get).with("/page2", {}) webrat_session.should_receive(:get).with("/page2", {})
@session.within "#container" do within "#container" do
@session.within "div" do within "div" do
@session.click_link "Link" click_link "Link"
end end
end end
end end
it "should click links within a scope" do it "should click links within a scope" do
@session.response_body = <<-EOS with_html <<-HTML
<html> <html>
<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>
</html> </html>
EOS HTML
@session.should_receive(:get).with("/page2", {}) webrat_session.should_receive(:get).with("/page2", {})
@session.within "#container" do within "#container" do
@session.click_link "Link" click_link "Link"
end end
end end
it "should submit forms within a scope" do it "should submit forms within a scope" do
@session.response_body = <<-EOS with_html <<-HTML
<html> <html>
<form id="form1" action="/form1"> <form id="form1" action="/form1">
<label>Email: <input type="text" name="email" /> <label>Email: <input type="text" name="email" />
@ -53,17 +49,17 @@ describe "within" do
<input type="submit" value="Add" /> <input type="submit" value="Add" />
</form> </form>
</html> </html>
EOS HTML
@session.should_receive(:get).with("/form2", "email" => "test@example.com") webrat_session.should_receive(:get).with("/form2", "email" => "test@example.com")
@session.within "#form2" do within "#form2" do
@session.fill_in "Email", :with => "test@example.com" fill_in "Email", :with => "test@example.com"
@session.click_button click_button
end end
end end
it "should not find buttons outside of the scope" do it "should not find buttons outside of the scope" do
@session.response_body = <<-EOS with_html <<-HTML
<html> <html>
<form action="/form1"> <form action="/form1">
<input type="submit" value="Add" /> <input type="submit" value="Add" />
@ -71,12 +67,12 @@ describe "within" do
<form id="form2" action="/form2"> <form id="form2" action="/form2">
</form> </form>
</html> </html>
EOS HTML
@session.within "#form2" do within "#form2" do
lambda { lambda {
@session.click_button click_button
}.should raise_error }.should raise_error(Webrat::NotFoundError)
end end
end end
end end

View File

@ -1,4 +1,8 @@
module Webrat module Webrat
def self.session_class
TestSession
end
class TestSession < Session class TestSession < Session
attr_accessor :response_body attr_accessor :response_body
attr_writer :response_code attr_writer :response_code

View File

@ -13,7 +13,11 @@ require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session")
require "merb-core" require "merb-core"
Spec::Runner.configure do |config| Spec::Runner.configure do |config|
# Nothing to configure yet include Webrat::Methods
def with_html(html)
webrat_session.response_body = html
end
end end
module Webrat module Webrat

View File

@ -4,43 +4,43 @@ describe Webrat::Link do
# include Webrat::Link # include Webrat::Link
before do before do
@session = mock(Webrat::TestSession) webrat_session = mock(Webrat::TestSession)
@link_text_with_nbsp = 'Link' + [0xA0].pack("U") + 'Text' @link_text_with_nbsp = 'Link' + [0xA0].pack("U") + 'Text'
end end
it "should pass through relative urls" do it "should pass through relative urls" do
link = Webrat::Link.new(@session, {"href" => "/path"}) link = Webrat::Link.new(webrat_session, {"href" => "/path"})
@session.should_receive(:request_page).with("/path", :get, {}) webrat_session.should_receive(:request_page).with("/path", :get, {})
link.click link.click
end end
it "shouldnt put base url onto " do it "shouldnt put base url onto " do
url = "https://www.example.com/path" url = "https://www.example.com/path"
@session.should_receive(:request_page).with(url, :get, {}) webrat_session.should_receive(:request_page).with(url, :get, {})
link = Webrat::Link.new(@session, {"href" => url}) link = Webrat::Link.new(webrat_session, {"href" => url})
link.click link.click
end end
it "should matches_text? on regexp" do it "should matches_text? on regexp" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp) link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?(/link/i).should == 0 link.matches_text?(/link/i).should == 0
end end
it "should matches_text? on link_text" do it "should matches_text? on link_text" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp) link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("Link Text").should == 0 link.matches_text?("Link Text").should == 0
end end
it "should matches_text? on substring" do it "should matches_text? on substring" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp) link.should_receive(:text).and_return(@link_text_with_nbsp)
link.matches_text?("nk Te").should_not be_nil link.matches_text?("nk Te").should_not be_nil
end end
it "should not matches_text? on link_text case insensitive" do it "should not matches_text? on link_text case insensitive" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp) link.should_receive(:text).and_return(@link_text_with_nbsp)
link.should_receive(:inner_html).and_return('Link&nbsp;Text') link.should_receive(:inner_html).and_return('Link&nbsp;Text')
link.should_receive(:title).and_return(nil) link.should_receive(:title).and_return(nil)
@ -48,13 +48,13 @@ describe Webrat::Link do
end end
it "should match text not include &nbsp;" do it "should match text not include &nbsp;" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('LinkText') link.should_receive(:text).and_return('LinkText')
link.matches_text?("LinkText").should == 0 link.matches_text?("LinkText").should == 0
end end
it "should not matches_text? on wrong text" do it "should not matches_text? on wrong text" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
nbsp = [0xA0].pack("U") nbsp = [0xA0].pack("U")
link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link") link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link")
link.should_receive(:inner_html).and_return("Some&nbsp;Other&nbsp;Link") link.should_receive(:inner_html).and_return("Some&nbsp;Other&nbsp;Link")
@ -67,32 +67,32 @@ describe Webrat::Link do
nokogiri_ja_kana = no_ko_gi_ri.pack("U*") nokogiri_ja_kana = no_ko_gi_ri.pack("U*")
nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("") nokogiri_char_ref = no_ko_gi_ri.map{|c| "&#x%X;" % c }.join("")
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(nokogiri_ja_kana) link.should_receive(:text).and_return(nokogiri_ja_kana)
link.matches_text?(nokogiri_ja_kana).should == 0 link.matches_text?(nokogiri_ja_kana).should == 0
end end
it "should match img link" do it "should match img link" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return('') link.should_receive(:text).and_return('')
link.should_receive(:inner_html).and_return('<img src="logo.png" />') link.should_receive(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10 link.matches_text?('logo.png').should == 10
end end
it "should matches_id? on exact matching id" do it "should matches_id? on exact matching id" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("some_id") link.should_receive(:id).and_return("some_id")
link.matches_id?("some_id").should == true link.matches_id?("some_id").should == true
end end
it "should not matches_id? on incorrect id" do it "should not matches_id? on incorrect id" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("other_id") link.should_receive(:id).and_return("other_id")
link.matches_id?("some_id").should == false link.matches_id?("some_id").should == false
end end
it "should matches_id? on matching id by regexp" do it "should matches_id? on matching id by regexp" do
link = Webrat::Link.new(@session, nil) link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:id).and_return("some_id") link.should_receive(:id).and_return("some_id")
link.matches_id?(/some/).should == true link.matches_id?(/some/).should == true
end end

View File

@ -23,21 +23,21 @@ describe Webrat::Session do
it "should open the page in the browser in MacOSX" do it "should open the page in the browser in MacOSX" do
session = Webrat::Session.new session = Webrat::Session.new
session.should_receive(:ruby_platform).and_return 'darwin' session.stub!(:ruby_platform => 'darwin')
session.should_receive(:`).with("open path") session.should_receive(:`).with("open path")
session.open_in_browser("path") session.open_in_browser("path")
end end
it "should open the page in the browser in cygwin" do it "should open the page in the browser in cygwin" do
session = Webrat::Session.new session = Webrat::Session.new
session.should_receive(:ruby_platform).and_return 'i386-cygwin' session.stub!(:ruby_platform => 'i386-cygwin')
session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file") session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file")
session.open_in_browser("path/to/file") session.open_in_browser("path/to/file")
end end
it "should open the page in the browser in Win32" do it "should open the page in the browser in Win32" do
session = Webrat::Session.new session = Webrat::Session.new
session.should_receive(:ruby_platform).and_return 'win32' session.stub!(:ruby_platform => 'win32')
session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file") session.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file")
session.open_in_browser("path/to/file") session.open_in_browser("path/to/file")
end end
@ -67,54 +67,54 @@ describe Webrat::Session do
describe "#http_accept" do describe "#http_accept" do
before(:each) do before(:each) do
@session = Webrat::Session.new webrat_session = Webrat::Session.new
end end
it "should set the Accept header with the string Mime type" do it "should set the Accept header with the string Mime type" do
@session.http_accept('application/xml') webrat_session.http_accept('application/xml')
@session.headers['Accept'].should == 'application/xml' webrat_session.headers['Accept'].should == 'application/xml'
end end
it "should set the Accept head with the string value of the symbol Mime type" do it "should set the Accept head with the string value of the symbol Mime type" do
@session.http_accept(:xml) webrat_session.http_accept(:xml)
@session.headers['Accept'].should == 'application/xml' webrat_session.headers['Accept'].should == 'application/xml'
end end
it "should raise an error if a symbol Mime type is passed that does not exist" do it "should raise an error if a symbol Mime type is passed that does not exist" do
lambda { @session.http_accept(:oogabooga) }.should raise_error(ArgumentError) lambda { webrat_session.http_accept(:oogabooga) }.should raise_error(ArgumentError)
end end
end end
describe "#request_page" do describe "#request_page" do
before(:each) do before(:each) do
Webrat.cache_config_for_test Webrat.cache_config_for_test
@session = Webrat::Session.new webrat_session = Webrat::Session.new
end end
after(:each) do after(:each) do
Webrat.reset_for_test Webrat.reset_for_test
end end
it "should raise an error if the request is not a success" do it "should raise an error if the request is not a success" do
@session.stub!(:get) webrat_session.stub!(:get)
@session.stub!(:response_body).and_return("Exception caught") webrat_session.stub!(:response_body => "Exception caught")
@session.stub!(:response_code).and_return(500) webrat_session.stub!(:response_code => 500)
@session.stub!(:formatted_error).and_return("application error") webrat_session.stub!(:formatted_error => "application error")
@session.stub!(:save_and_open_page) webrat_session.stub!(:save_and_open_page)
lambda { @session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError) lambda { webrat_session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError)
end end
it "should raise an error but not open if the request is not a success and config quashes save_and_open" do it "should raise an error but not open if the request is not a success and config quashes save_and_open" do
Webrat.configure do |config| Webrat.configure do |config|
config.open_error_files = false config.open_error_files = false
end end
@session.stub!(:get) webrat_session.stub!(:get)
@session.stub!(:response_body).and_return("Exception caught") webrat_session.stub!(:response_body => "Exception caught")
@session.stub!(:response_code).and_return(500) webrat_session.stub!(:response_code => 500)
@session.stub!(:formatted_error).and_return("application error") webrat_session.stub!(:formatted_error => "application error")
@session.should_not_receive(:save_and_open_page) webrat_session.should_not_receive(:save_and_open_page)
lambda { @session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError) lambda { webrat_session.request_page('some url', :get, {}) }.should raise_error(Webrat::PageLoadError)
end end
end end
end end

View File

@ -24,7 +24,6 @@ describe Webrat::Session do
end end
context "a session with a response" do context "a session with a response" do
setup do setup do
@session = Webrat::MerbSession.new @session = Webrat::MerbSession.new
@response = OpenStruct.new @response = OpenStruct.new

View File

@ -2,47 +2,45 @@ require File.expand_path(File.dirname(__FILE__) + '/helper')
describe "attach_file" do describe "attach_file" do
before do before do
@session = Webrat::TestSession.new
@filename = __FILE__ @filename = __FILE__
@uploaded_file = mock("uploaded file") @uploaded_file = mock("uploaded file")
ActionController::TestUploadedFile.stub!(:new).and_return(@uploaded_file) ActionController::TestUploadedFile.stub!(:new => @uploaded_file)
end end
it "should fail if no file field found" do it "should fail if no file field found" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/widgets"> <form method="post" action="/widgets">
</form> </form>
EOS HTML
lambda { @session.attach_file("Doc", "/some/path") }.should raise_error lambda { attach_file("Doc", "/some/path") }.should raise_error
end end
it "should submit empty strings for blank file fields" do it "should submit empty strings for blank file fields" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } }) webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } })
@session.click_button click_button
end end
it "should submit the attached file" do it "should submit the attached file" do
@session.response_body = <<-EOS with_html <<-HTML
<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 HTML
@session.should_receive(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } }) webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } })
@session.attach_file "Document", @filename attach_file "Document", @filename
@session.click_button click_button
end end
it "should support collections" do it "should support collections" do
@session.response_body = <<-EOS with_html <<-HTML
<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][]" />
@ -50,23 +48,23 @@ describe "attach_file" do
<input type="file" id="widget_file2" name="widget[files][]" /> <input type="file" id="widget_file2" name="widget[files][]" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
@session.should_receive(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } }) webrat_session.should_receive(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } })
@session.attach_file "Document", @filename attach_file "Document", @filename
@session.attach_file "Spreadsheet", @filename attach_file "Spreadsheet", @filename
@session.click_button click_button
end end
it "should allow the content type to be specified" do it "should allow the content type to be specified" do
@session.response_body = <<-EOS with_html <<-HTML
<form method="post" action="/widgets"> <form method="post" action="/widgets">
<label for="person_picture">Picture</label> <label for="person_picture">Picture</label>
<input type="file" id="person_picture" name="person[picture]" /> <input type="file" id="person_picture" name="person[picture]" />
<input type="submit" /> <input type="submit" />
</form> </form>
EOS HTML
ActionController::TestUploadedFile.should_receive(:new).with(@filename, "image/png").any_number_of_times ActionController::TestUploadedFile.should_receive(:new).with(@filename, "image/png").any_number_of_times
@session.attach_file "Picture", @filename, "image/png" attach_file "Picture", @filename, "image/png"
@session.click_button click_button
end end
end end

View File

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