diff --git a/History.txt b/History.txt index edbf24a..77af696 100644 --- a/History.txt +++ b/History.txt @@ -2,9 +2,11 @@ * Enhancements + * Support file fields using attaches_file (Patch from Kyle Hargraves) * Support button elements (Patch from Nick Sieger) * Support matching select options by regexp (Patch from Kyle Hargraves) * Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves) + * Support links to fully qualified URLs starting with http:// or https:// (Luke Melia) * Bug fixes @@ -26,7 +28,7 @@ * Added reloads method to reload the page (Patch from Kamal Fariz Mahyuddi) * Prevent making a request if clicking on local anchor link (Patch from Kamal Fariz Mahyuddi) * Added clicks_link_within(selector, link_text), allowing restricting link search - to within a given css selector (Path from Luke Melia) + to within a given css selector (Patch from Luke Melia) * Allow specifying the input name/label when doing a select (Patch from David Chelimsky) * Raise a specific exception if the developer tries to manipulate form elements before loading a page (Patch from James Deville) * Add support for alternate POST, PUT and DELETE link clicking (Patch from Kyle Hargraves) diff --git a/README.txt b/README.txt index ffbdf08..df8ec0e 100644 --- a/README.txt +++ b/README.txt @@ -1,14 +1,14 @@ -= Webrat - Ruby Acceptance Testing for Web applications +Webrat +====== - http://rubyforge.org/projects/webrat - http://github.com/brynary/webrat +- [Code on GitHub](http://github.com/brynary/webrat) +- [Tickets on Lighthouse](http://webrat.lighthouseapp.com/) -* mailto:bryan@brynary.com -* mailto:seth@mojodna.net +Description +----------- -== DESCRIPTION: - -Webrat lets you quickly write robust and thorough acceptance tests for a Ruby +Webrat (_Ruby Acceptance Testing for Web applications_) +lets you quickly write robust and thorough acceptance tests for a Ruby web application. By leveraging the DOM, it can run tests similarly to an in-browser testing solution without the associated performance hit (and browser dependency). The result is tests that are less fragile and more @@ -19,20 +19,21 @@ Selenium, the primary consideration should be how much JavaScript the application uses. In-browser testing is currently the only way to test JS, and that may make it a requirement for your project. If JavaScript is not central to your application, Webrat is a simpler, effective solution that will let you -run your tests much faster and more frequently. (Benchmarks forthcoming.) +run your tests much faster and more frequently. -Initial development was sponsored by EastMedia (http://www.eastmedia.com). +Initial development was sponsored by [EastMedia](http://www.eastmedia.com). -== SYNOPSIS: +Synopsis +-------- - def test_sign_up - visits "/" - clicks_link "Sign up" - fills_in "Email", :with => "good@example.com" - selects "Free account" - clicks_button "Register" - ... - end + def test_sign_up + visits "/" + clicks_link "Sign up" + fills_in "Email", :with => "good@example.com" + selects "Free account" + clicks_button "Register" + ... + end Behind the scenes, this will perform the following work: @@ -50,37 +51,47 @@ Behind the scenes, this will perform the following work: Take special note of the things _not_ specified in that test, that might cause tests to break unnecessarily as your application evolves: -* The input field IDs or names (e.g. "user_email" or "user[email]"), which +- The input field IDs or names (e.g. "user_email" or "user[email]"), which could change if you rename a model -* The ID of the form element (Webrat can do a good job of guessing, even if +- The ID of the form element (Webrat can do a good job of guessing, even if there are multiple forms on the page.) -* The URLs of links followed -* The URL the form submission should be sent to, which could change if you +- The URLs of links followed +- The URL the form submission should be sent to, which could change if you adjust your routes or controllers -* The HTTP method for the login request +- The HTTP method for the login request -A test written with Webrat can handle these changes smoothly. +A test written with Webrat can handle these changes to these without any modifications. -== REQUIREMENTS: +Install +------- -* Rails >= 1.2.6 -* Hpricot >= 0.6 -* Rails integration tests in Test::Unit _or_ -* RSpec stories (using an RSpec version >= revision 2997) +To install the latest release: -== INSTALL: + sudo gem install webrat In your stories/helper.rb: - require "webrat" + require "webrat" You could also unpack the gem into vendor/plugins. -== HISTORY: +Requirements +------------ -See CHANGELOG in this directory. +- Rails >= 1.2.6 +- Hpricot >= 0.6 +- Rails integration tests in Test::Unit _or_ +- RSpec stories (using an RSpec version >= revision 2997) -== LICENSE: +Authors +------- + +- Maintained by [Bryan Helmkamp](mailto:bryan@brynary.com) +- Original code written by [Seth Fitzsimmons](mailto:seth@mojodna.net) +- Many other contributors. See attributions in History.txt + +License +------- Copyright (c) 2007 Bryan Helmkamp, Seth Fitzsimmons. -See MIT-LICENSE in this directory. +See MIT-LICENSE.txt in this directory. diff --git a/Rakefile b/Rakefile index d1ce857..35fcfc8 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,7 @@ require 'rubygems' require 'hoe' +require 'spec' +require 'spec/rake/spectask' require './lib/webrat.rb' Hoe.new('webrat', Webrat::VERSION) do |p| @@ -16,10 +18,48 @@ Hoe.new('webrat', Webrat::VERSION) do |p| p.extra_deps << ["hpricot", ">= 0.6"] p.remote_rdoc_dir = '' # Release to root - p.test_globs = ['test/**/*_test.rb'] end desc "Upload rdoc to brynary.com" task :publish_rdoc => :docs do sh "scp -r doc/ brynary.com:/apps/uploads/webrat" +end + +Rake::TaskManager.class_eval do + def remove_task(task_name) + @tasks.delete(task_name.to_s) + end +end + +def remove_task(task_name) + Rake.application.remove_task(task_name) +end + +remove_task "test" +remove_task "test_deps" + +desc "Run all specs in spec directory" +Spec::Rake::SpecTask.new do |t| + t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""] + t.spec_files = FileList['spec/**/*_spec.rb'] +end + +desc "Run all specs in spec directory with RCov" +Spec::Rake::SpecTask.new(:rcov) do |t| + t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""] + t.spec_files = FileList['spec/**/*_spec.rb'] + t.rcov = true + t.rcov_opts = lambda do + IO.readlines(File.dirname(__FILE__) + "/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten + end +end + +require 'spec/rake/verify_rcov' +RCov::VerifyTask.new(:verify_rcov => :rcov) do |t| + t.threshold = 95.5 # Make sure you have rcov 0.7 or higher! +end + +remove_task "default" +task :default do + Rake::Task["verify_rcov"].invoke end \ No newline at end of file diff --git a/lib/webrat.rb b/lib/webrat.rb index 4401f1a..26c8dde 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -35,7 +35,7 @@ module ActionController current_page.save_and_open end - [:reloads, :fills_in, :clicks_button, :selects, :chooses, :checks, :unchecks, :clicks_link, :clicks_link_within, :clicks_put_link, :clicks_get_link, :clicks_post_link, :clicks_delete_link].each do |method_name| + [:reloads, :fills_in, :clicks_button, :selects, :attaches_file, :chooses, :checks, :unchecks, :clicks_link, :clicks_link_within, :clicks_put_link, :clicks_get_link, :clicks_post_link, :clicks_delete_link].each do |method_name| define_method(method_name) do |*args| current_page.send(method_name, *args) end diff --git a/lib/webrat/field.rb b/lib/webrat/field.rb index f6b1a1e..e45a192 100644 --- a/lib/webrat/field.rb +++ b/lib/webrat/field.rb @@ -97,6 +97,21 @@ module Webrat end end + def replace_param_value(params, oval, nval) + output = Hash.new + params.each do |key, value| + case value + when Hash + value = replace_param_value(value, oval, nval) + when Array + value = value.map { |o| o == oval ? nval : oval } + when oval + value = nval + end + output[key] = value + end + output + end end class ButtonField < Field @@ -221,6 +236,15 @@ module Webrat end class FileField < Field + + def to_param + if @value.nil? + super + else + replace_param_value(super, @value, ActionController::TestUploadedFile.new(@value)) + end + end + end class TextField < Field diff --git a/lib/webrat/link.rb b/lib/webrat/link.rb index d9ff682..e13d21d 100644 --- a/lib/webrat/link.rb +++ b/lib/webrat/link.rb @@ -10,6 +10,7 @@ module Webrat method ||= http_method return if href =~ /^#/ && method == :get + update_protocol(href) Page.new(@page.session, absolute_href, method, authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token}) end @@ -26,9 +27,19 @@ module Webrat def href @element["href"] end + + def update_protocol(href) + if href =~ /^https:/ + @page.session.https!(true) + elsif href =~ /^http:/ + @page.session.https!(false) + end + end def absolute_href - if href =~ /^\?/ + if href =~ %r{^https?://www.example.com(/.*)} + $LAST_MATCH_INFO.captures.first + elsif href =~ /^\?/ "#{@page.url}#{href}" elsif href !~ /^\// "#{@page.url}/#{href}" diff --git a/lib/webrat/page.rb b/lib/webrat/page.rb index 65e5056..17fcd0e 100644 --- a/lib/webrat/page.rb +++ b/lib/webrat/page.rb @@ -88,6 +88,17 @@ module Webrat option.choose end + # Verifies that an input file field exists on the current page and sets + # its value to the given +file+, so that the file will be uploaded + # along with the form. + # + # Example: + # attaches_file "Photo", "/path/to/the/photo.jpg" + def attaches_file(id_or_name_or_label, path) + field = find_field(id_or_name_or_label, FileField) + field.set(path) + end + # Saves the currently loaded page out to RAILS_ROOT/tmp/ and opens it in the default # web browser if on OS X. Useful for debugging. # diff --git a/spec/attaches_file_spec.rb b/spec/attaches_file_spec.rb new file mode 100644 index 0000000..30111c1 --- /dev/null +++ b/spec/attaches_file_spec.rb @@ -0,0 +1,62 @@ +require File.expand_path(File.dirname(__FILE__) + "/spec_helper") + +describe "attaches_file" do + before do + @session = ActionController::Integration::Session.new + @session.stubs(:assert_response) + @session.stubs(:get_via_redirect) + @session.stubs(:response).returns(@response=mock) + + @filename = __FILE__ + @uploaded_file = mock + ActionController::TestUploadedFile.stubs(:new).returns(@uploaded_file) + end + + it "should fail if no file field found" do + @response.stubs(:body).returns(<<-EOS) +
+
+ EOS + lambda { @session.attaches_file("Doc", "/some/path") }.should raise_error + end + + it "should submit empty strings for blank file fields" do + @response.stubs(:body).returns(<<-EOS) +
+ + +
+ EOS + @session.expects(:post_via_redirect).with("/widgets", { "widget" => { "file" => "" } }) + @session.clicks_button + end + + it "should submit the attached file" do + @response.stubs(:body).returns(<<-EOS) +
+ + + +
+ EOS + @session.expects(:post_via_redirect).with("/widgets", { "widget" => { "file" => @uploaded_file } }) + @session.attaches_file "Document", @filename + @session.clicks_button + end + + it "should support collections" do + @response.stubs(:body).returns(<<-EOS) +
+ + + + + +
+ EOS + @session.expects(:post_via_redirect).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } }) + @session.attaches_file "Document", @filename + @session.attaches_file "Spreadsheet", @filename + @session.clicks_button + end +end diff --git a/test/checks_test.rb b/spec/checks_spec.rb similarity index 76% rename from test/checks_test.rb rename to spec/checks_spec.rb index 7d3113b..43ab091 100644 --- a/test/checks_test.rb +++ b/spec/checks_spec.rb @@ -1,37 +1,33 @@ -require File.dirname(__FILE__) + "/helper" +require File.expand_path(File.dirname(__FILE__) + "/spec_helper") -class ChecksTest < Test::Unit::TestCase - def setup +describe "checks" do + before do @session = ActionController::Integration::Session.new @session.stubs(:assert_response) @session.stubs(:get_via_redirect) @session.stubs(:response).returns(@response=mock) end - def test_should_fail_if_no_checkbox_found + it "should fail if no checkbox found" do @response.stubs(:body).returns(<<-EOS)
EOS - assert_raises RuntimeError do - @session.checks "remember_me" - end + lambda { @session.checks "remember_me" }.should raise_error end - def test_should_fail_if_input_is_not_a_checkbox + it "should fail if input is not a checkbox" do @response.stubs(:body).returns(<<-EOS)
EOS - assert_raises RuntimeError do - @session.checks "remember_me" - end + lambda { @session.checks "remember_me" }.should raise_error end - def test_should_check_rails_style_checkboxes + it "should check rails style checkboxes" do @response.stubs(:body).returns(<<-EOS)
@@ -45,7 +41,7 @@ class ChecksTest < Test::Unit::TestCase @session.clicks_button end - def test_should_result_in_the_value_on_being_posted_if_not_specified + it "should result in the value on being posted if not specified" do @response.stubs(:body).returns(<<-EOS) @@ -57,7 +53,7 @@ class ChecksTest < Test::Unit::TestCase @session.clicks_button end - def test_should_result_in_a_custom_value_being_posted + it "should result in a custom value being posted" do @response.stubs(:body).returns(<<-EOS) @@ -70,38 +66,34 @@ class ChecksTest < Test::Unit::TestCase end end -class UnchecksTest < Test::Unit::TestCase - def setup +describe "unchecks" do + before do @session = ActionController::Integration::Session.new @session.stubs(:assert_response) @session.stubs(:get_via_redirect) @session.stubs(:response).returns(@response=mock) end - def test_should_fail_if_no_checkbox_found + it "should fail if no checkbox found" do @response.stubs(:body).returns(<<-EOS)
EOS - assert_raises RuntimeError do - @session.unchecks "remember_me" - end + lambda { @session.unchecks "remember_me" }.should raise_error end - def test_should_fail_if_input_is_not_a_checkbox + it "should fail if input is not a checkbox" do @response.stubs(:body).returns(<<-EOS)
EOS - assert_raises RuntimeError do - @session.unchecks "remember_me" - end + lambda { @session.unchecks "remember_me" }.should raise_error end - def test_should_uncheck_rails_style_checkboxes + it "should uncheck rails style checkboxes" do @response.stubs(:body).returns(<<-EOS)
@@ -116,7 +108,7 @@ class UnchecksTest < Test::Unit::TestCase @session.clicks_button end - def test_should_result_in_value_not_being_posted + it "should result in value not being posted" do @response.stubs(:body).returns(<<-EOS) diff --git a/test/chooses_test.rb b/spec/chooses_spec.rb similarity index 78% rename from test/chooses_test.rb rename to spec/chooses_spec.rb index 6b9afa8..fc01c3d 100644 --- a/test/chooses_test.rb +++ b/spec/chooses_spec.rb @@ -1,38 +1,33 @@ -require File.dirname(__FILE__) + "/helper" +require File.expand_path(File.dirname(__FILE__) + "/spec_helper") -class ChoosesTest < Test::Unit::TestCase - - def setup +describe "chooses" do + before do @session = ActionController::Integration::Session.new @session.stubs(:assert_response) @session.stubs(:get_via_redirect) @session.stubs(:response).returns(@response=mock) end - def test_should_fail_if_no_radio_buttons_found + it "should fail if no radio buttons found" do @response.stubs(:body).returns(<<-EOS)
EOS - assert_raises RuntimeError do - @session.chooses "first option" - end + lambda { @session.chooses "first option" }.should raise_error end - def test_should_fail_if_input_is_not_a_radio_button + it "should fail if input is not a radio button" do @response.stubs(:body).returns(<<-EOS)
EOS - assert_raises RuntimeError do - @session.chooses "first_option" - end + lambda { @session.chooses "first_option" }.should raise_error end - def test_should_check_rails_style_radio_buttons + it "should check rails style radio buttons" do @response.stubs(:body).returns(<<-EOS)
@@ -47,7 +42,7 @@ class ChoosesTest < Test::Unit::TestCase @session.clicks_button end - def test_should_only_submit_last_chosen_value + it "should only submit last chosen value" do @response.stubs(:body).returns(<<-EOS) @@ -63,7 +58,7 @@ class ChoosesTest < Test::Unit::TestCase @session.clicks_button end - def test_should_result_in_the_value_on_being_posted_if_not_specified + it "should result in the value on being posted if not specified" do @response.stubs(:body).returns(<<-EOS) @@ -75,7 +70,7 @@ class ChoosesTest < Test::Unit::TestCase @session.clicks_button end - def test_should_result_in_the_value_on_being_posted_if_not_specified_and_checked_by_default + it "should result in the value on being posted if not specified and checked by default" do @response.stubs(:body).returns(<<-EOS) @@ -85,5 +80,4 @@ class ChoosesTest < Test::Unit::TestCase @session.expects(:post_via_redirect).with("/login", "first_option" => "on") @session.clicks_button end - -end \ No newline at end of file +end diff --git a/test/clicks_button_test.rb b/spec/clicks_button_spec.rb similarity index 85% rename from test/clicks_button_test.rb rename to spec/clicks_button_spec.rb index ea21557..c9d32c0 100644 --- a/test/clicks_button_test.rb +++ b/spec/clicks_button_spec.rb @@ -1,7 +1,7 @@ -require File.dirname(__FILE__) + "/helper" +require File.expand_path(File.dirname(__FILE__) + "/spec_helper") -class ClicksButtonTest < Test::Unit::TestCase - def setup +describe "clicks_button" do + before do @session = ActionController::Integration::Session.new @session.stubs(:assert_response) @session.stubs(:get_via_redirect) @@ -11,29 +11,25 @@ class ClicksButtonTest < Test::Unit::TestCase @session.stubs(:response).returns(@response) end - def test_should_fail_if_no_buttons + it "should fail if no buttons" do @response.stubs(:body).returns(<<-EOS)
EOS - assert_raises RuntimeError do - @session.clicks_button - end + lambda { @session.clicks_button }.should raise_error end - def test_should_fail_if_input_is_not_a_submit_button + it "should fail if input is not a submit button" do @response.stubs(:body).returns(<<-EOS)
EOS - - assert_raises RuntimeError do - @session.clicks_button - end + + lambda { @session.clicks_button }.should raise_error end - def test_should_default_to_get_method + it "should default to get method" do @response.stubs(:body).returns(<<-EOS)
@@ -43,7 +39,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_assert_valid_response + it "should assert valid response" do @response.stubs(:body).returns(<<-EOS) @@ -53,7 +49,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_default_to_current_url + it "should default to current url" do @response.stubs(:body).returns(<<-EOS) @@ -64,7 +60,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_submit_the_first_form_by_default + it "should submit the first form by default" do @response.stubs(:body).returns(<<-EOS) @@ -77,7 +73,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_not_explode_on_file_fields + it "should not explode on file fields" do @response.stubs(:body).returns(<<-EOS) @@ -87,7 +83,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_submit_the_form_with_the_specified_button + it "should submit the form with the specified button" do @response.stubs(:body).returns(<<-EOS) @@ -100,7 +96,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button "Form2" end - def test_should_use_action_from_form + it "should use action from form" do @response.stubs(:body).returns(<<-EOS) @@ -110,7 +106,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_use_method_from_form + it "should use method from form" do @response.stubs(:body).returns(<<-EOS) @@ -120,7 +116,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_button_as_param_if_it_has_a_name + it "should send button as param if it has a name" do @response.stubs(:body).returns(<<-EOS) @@ -131,7 +127,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button("Login") end - def test_should_not_send_button_as_param_if_it_has_no_name + it "should not send button as param if it has no name" do @response.stubs(:body).returns(<<-EOS) @@ -142,7 +138,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button("Login") end - def test_should_send_default_password_field_values + it "should send default password field values" do @response.stubs(:body).returns(<<-EOS) @@ -153,7 +149,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_default_hidden_field_values + it "should send default hidden field values" do @response.stubs(:body).returns(<<-EOS) @@ -164,7 +160,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_default_text_field_values + it "should send default text field values" do @response.stubs(:body).returns(<<-EOS) @@ -175,7 +171,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_default_checked_fields + it "should send default checked fields" do @response.stubs(:body).returns(<<-EOS) @@ -186,7 +182,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_default_radio_options + it "should send default radio options" do @response.stubs(:body).returns(<<-EOS) @@ -200,7 +196,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_correct_data_for_rails_style_unchecked_fields + it "should send correct data for rails style unchecked fields" do @response.stubs(:body).returns(<<-EOS) @@ -212,7 +208,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_correct_data_for_rails_style_checked_fields + it "should send correct data for rails style checked fields" do @response.stubs(:body).returns(<<-EOS) @@ -224,7 +220,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_default_collection_fields + it "should send default collection fields" do @response.stubs(:body).returns(<<-EOS) @@ -246,7 +242,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_not_send_default_unchecked_fields + it "should not send default unchecked fields" do @response.stubs(:body).returns(<<-EOS) @@ -257,7 +253,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_default_textarea_values + it "should send default textarea values" do @response.stubs(:body).returns(<<-EOS) @@ -268,7 +264,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_default_selected_option_value_from_select + it "should send default selected option value from select" do @response.stubs(:body).returns(<<-EOS) @@ -296,7 +292,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_first_select_option_value_when_no_option_selected + it "should send first select option value when no option selected" do @response.stubs(:body).returns(<<-EOS) @@ -322,7 +318,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_send_default_empty_text_field_values + it "should send default empty text field values" do @response.stubs(:body).returns(<<-EOS) @@ -333,7 +329,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_recognize_button_tags + it "should recognize button tags" do @response.stubs(:body).returns(<<-EOS) @@ -344,7 +340,7 @@ class ClicksButtonTest < Test::Unit::TestCase @session.clicks_button end - def test_should_recognize_button_tags_by_content + it "should recognize button tags by content" do @response.stubs(:body).returns(<<-EOS) diff --git a/test/clicks_link_test.rb b/spec/clicks_link_spec.rb similarity index 78% rename from test/clicks_link_test.rb rename to spec/clicks_link_spec.rb index 3b1c7e9..26132c5 100644 --- a/test/clicks_link_test.rb +++ b/spec/clicks_link_spec.rb @@ -1,14 +1,14 @@ -require File.dirname(__FILE__) + "/helper" +require File.expand_path(File.dirname(__FILE__) + "/spec_helper") -class ClicksLinkTest < Test::Unit::TestCase - def setup +describe "clicks_link" do + before do @session = ActionController::Integration::Session.new @session.stubs(:assert_response) @session.stubs(:get_via_redirect) @session.stubs(:response).returns(@response=mock) end - def test_should_use_get_by_default + it "should use get by default" do @response.stubs(:body).returns(<<-EOS) Link text EOS @@ -16,7 +16,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link "Link text" end - def test_should_click_get_links + it "should click get links" do @response.stubs(:body).returns(<<-EOS) Link text EOS @@ -24,7 +24,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_get_link "Link text" end - def test_should_click_delete_links + it "should click delete links" do @response.stubs(:body).returns(<<-EOS) Link text EOS @@ -32,7 +32,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_delete_link "Link text" end - def test_should_click_post_links + it "should click post links" do @response.stubs(:body).returns(<<-EOS) Link text EOS @@ -40,7 +40,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_post_link "Link text" end - def test_should_click_put_links + it "should click put links" do @response.stubs(:body).returns(<<-EOS) Link text EOS @@ -48,7 +48,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_put_link "Link text" end - def test_should_click_rails_javascript_links_with_authenticity_tokens + it "should click rails javascript links with authenticity tokens" do @response.stubs(:body).returns(<<-EOS) Link text EOS @@ -127,7 +127,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link "Link text" end - def test_should_not_be_case_sensitive + it "should not be case sensitive" do @response.stubs(:body).returns(<<-EOS) Link text EOS @@ -135,7 +135,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link "LINK TEXT" end - def test_should_match_link_substrings + it "should match link substrings" do @response.stubs(:body).returns(<<-EOS) This is some cool link text, isn't it? EOS @@ -143,7 +143,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link "Link text" end - def test_should_work_with_elements_in_the_link + it "should work with elements in the link" do @response.stubs(:body).returns(<<-EOS) Link text EOS @@ -151,7 +151,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link "Link text" end - def test_should_match_the_first_matching_link + it "should match the first matching link" do @response.stubs(:body).returns(<<-EOS) Link text Link text @@ -160,7 +160,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link "Link text" end - def test_should_choose_the_shortest_link_text_match + it "should choose the shortest link text match" do @response.stubs(:body).returns(<<-EOS) Linkerama Link @@ -170,7 +170,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link "Link" end - def test_should_click_link_within_a_selector + it "should click link within a selector" do @response.stubs(:body).returns(<<-EOS) Link
@@ -182,7 +182,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link_within "#container", "Link" end - def test_should_not_make_request_when_link_is_local_anchor + it "should not make request when link is local anchor" do @response.stubs(:body).returns(<<-EOS) Jump to Section 1 EOS @@ -191,7 +191,7 @@ class ClicksLinkTest < Test::Unit::TestCase @session.clicks_link "Jump to Section 1" end - def test_should_follow_relative_links + it "should follow relative links" do @session.current_page.stubs(:url).returns("/page") @response.stubs(:body).returns(<<-EOS) Jump to sub page @@ -199,8 +199,25 @@ class ClicksLinkTest < Test::Unit::TestCase @session.expects(:get_via_redirect).with("/page/sub", {}) @session.clicks_link "Jump to sub page" end + + it "should follow fully qualified local links" do + @response.stubs(:body).returns(<<-EOS) + Jump to sub page + EOS + @session.expects(:get_via_redirect).with("/page/sub", {}) + @session.clicks_link "Jump to sub page" + end - def test_should_follow_query_parameters + it "should follow fully qualified secure local links" do + @response.stubs(:body).returns(<<-EOS) + Jump to sub page + EOS + @session.expects(:https!).with(true) + @session.expects(:get_via_redirect).with("/page/sub", {}) + @session.clicks_link "Jump to sub page" + end + + it "should follow query parameters" do @session.current_page.stubs(:url).returns("/page") @response.stubs(:body).returns(<<-EOS) Jump to foo bar diff --git a/test/fills_in_test.rb b/spec/fills_in_spec.rb similarity index 83% rename from test/fills_in_test.rb rename to spec/fills_in_spec.rb index 0ebb567..2904b4b 100644 --- a/test/fills_in_test.rb +++ b/spec/fills_in_spec.rb @@ -1,14 +1,14 @@ -require File.dirname(__FILE__) + "/helper" +require File.expand_path(File.dirname(__FILE__) + "/spec_helper") -class FillsInTest < Test::Unit::TestCase - def setup +describe "fills_in" do + before do @session = ActionController::Integration::Session.new @session.stubs(:assert_response) @session.stubs(:get_via_redirect) @session.stubs(:response).returns(@response=mock) end - def test_should_work_with_textareas + it "should work with textareas" do @response.stubs(:body).returns(<<-EOS) @@ -21,7 +21,7 @@ class FillsInTest < Test::Unit::TestCase @session.clicks_button end - def test_should_work_with_password_fields + it "should work with password fields" do @response.stubs(:body).returns(<<-EOS) @@ -33,18 +33,16 @@ class FillsInTest < Test::Unit::TestCase @session.clicks_button end - def test_should_fail_if_input_not_found + it "should fail if input not found" do @response.stubs(:body).returns(<<-EOS) EOS - assert_raises RuntimeError do - @session.fills_in "Email", :with => "foo@example.com" - end + lambda { @session.fills_in "Email", :with => "foo@example.com" }.should raise_error end - def test_should_allow_overriding_default_form_values + it "should allow overriding default form values" do @response.stubs(:body).returns(<<-EOS)
@@ -57,7 +55,7 @@ class FillsInTest < Test::Unit::TestCase @session.clicks_button end - def test_should_choose_the_shortest_label_match + it "should choose the shortest label match" do @response.stubs(:body).returns(<<-EOS) @@ -73,7 +71,7 @@ class FillsInTest < Test::Unit::TestCase @session.clicks_button end - def test_should_choose_the_first_label_match_if_closest_is_a_tie + it "should choose the first label match if closest is a tie" do @response.stubs(:body).returns(<<-EOS) @@ -89,7 +87,7 @@ class FillsInTest < Test::Unit::TestCase @session.clicks_button end - def test_should_anchor_label_matches_to_start_of_label + it "should anchor label matches to start of label" do @response.stubs(:body).returns(<<-EOS) @@ -97,10 +95,10 @@ class FillsInTest < Test::Unit::TestCase
EOS - assert_raises(RuntimeError) { @session.fills_in "mail", :with => "value" } + lambda { @session.fills_in "mail", :with => "value" }.should raise_error end - def test_should_anchor_label_matches_to_word_boundaries + it "should anchor label matches to word boundaries" do @response.stubs(:body).returns(<<-EOS)
@@ -108,10 +106,10 @@ class FillsInTest < Test::Unit::TestCase
EOS - assert_raises(RuntimeError) { @session.fills_in "Email", :with => "value" } + lambda { @session.fills_in "Email", :with => "value" }.should raise_error end - def test_should_work_with_inputs_nested_in_labels + it "should work with inputs nested in labels" do @response.stubs(:body).returns(<<-EOS)