diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 07b05a6..37983b2 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -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 diff --git a/lib/webrat/core/methods.rb b/lib/webrat/core/methods.rb index 04253c7..2b1b77a 100644 --- a/lib/webrat/core/methods.rb +++ b/lib/webrat/core/methods.rb @@ -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 diff --git a/spec/api/basic_auth_spec.rb b/spec/api/basic_auth_spec.rb index a2fc1e3..16f908d 100644 --- a/spec/api/basic_auth_spec.rb +++ b/spec/api/basic_auth_spec.rb @@ -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
- 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 diff --git a/spec/api/check_spec.rb b/spec/api/check_spec.rb index 8668eb3..8e9bcd4 100644 --- a/spec/api/check_spec.rb +++ b/spec/api/check_spec.rb @@ -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 - - EOS - - lambda { @session.check "remember_me" }.should raise_error + with_html <<-HTML + + + + HTML + + lambda { check "remember_me" }.should raise_error(Webrat::NotFoundError) end it "should fail if input is not a checkbox" do - @session.response_body = <<-EOS - - EOS + with_html <<-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 - 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 - - EOS - @session.should_receive(:post).with("/login", "remember_me" => "on") - @session.check "remember_me" - @session.click_button + with_html <<-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 - - EOS - lambda { @session.check "remember_me" }.should raise_error + with_html <<-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 - - EOS - @session.should_receive(:post).with("/login", "remember_me" => "yes") - @session.check "remember_me" - @session.click_button + with_html <<-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 - 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 - 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 - 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 - 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 - 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 diff --git a/spec/api/choose_spec.rb b/spec/api/choose_spec.rb index dcb8af6..9137388 100644 --- a/spec/api/choose_spec.rb +++ b/spec/api/choose_spec.rb @@ -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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 diff --git a/spec/api/click_area_spec.rb b/spec/api/click_area_spec.rb index 8de3bf1..791817f 100644 --- a/spec/api/click_area_spec.rb +++ b/spec/api/click_area_spec.rb @@ -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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 diff --git a/spec/api/click_button_spec.rb b/spec/api/click_button_spec.rb index a0c1e81..5e55ad1 100644 --- a/spec/api/click_button_spec.rb +++ b/spec/api/click_button_spec.rb @@ -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 - 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 - 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 - 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 - 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 - 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 - - EOS - @session.response_code = status - lambda { @session.click_button }.should_not raise_error + with_html <<-HTML + + 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 diff --git a/spec/api/click_link_spec.rb b/spec/api/click_link_spec.rb index 993bd2b..da7afb3 100644 --- a/spec/api/click_link_spec.rb +++ b/spec/api/click_link_spec.rb @@ -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 Save & go back - 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 Link text - 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 Link text - 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 Link text - 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 Link text - 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 Link text - 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 Link text - 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 Link text - 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 Link text - 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 Link text - 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 Posts - 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 Delete - 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 Posts - 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 Posts - 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 Put - 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 Link - 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 Link text - 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 Link text - 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 Link text - 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 Link text - 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 This is some cool link text, isn't it? - 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 Link text - 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 Link text Link text - 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 Linkerama Link - 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 This is a link - 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 - My house - Location - EOS + pending "needs fix" do + with_html <<-HTML + My house + Location + 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 Link - 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 Jump to Section 1 - 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 Jump to sub page - 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 Jump to sub page - 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 Jump to sub page - 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 Jump to foo bar - 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 diff --git a/spec/api/field_labeled_spec.rb b/spec/api/field_labeled_spec.rb index ebfe2f2..0f855f9 100644 --- a/spec/api/field_labeled_spec.rb +++ b/spec/api/field_labeled_spec.rb @@ -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 diff --git a/spec/api/fill_in_spec.rb b/spec/api/fill_in_spec.rb index e2150b6..e9e0135 100644 --- a/spec/api/fill_in_spec.rb +++ b/spec/api/fill_in_spec.rb @@ -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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 diff --git a/spec/api/reload_spec.rb b/spec/api/reload_spec.rb index 3382736..6a36325 100644 --- a/spec/api/reload_spec.rb +++ b/spec/api/reload_spec.rb @@ -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 diff --git a/spec/api/save_and_open_spec.rb b/spec/api/save_and_open_spec.rb index c1d4e79..a4281a3 100644 --- a/spec/api/save_and_open_spec.rb +++ b/spec/api/save_and_open_spec.rb @@ -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 @@ -16,9 +14,9 @@ describe "save_and_open_page" do 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 diff --git a/spec/api/selects_date_spec.rb b/spec/api/selects_date_spec.rb index 3b49d8c..7c5c90f 100644 --- a/spec/api/selects_date_spec.rb +++ b/spec/api/selects_date_spec.rb @@ -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 - 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 - 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 - 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 - 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 diff --git a/spec/api/selects_datetime_spec.rb b/spec/api/selects_datetime_spec.rb index 821cecd..af27c2c 100644 --- a/spec/api/selects_datetime_spec.rb +++ b/spec/api/selects_datetime_spec.rb @@ -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 - 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 - 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 - 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 - 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 diff --git a/spec/api/selects_spec.rb b/spec/api/selects_spec.rb index 2ee4ae0..8ec04b4 100644 --- a/spec/api/selects_spec.rb +++ b/spec/api/selects_spec.rb @@ -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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 diff --git a/spec/api/selects_time_spec.rb b/spec/api/selects_time_spec.rb index 86c0b75..1f99d0b 100644 --- a/spec/api/selects_time_spec.rb +++ b/spec/api/selects_time_spec.rb @@ -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 - 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 - 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 - 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 - 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 diff --git a/spec/api/visit_spec.rb b/spec/api/visit_spec.rb index 8959686..c6753d8 100644 --- a/spec/api/visit_spec.rb +++ b/spec/api/visit_spec.rb @@ -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 diff --git a/spec/api/within_spec.rb b/spec/api/within_spec.rb index d9461e6..0af74ed 100644 --- a/spec/api/within_spec.rb +++ b/spec/api/within_spec.rb @@ -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 - 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 Link - 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 - 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 - 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 diff --git a/spec/fakes/test_session.rb b/spec/fakes/test_session.rb index 6fdfd97..9f9cbdb 100644 --- a/spec/fakes/test_session.rb +++ b/spec/fakes/test_session.rb @@ -1,4 +1,8 @@ module Webrat + def self.session_class + TestSession + end + class TestSession < Session attr_accessor :response_body attr_writer :response_code diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ba96616..23900b3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/webrat/core/link_spec.rb b/spec/webrat/core/link_spec.rb index ef6ddf6..a06f099 100755 --- a/spec/webrat/core/link_spec.rb +++ b/spec/webrat/core/link_spec.rb @@ -1,100 +1,100 @@ -require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") - -describe Webrat::Link do -# include Webrat::Link - - before do - @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.click - end - - it "shouldnt put base url onto " do - url = "https://www.example.com/path" - @session.should_receive(:request_page).with(url, :get, {}) - link = Webrat::Link.new(@session, {"href" => url}) - link.click - end - - it "should matches_text? on regexp" do - link = Webrat::Link.new(@session, nil) - link.should_receive(:text).and_return(@link_text_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.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.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.should_receive(:text).and_return(@link_text_with_nbsp) - link.should_receive(:inner_html).and_return('Link Text') - link.should_receive(:title).and_return(nil) - link.matches_text?("link_text").should == false - end - - it "should match text not include " do - link = Webrat::Link.new(@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) - nbsp = [0xA0].pack("U") - link.should_receive(:text).and_return("Some"+nbsp+"Other"+nbsp+"Link") - link.should_receive(:inner_html).and_return("Some Other Link") - link.should_receive(:title).and_return(nil) - link.matches_text?("Link Text").should == false - end - - it "should match text including character reference" do - no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA] - nokogiri_ja_kana = no_ko_gi_ri.pack("U*") - nokogiri_char_ref = no_ko_gi_ri.map{|c| "%X;" % c }.join("") - - link = Webrat::Link.new(@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.should_receive(:text).and_return('') - link.should_receive(:inner_html).and_return('') - link.matches_text?('logo.png').should == 10 - end - - it "should matches_id? on exact matching id" do - link = Webrat::Link.new(@session, nil) - link.should_receive(:id).and_return("some_id") - link.matches_id?("some_id").should == true - end - - it "should not matches_id? on incorrect id" do - link = Webrat::Link.new(@session, nil) - link.should_receive(:id).and_return("other_id") - link.matches_id?("some_id").should == false - end - - it "should matches_id? on matching id by regexp" do - link = Webrat::Link.new(@session, nil) - link.should_receive(:id).and_return("some_id") - link.matches_id?(/some/).should == true - end - -end +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") + +describe Webrat::Link do +# include Webrat::Link + + before do + 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(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" + 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(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(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(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(webrat_session, nil) + link.should_receive(:text).and_return(@link_text_with_nbsp) + link.should_receive(:inner_html).and_return('Link Text') + link.should_receive(:title).and_return(nil) + link.matches_text?("link_text").should == false + end + + it "should match text not include " do + 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(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 Other Link") + link.should_receive(:title).and_return(nil) + link.matches_text?("Link Text").should == false + end + + it "should match text including character reference" do + no_ko_gi_ri = [0x30CE,0x30B3,0x30AE,0x30EA] + nokogiri_ja_kana = no_ko_gi_ri.pack("U*") + nokogiri_char_ref = no_ko_gi_ri.map{|c| "%X;" % c }.join("") + + 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(webrat_session, nil) + link.should_receive(:text).and_return('') + link.should_receive(:inner_html).and_return('') + link.matches_text?('logo.png').should == 10 + end + + it "should matches_id? on exact matching id" do + 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(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(webrat_session, nil) + link.should_receive(:id).and_return("some_id") + link.matches_id?(/some/).should == true + end + +end diff --git a/spec/webrat/core/session_spec.rb b/spec/webrat/core/session_spec.rb index fe0d0b8..6fdcc48 100644 --- a/spec/webrat/core/session_spec.rb +++ b/spec/webrat/core/session_spec.rb @@ -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 diff --git a/spec/webrat/merb/session_spec.rb b/spec/webrat/merb/session_spec.rb index 4553bc0..fadefa0 100644 --- a/spec/webrat/merb/session_spec.rb +++ b/spec/webrat/merb/session_spec.rb @@ -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 diff --git a/spec/webrat/rails/attaches_file_spec.rb b/spec/webrat/rails/attaches_file_spec.rb index deb3499..9d41086 100644 --- a/spec/webrat/rails/attaches_file_spec.rb +++ b/spec/webrat/rails/attaches_file_spec.rb @@ -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 - 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 - 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 - 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 - 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 - 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 \ No newline at end of file diff --git a/spec/webrat/rails/rails_spec.rb b/spec/webrat/rails/rails_spec.rb index 0cd2849..86e9855 100644 --- a/spec/webrat/rails/rails_spec.rb +++ b/spec/webrat/rails/rails_spec.rb @@ -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 # -# 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 # Jump to sub page -# 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 \ No newline at end of file