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"
module Webrat
class DisabledFieldError < WebratError
end
class Field #:nodoc:
def self.class_for_element(element)
@ -64,7 +67,8 @@ module Webrat
end
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
def to_param

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,36 +2,34 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "field_labeled" do
class << self
def using_this_html html
before(:each) do
@session = Webrat::TestSession.new
@session.response_body = html
with_html(html)
end
end
def field_labeled label
def field_labeled(label)
@label = label
yield
end
def should_return_a type, opts
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
def with_an_id_of id, opts
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
def should_raise_error_matching regexp, opts
it "should raise with wrong label" do
lambda {
@session.field_labeled(opts[:for])
field_labeled(opts[:for])
}.should raise_error(regexp)
end
end

View File

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

View File

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

View File

@ -2,9 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "save_and_open_page" do
before do
@session = Webrat::TestSession.new
@session.response_body = <<-HTML
with_html <<-HTML
<html>
<head>
<link href="/stylesheets/foo.css" media="screen" rel="stylesheet" type="text/css" />
@ -16,9 +14,9 @@ describe "save_and_open_page" do
</html>
HTML
File.stub!(:exist?).and_return(true)
Time.stub!(:now).and_return(1234)
@session.stub!(:open_in_browser)
File.stub!(:exist? => true)
Time.stub!(:now => 1234)
webrat_session.stub!(:open_in_browser)
@file_handle = mock("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
@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
@session.save_and_open_page
save_and_open_page
end
it "should rewrite image paths" do
@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
@session.save_and_open_page
save_and_open_page
end
it "should open the temp file in a browser" do
@session.should_receive(:open_in_browser).with(filename)
@session.save_and_open_page
webrat_session.should_receive(:open_in_browser).with(filename)
save_and_open_page
end
def filename

View File

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

View File

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

View File

@ -1,116 +1,112 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "selects" do
before do
@session = Webrat::TestSession.new
end
it "should fail with a helpful message when option not found" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
</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")
end
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">
<select name="month"><option value="1">January</option></select>
<select name="year"><option value="2008">2008</option></select>
</form>
EOS
HTML
lambda { @session.selects "February", :from => "year" }.should raise_error
lambda { selects "February", :from => "year" }.should raise_error
end
it "should fail if specified list not found" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="get" action="/login">
<select name="month"><option value="1">January</option></select>
</form>
EOS
HTML
lambda { @session.selects "February", :from => "year" }.should raise_error
lambda { selects "February", :from => "year" }.should raise_error
end
it "should fail if the select is disabled" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="month" disabled="disabled"><option value="1">January</option></select>
<input type="submit" />
</form>
EOS
HTML
lambda { @session.selects "January", :from => "month" }.should raise_error
lambda { selects "January", :from => "month" }.should raise_error
end
it "should send value from option" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="month"><option value="1">January</option></select>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", "month" => "1")
@session.selects "January", :from => "month"
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", "month" => "1")
selects "January", :from => "month"
click_button
end
it "should send values with HTML encoded ampersands" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="encoded"><option value="A &amp; B">Encoded</option></select>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", "encoded" => "A & B")
@session.selects "Encoded", :from => "encoded"
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", "encoded" => "A & B")
selects "Encoded", :from => "encoded"
click_button
end
it "should work with empty select lists" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="month"></select>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", 'month' => '')
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", 'month' => '')
click_button
end
it "should work without specifying the field name or label" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="month"><option value="1">January</option></select>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", "month" => "1")
@session.selects "January"
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", "month" => "1")
selects "January"
click_button
end
it "should send value from option in list specified by name" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="start_month"><option value="s1">January</option></select>
<select name="end_month"><option value="e1">January</option></select>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "end_month"
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
selects "January", :from => "end_month"
click_button
end
it "should send value from option in list specified by label" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<label for="start_month">Start Month</label>
<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>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects "January", :from => "End Month"
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
selects "January", :from => "End Month"
click_button
end
it "should use option text if no value" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", "month" => "January")
@session.selects "January", :from => "month"
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", "month" => "January")
selects "January", :from => "month"
click_button
end
it "should find option by regexp" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", "month" => "January")
@session.selects(/jan/i)
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", "month" => "January")
selects(/jan/i)
click_button
end
it "should fail if no option matching the regexp exists" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<select name="month"><option>January</option></select>
<input type="submit" />
</form>
EOS
HTML
lambda {
@session.selects(/feb/i)
selects(/feb/i)
}.should raise_error
end
it "should find option by regexp in list specified by label" do
@session.response_body = <<-EOS
with_html <<-HTML
<form method="post" action="/login">
<label for="start_month">Start Month</label>
<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>
<input type="submit" />
</form>
EOS
@session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
@session.selects(/jan/i, :from => "End Month")
@session.click_button
HTML
webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1")
selects(/jan/i, :from => "End Month")
click_button
end
end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,43 +4,43 @@ describe Webrat::Link do
# include Webrat::Link
before do
@session = mock(Webrat::TestSession)
webrat_session = mock(Webrat::TestSession)
@link_text_with_nbsp = 'Link' + [0xA0].pack("U") + 'Text'
end
it "should pass through relative urls" do
link = Webrat::Link.new(@session, {"href" => "/path"})
@session.should_receive(:request_page).with("/path", :get, {})
link = Webrat::Link.new(webrat_session, {"href" => "/path"})
webrat_session.should_receive(:request_page).with("/path", :get, {})
link.click
end
it "shouldnt put base url onto " do
url = "https://www.example.com/path"
@session.should_receive(:request_page).with(url, :get, {})
link = Webrat::Link.new(@session, {"href" => url})
webrat_session.should_receive(:request_page).with(url, :get, {})
link = Webrat::Link.new(webrat_session, {"href" => url})
link.click
end
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.matches_text?(/link/i).should == 0
end
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.matches_text?("Link Text").should == 0
end
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.matches_text?("nk Te").should_not be_nil
end
it "should not matches_text? on link_text case insensitive" do
link = Webrat::Link.new(@session, nil)
link = Webrat::Link.new(webrat_session, nil)
link.should_receive(:text).and_return(@link_text_with_nbsp)
link.should_receive(:inner_html).and_return('Link&nbsp;Text')
link.should_receive(:title).and_return(nil)
@ -48,13 +48,13 @@ describe Webrat::Link do
end
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.matches_text?("LinkText").should == 0
end
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")
link.should_receive(:text).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_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.matches_text?(nokogiri_ja_kana).should == 0
end
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(:inner_html).and_return('<img src="logo.png" />')
link.matches_text?('logo.png').should == 10
end
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.matches_id?("some_id").should == true
end
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.matches_id?("some_id").should == false
end
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.matches_id?(/some/).should == true
end

View File

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

View File

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

View File

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

View File

@ -1,20 +1,20 @@
# it "should default to current url" do
# # @session.current_page.stub!(:url).and_return("/page")
# @session.response_body = <<-EOS
# # current_page.stub!(:url => "/page")
# response_body = <<-HTML
# <form method="get">
# <input type="submit" />
# </form>
# EOS
# @page.stub!(:url).and_return("/current")
# @session.should_receive(:get).with("/current", {})
# @session.click_button
# HTML
# @page.stub!(:url => "/current")
# webrat_session.should_receive(:get).with("/current", {})
# click_button
# end
#
# 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>
# EOS
# @session.should_receive(:https!).with(true)
# @session.should_receive(:get).with("/page/sub", {})
# @session.click_link "Jump to sub page"
# HTML
# webrat_session.should_receive(:https!).with(true)
# webrat_session.should_receive(:get).with("/page/sub", {})
# click_link "Jump to sub page"
# end