From f3d508b8172ea56b601d2e77a762d911a1b72147 Mon Sep 17 00:00:00 2001 From: "mike.gaffney" Date: Tue, 21 Oct 2008 23:55:58 -0500 Subject: [PATCH 01/32] added another stipulation to the https get test on rails session. Added a test for the link object with 2 simple tests. 2nd was done while looking for a bug --- .gitignore | 4 +++- spec/webrat/core/link_spec.rb | 23 +++++++++++++++++++++++ spec/webrat/rails/rails_session_spec.rb | 3 ++- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100755 spec/webrat/core/link_spec.rb diff --git a/.gitignore b/.gitignore index f07c0b3..1ff7516 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ coverage pkg doc ri -email.txt \ No newline at end of file +email.txt +.project +.loadpath diff --git a/spec/webrat/core/link_spec.rb b/spec/webrat/core/link_spec.rb new file mode 100755 index 0000000..bb2cc7c --- /dev/null +++ b/spec/webrat/core/link_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") + +describe Webrat::Link do +# include Webrat::Link + + before do + @session = mock(Webrat::TestSession) + end + + it "should pass through relative urls" do + link = Webrat::Link.new(@session, {"href" => "/path"}) + @session.should_receive(:request_page).with("/path", :get, {}) + link.click + end + + it "shouldnt put base url onto " do + url = "https://www.example.com/path" + @session.should_receive(:request_page).with(url, :get, {}) + link = Webrat::Link.new(@session, {"href" => url}) + link.click + end + +end \ No newline at end of file diff --git a/spec/webrat/rails/rails_session_spec.rb b/spec/webrat/rails/rails_session_spec.rb index 5495ed4..b5dd5e1 100644 --- a/spec/webrat/rails/rails_session_spec.rb +++ b/spec/webrat/rails/rails_session_spec.rb @@ -57,10 +57,11 @@ describe Webrat::RailsSession do end context "the URL is https://" do - it "should call #https! with true before the request" do + it "should call #https! with true before the request and just pass on the path" do integration_session = mock("integration session", :get_via_redirect => nil) rails_session = Webrat::RailsSession.new(integration_session) integration_session.should_receive(:https!).with(true) + integration_session.should_receive(:get_via_redirect).with("/url", "data", "headers") rails_session.get("https://www.example.com/url", "data", "headers") end end From 0b17495446eac62f7694ceb88c823724f30aaeca Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Mon, 27 Oct 2008 21:29:22 -0500 Subject: [PATCH 02/32] move merb/param_parser to core so it can be used for other sessions other than merb (i.e. mechanize) --- Manifest.txt | 4 ++-- lib/webrat/{merb => core}/param_parser.rb | 0 lib/webrat/{merb => core}/url_encoded_pair_parser.rb | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename lib/webrat/{merb => core}/param_parser.rb (100%) rename lib/webrat/{merb => core}/url_encoded_pair_parser.rb (100%) diff --git a/Manifest.txt b/Manifest.txt index 08b0319..e76f6cc 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -16,16 +16,16 @@ lib/webrat/core/form.rb lib/webrat/core/label.rb lib/webrat/core/link.rb lib/webrat/core/logging.rb +lib/webrat/core/param_parser.rb lib/webrat/core/scope.rb lib/webrat/core/select_option.rb lib/webrat/core/session.rb +lib/webrat/core/url_encoded_pair_parser.rb lib/webrat/mechanize.rb lib/webrat/mechanize/mechanize_session.rb lib/webrat/merb.rb lib/webrat/merb/indifferent_access.rb -lib/webrat/merb/param_parser.rb lib/webrat/merb/support.rb -lib/webrat/merb/url_encoded_pair_parser.rb lib/webrat/rack/rack_session.rb lib/webrat/rails.rb lib/webrat/rails/rails_session.rb diff --git a/lib/webrat/merb/param_parser.rb b/lib/webrat/core/param_parser.rb similarity index 100% rename from lib/webrat/merb/param_parser.rb rename to lib/webrat/core/param_parser.rb diff --git a/lib/webrat/merb/url_encoded_pair_parser.rb b/lib/webrat/core/url_encoded_pair_parser.rb similarity index 100% rename from lib/webrat/merb/url_encoded_pair_parser.rb rename to lib/webrat/core/url_encoded_pair_parser.rb From 5efa47aa530b64e6b17b9ecdb9282a15498d0d1c Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Mon, 27 Oct 2008 21:58:21 -0500 Subject: [PATCH 03/32] fixed requires and manifest --- Manifest.txt | 11 +++-------- lib/webrat/core.rb | 1 + lib/webrat/core/field.rb | 1 - lib/webrat/core/param_parser.rb | 2 +- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Manifest.txt b/Manifest.txt index 90c67d6..a811ec6 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -7,6 +7,9 @@ TODO.txt init.rb install.rb lib/webrat.rb +lib/webrat/core_extensions/blank.rb +lib/webrat/core_extensions/hash_with_indifferent_access.rb +lib/webrat/core_extensions/nil_to_param.rb lib/webrat/core.rb lib/webrat/core/area.rb lib/webrat/core/assertions.rb @@ -24,8 +27,6 @@ lib/webrat/core/url_encoded_pair_parser.rb lib/webrat/mechanize.rb lib/webrat/mechanize/mechanize_session.rb lib/webrat/merb.rb -lib/webrat/merb/indifferent_access.rb -lib/webrat/merb/support.rb lib/webrat/rack/rack_session.rb lib/webrat/rails.rb lib/webrat/rails/rails_session.rb @@ -40,10 +41,6 @@ lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js lib/webrat/selenium/selenium_session.rb lib/webrat/sinatra/sinatra_session.rb -manifest.diff -mechanize_spike.rb -selenium_spike.rb -spec/api/attaches_file_spec.rb spec/api/basic_auth_spec.rb spec/api/checks_spec.rb spec/api/chooses_spec.rb @@ -53,14 +50,12 @@ spec/api/clicks_link_spec.rb spec/api/fills_in_spec.rb spec/api/reloads_spec.rb spec/api/save_and_open_spec.rb -spec/api/selects_date_spec.rb spec/api/selects_spec.rb spec/api/should_not_see_spec.rb spec/api/should_see_spec.rb spec/api/visits_spec.rb spec/api/within_spec.rb spec/fakes/test_session.rb -spec/integration/rails_spec.rb spec/rcov.opts spec/spec_helper.rb spec/webrat/core/logging_spec.rb diff --git a/lib/webrat/core.rb b/lib/webrat/core.rb index b7aaf4f..8a3ae30 100644 --- a/lib/webrat/core.rb +++ b/lib/webrat/core.rb @@ -1,5 +1,6 @@ require "webrat/core/logging" require "webrat/core/flunk" +require "webrat/core/param_parser" require "webrat/core/form" require "webrat/core/scope" require "webrat/core/link" diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 31e244b..b94ec2b 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -1,7 +1,6 @@ require "cgi" require "webrat/core_extensions/blank" require "webrat/core_extensions/nil_to_param" -require "webrat/merb/param_parser" module Webrat class Field diff --git a/lib/webrat/core/param_parser.rb b/lib/webrat/core/param_parser.rb index d2b55c7..c570039 100644 --- a/lib/webrat/core/param_parser.rb +++ b/lib/webrat/core/param_parser.rb @@ -1,6 +1,6 @@ require "cgi" require "webrat/core_extensions/blank" -require "webrat/merb/url_encoded_pair_parser" +require "webrat/core/url_encoded_pair_parser" module Webrat class ParamParser From 71adad4ada338289f8773a786a959b34101c50ac Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Mon, 27 Oct 2008 22:01:27 -0500 Subject: [PATCH 04/32] the manifest was missing core/mime.rb --- Manifest.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/Manifest.txt b/Manifest.txt index a811ec6..f18d97e 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -19,6 +19,7 @@ lib/webrat/core/form.rb lib/webrat/core/label.rb lib/webrat/core/link.rb lib/webrat/core/logging.rb +lib/webrat/core/mime.rb lib/webrat/core/param_parser.rb lib/webrat/core/scope.rb lib/webrat/core/select_option.rb From 40c6b6ed4495d0534e81286d70c37c058f11d193 Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Mon, 27 Oct 2008 22:01:48 -0500 Subject: [PATCH 05/32] "fixed" MechanizeSession, Session expects get and post to have 3 arguments --- lib/webrat/mechanize/mechanize_session.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/webrat/mechanize/mechanize_session.rb b/lib/webrat/mechanize/mechanize_session.rb index 7a817c4..fe607a6 100644 --- a/lib/webrat/mechanize/mechanize_session.rb +++ b/lib/webrat/mechanize/mechanize_session.rb @@ -6,11 +6,11 @@ module Webrat @mechanize = mechanize end - def get(url, data) + def get(url, data, headers_argument_not_used = nil) @mechanize_page = @mechanize.get(url, data) end - def post(url, data) + def post(url, data, headers_argument_not_used = nil) @mechanize_page = @mechanize.post(url, data) end From 275829d382140f42e7688c246b2a76140c953cc5 Mon Sep 17 00:00:00 2001 From: gaffo Date: Tue, 28 Oct 2008 21:09:23 -0500 Subject: [PATCH 06/32] [#25 state:open] Added tests to create matches_id? function in link --- lib/webrat/core/link.rb | 11 ++++++++ spec/webrat/core/link_spec.rb | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index 89e532f..b10fb0c 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -33,11 +33,22 @@ module Webrat html =~ matcher || title =~ matcher end + def matches_id?(id_or_regexp) + if id_or_regexp.is_a?(Regexp) + (id =~ id_or_regexp) ? true : false + else + (id == id_or_regexp) ? true : false + end + end + def text @element.innerHTML end protected + def id + @element['id'] + end def data authenticity_token.blank? ? {} : {"authenticity_token" => authenticity_token} diff --git a/spec/webrat/core/link_spec.rb b/spec/webrat/core/link_spec.rb index bb2cc7c..7c416d8 100755 --- a/spec/webrat/core/link_spec.rb +++ b/spec/webrat/core/link_spec.rb @@ -20,4 +20,54 @@ describe Webrat::Link do link.click end + it "should matches_text? on regexp" do + link = Webrat::Link.new(@session, nil) + link.should_receive(:text).and_return("Link Text") + link.matches_text?(/link/i).should == 0 + end + + it "should matches_text? on link_text" do + link = Webrat::Link.new(@session, nil) + link.should_receive(:text).and_return("Link Text") + link.matches_text?("Link Text").should == 0 + end + + it "should not matches_text? on link_text case insensitive" do + link = Webrat::Link.new(@session, nil) + link.should_receive(:text).and_return("Link Text") + link.should_receive(:title).and_return(nil) + link.matches_text?("link_text").should == false + end + + it "should match text including  " do + link = Webrat::Link.new(@session, nil) + link.should_receive(:text).and_return("Link Text") + link.matches_text?("Link Text").should == 0 + end + + it "should not matches_text? on wrong text" do + link = Webrat::Link.new(@session, nil) + link.should_receive(:text).and_return("Some Other Link") + link.should_receive(:title).and_return(nil) + link.matches_text?("Link Text").should == false + end + + it "should matches_id? on exact matching id" do + link = Webrat::Link.new(@session, nil) + link.should_receive(:id).and_return("some_id") + link.matches_id?("some_id").should == true + end + + it "should not matches_id? on incorrect id" do + link = Webrat::Link.new(@session, nil) + link.should_receive(:id).and_return("other_id") + link.matches_id?("some_id").should == false + end + + it "should matches_id? on matching id by regexp" do + link = Webrat::Link.new(@session, nil) + link.should_receive(:id).and_return("some_id") + link.matches_id?(/some/).should == true + end + end \ No newline at end of file From 067bc791c52637c360baf29503599de7ec6f7b56 Mon Sep 17 00:00:00 2001 From: gaffo Date: Tue, 28 Oct 2008 21:13:54 -0500 Subject: [PATCH 07/32] added tests for substrings --- spec/webrat/core/link_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/webrat/core/link_spec.rb b/spec/webrat/core/link_spec.rb index 7c416d8..bf20341 100755 --- a/spec/webrat/core/link_spec.rb +++ b/spec/webrat/core/link_spec.rb @@ -32,6 +32,12 @@ describe Webrat::Link do link.matches_text?("Link Text").should == 0 end + it "should matches_text? on substring" do + link = Webrat::Link.new(@session, nil) + link.should_receive(:text).and_return("Link Text") + link.matches_text?("nk Te").should_not be_nil + end + it "should not matches_text? on link_text case insensitive" do link = Webrat::Link.new(@session, nil) link.should_receive(:text).and_return("Link Text") From 397dec14364c1f7385bccd3213cd01ac1269aef8 Mon Sep 17 00:00:00 2001 From: gaffo Date: Tue, 28 Oct 2008 21:32:55 -0500 Subject: [PATCH 08/32] added id support on clicks_link, added tests for matches_text? and matches_id? into link spec --- lib/webrat/core/scope.rb | 18 +++++++++++++----- spec/api/clicks_link_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index cdc1e7c..13d91bd 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -111,14 +111,22 @@ module Webrat # Passing a :method in the options hash overrides the HTTP method used # for making the link request # + # It will try to find links by (in order of precedence): + # innerHTML, with simple   handling + # title + # id + # + # innerHTML and title are matchable by text subtring or Regexp + # id is matchable by full text equality or Regexp + # # Example: # clicks_link "Sign up" # # clicks_link "Sign up", :javascript => false # # clicks_link "Sign up", :method => :put - def clicks_link(link_text, options = {}) - find_link(link_text).click(options) + def clicks_link(text_or_title_or_id, options = {}) + find_link(text_or_title_or_id).click(options) end alias_method :click_link, :clicks_link @@ -214,15 +222,15 @@ module Webrat end end - def find_link(text, selector = nil) + def find_link(text_or_title_or_id, selector = nil) matching_links = links_within(selector).select do |possible_link| - possible_link.matches_text?(text) + possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id) end if matching_links.any? matching_links.min { |a, b| a.text.length <=> b.text.length } else - flunk("Could not find link with text #{text.inspect}") + flunk("Could not find link with text or title or id #{text_or_title_or_id.inspect}") end end diff --git a/spec/api/clicks_link_spec.rb b/spec/api/clicks_link_spec.rb index 3bfe6f7..a88831a 100644 --- a/spec/api/clicks_link_spec.rb +++ b/spec/api/clicks_link_spec.rb @@ -21,6 +21,14 @@ describe "clicks_link" do @session.clicks_link "Link text", :method => :get end + it "should click link on substring" do + @session.response_body = <<-EOS + Link text + EOS + @session.should_receive(:get).with("/page", {}) + @session.clicks_link "ink tex", :method => :get + end + it "should click delete links" do @session.response_body = <<-EOS Link text @@ -54,6 +62,22 @@ describe "clicks_link" do @session.clicks_link /link [a-z]/i end + it "should click links by id" do + @session.response_body = <<-EOS + Link text + EOS + @session.should_receive(:get).with("/page", {}) + @session.clicks_link "link_text_link" + end + + it "should click links by id regexp" do + @session.response_body = <<-EOS + Link text + EOS + @session.should_receive(:get).with("/page", {}) + @session.clicks_link /_text_/ + end + it "should click rails javascript links with authenticity tokens" do @session.response_body = <<-EOS Date: Mon, 3 Nov 2008 20:44:33 -0600 Subject: [PATCH 10/32] exposed the mechanize page --- lib/webrat/mechanize/mechanize_session.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/webrat/mechanize/mechanize_session.rb b/lib/webrat/mechanize/mechanize_session.rb index fe607a6..cc8e241 100644 --- a/lib/webrat/mechanize/mechanize_session.rb +++ b/lib/webrat/mechanize/mechanize_session.rb @@ -6,6 +6,10 @@ module Webrat @mechanize = mechanize end + def page + @mechanize_page + end + def get(url, data, headers_argument_not_used = nil) @mechanize_page = @mechanize.get(url, data) end From 98d52176ad3208ae2d307a2ed52aaee176105164 Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Wed, 5 Nov 2008 14:20:47 +1100 Subject: [PATCH 11/32] fix following fully qualified local links --- lib/webrat/core/link.rb | 2 +- spec/api/clicks_link_spec.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index 89e532f..b894f7f 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -54,7 +54,7 @@ module Webrat def absolute_href if href =~ /^\?/ "#{@session.current_url}#{href}" - elsif href !~ %r{^https?://www.example.com(/.*)} && (href !~ /^\//) + elsif href !~ %r{^https?://} && (href !~ /^\//) "#{@session.current_url}/#{href}" else href diff --git a/spec/api/clicks_link_spec.rb b/spec/api/clicks_link_spec.rb index 3bfe6f7..b464563 100644 --- a/spec/api/clicks_link_spec.rb +++ b/spec/api/clicks_link_spec.rb @@ -270,10 +270,11 @@ describe "clicks_link" do end it "should follow fully qualified local links" do + @session.stub!(:current_url).and_return("/page") @session.response_body = <<-EOS - Jump to sub page + Jump to sub page EOS - @session.should_receive(:get).with("http://www.example.com/page/sub", {}) + @session.should_receive(:get).with("http://subdomain.example.com/page/sub", {}) @session.clicks_link "Jump to sub page" end From d039c9590f4d4aac18dca033e40150bd61c8f6b9 Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Sat, 8 Nov 2008 08:45:51 -0600 Subject: [PATCH 12/32] ignore the log dir --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1e26df0..61c5eb1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ pkg doc ri email.txt +log From 7dfd59c29cd3eee6e42d0866fc84f10ce200ae93 Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Sat, 8 Nov 2008 08:46:14 -0600 Subject: [PATCH 13/32] the mechanize session must specially handle params {:user => {:name => "Nancy"}} now becomes {"user[name]" => "Nancy"} --- lib/webrat/mechanize/mechanize_session.rb | 11 +++++++++- .../mechanize/mechanize_session_spec.rb | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/webrat/mechanize/mechanize_session.rb b/lib/webrat/mechanize/mechanize_session.rb index cc8e241..fdd731b 100644 --- a/lib/webrat/mechanize/mechanize_session.rb +++ b/lib/webrat/mechanize/mechanize_session.rb @@ -15,7 +15,16 @@ module Webrat end def post(url, data, headers_argument_not_used = nil) - @mechanize_page = @mechanize.post(url, data) + post_data = data.inject({}) do |memo, param| + case param.last + when Hash + param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value } + else + memo[param.first] = param.last + end + memo + end + @mechanize_page = @mechanize.post(url, post_data) end def response_body diff --git a/spec/webrat/mechanize/mechanize_session_spec.rb b/spec/webrat/mechanize/mechanize_session_spec.rb index aa5077e..452d3b7 100644 --- a/spec/webrat/mechanize/mechanize_session_spec.rb +++ b/spec/webrat/mechanize/mechanize_session_spec.rb @@ -12,4 +12,25 @@ describe Webrat::MechanizeSession do @mech.headers.should == {} end end + + describe "post" do + def url + 'http://test.host/users' + end + + def data + {:user => {:first_name => 'Nancy', :last_name => 'Callahan'}} + end + + def flattened_data + {'user[first_name]' => 'Nancy', 'user[last_name]' => 'Callahan'} + end + + it "should flatten model post data" do + mechanize = mock :mechanize + mechanize.should_receive(:post).with(url, flattened_data) + + Webrat::MechanizeSession.new(mechanize).post(url, data) + end + end end \ No newline at end of file From b4bdeb5319ecabddad6cf871fd6bab438e422d6a Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Tue, 11 Nov 2008 00:28:15 -0500 Subject: [PATCH 14/32] Fall back on Hpricot when Nokogiri isn't available (eg. JRuby) --- lib/webrat.rb | 6 +- lib/webrat/core.rb | 1 + lib/webrat/core/field.rb | 8 +- lib/webrat/core/form.rb | 2 +- lib/webrat/core/hpricot.rb | 0 lib/webrat/core/link.rb | 2 +- lib/webrat/core/matchers/have_content.rb | 43 +-------- lib/webrat/core/matchers/have_xpath.rb | 2 +- lib/webrat/core/scope.rb | 10 +- lib/webrat/core/xml.rb | 35 +++++++ spec/api/selects_spec.rb | 2 + spec/webrat/core/field_spec.rb | 4 +- spec/webrat/core/session_spec.rb | 2 +- spec/webrat/nokogiri_spec.rb | 116 ++++++++++++----------- 14 files changed, 116 insertions(+), 117 deletions(-) create mode 100644 lib/webrat/core/hpricot.rb create mode 100644 lib/webrat/core/xml.rb diff --git a/lib/webrat.rb b/lib/webrat.rb index 364dbf1..c4c5118 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -13,16 +13,16 @@ module Webrat end end -# We need Nokogiri's CSS to XPath support, even if using REXML +# We need Nokogiri's CSS to XPath support, even if using REXML and Hpricot for parsing and searching require "nokogiri/css" -# Require nokogiri and fall back on rexml +# Require nokogiri and fall back on rexml+Hpricot begin require "nokogiri" require "webrat/core/nokogiri" rescue LoadError => e + require "hpricot" require "rexml/document" - warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"") end require "webrat/core" diff --git a/lib/webrat/core.rb b/lib/webrat/core.rb index cce4704..ccda801 100644 --- a/lib/webrat/core.rb +++ b/lib/webrat/core.rb @@ -1,3 +1,4 @@ +require "webrat/core/xml" require "webrat/core/nokogiri" require "webrat/core/logging" require "webrat/core/flunk" diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 4649639..065a049 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -111,7 +111,7 @@ module Webrat end unless id.blank? - @label_elements += @form.element.search("label[@for='#{id}']") + @label_elements += Webrat::XML.css_search(@form.element, "label[@for='#{id}']") end @label_elements @@ -311,8 +311,8 @@ module Webrat protected def default_value - selected_options = @element.search(".//option[@selected='selected']") - selected_options = @element.search(".//option[position() = 1]") if selected_options.empty? + selected_options = Webrat::XML.css_search(@element, "option[@selected='selected']") + selected_options = Webrat::XML.css_search(@element, "option:first") if selected_options.empty? selected_options.map do |option| return "" if option.nil? @@ -325,7 +325,7 @@ module Webrat end def option_elements - @element.search(".//option") + Webrat::XML.css_search(@element, "option") end end diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 4e6585f..b07247f 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -39,7 +39,7 @@ module Webrat def fields return @fields if @fields - @fields = (@element.search(".//button", ".//input", ".//textarea", ".//select")).collect do |field_element| + @fields = Webrat::XML.css_search(@element, "button", "input", "textarea", "select").collect do |field_element| Field.class_for_element(field_element).new(self, field_element) end end diff --git a/lib/webrat/core/hpricot.rb b/lib/webrat/core/hpricot.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index bc13655..c7205ad 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -22,7 +22,7 @@ module Webrat end def matches_text?(link_text) - html = text.gsub(' ',' ') + html = text.gsub(' ',' ').gsub(' ', ' ') if link_text.is_a?(Regexp) matcher = link_text diff --git a/lib/webrat/core/matchers/have_content.rb b/lib/webrat/core/matchers/have_content.rb index d2e8c2c..1383137 100644 --- a/lib/webrat/core/matchers/have_content.rb +++ b/lib/webrat/core/matchers/have_content.rb @@ -7,15 +7,7 @@ module Webrat end def matches?(stringlike) - if defined?(Nokogiri::XML) - matches_nokogiri?(stringlike) - else - matches_rexml?(stringlike) - end - end - - def matches_rexml?(stringlike) - @document = rexml_document(stringlike) + @document = Webrat::XML.document(stringlike) @element = @document.inner_text case @content @@ -25,39 +17,6 @@ module Webrat @element.match(@content) end end - - def matches_nokogiri?(stringlike) - @document = Webrat.nokogiri_document(stringlike) - @element = @document.inner_text - - case @content - when String - @element.include?(@content) - when Regexp - @element.match(@content) - end - end - - def rexml_document(stringlike) - stringlike = stringlike.body.to_s if stringlike.respond_to?(:body) - - case stringlike - when REXML::Document - stringlike.root - when REXML::Node - stringlike - when StringIO, String - begin - REXML::Document.new(stringlike.to_s).root - rescue REXML::ParseException => e - if e.message.include?("second root element") - REXML::Document.new("#{stringlike}").root - else - raise e - end - end - end - end # ==== Returns # String:: The failure message. diff --git a/lib/webrat/core/matchers/have_xpath.rb b/lib/webrat/core/matchers/have_xpath.rb index b3eb96f..740761a 100644 --- a/lib/webrat/core/matchers/have_xpath.rb +++ b/lib/webrat/core/matchers/have_xpath.rb @@ -45,7 +45,7 @@ module Webrat @query = query end - @document = Webrat.nokogiri_document(stringlike) + @document = Webrat::XML.document(stringlike) matched = @document.xpath(*@query) matched.any? && (!@block || @block.call(matched)) end diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 98336ee..07f6f2f 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -166,13 +166,13 @@ module Webrat def page_dom #:nodoc: return @response.dom if @response.respond_to?(:dom) - dom = Webrat.nokogiri_document(@response_body) + dom = Webrat::XML.document(@response_body) Webrat.define_dom_method(@response, dom) return dom end def scoped_dom #:nodoc: - Webrat.nokogiri_document(@scope.dom.search(@selector).first.to_html) + Webrat::XML.document(@scope.dom.search(@selector).first.to_html) end def locate_field(field_locator, *field_types) #:nodoc: @@ -184,13 +184,13 @@ module Webrat end def areas #:nodoc: - dom.search("area").map do |element| + Webrat::XML.css_search(dom, "area").map do |element| Area.new(@session, element) end end def links #:nodoc: - dom.search("a[@href]").map do |link_element| + Webrat::XML.css_search(dom, "a[@href]").map do |link_element| Link.new(@session, link_element) end end @@ -198,7 +198,7 @@ module Webrat def forms #:nodoc: return @forms if @forms - @forms = dom.search("form").map do |form_element| + @forms = Webrat::XML.css_search(dom, "form").map do |form_element| Form.new(@session, form_element) end end diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb new file mode 100644 index 0000000..bf576a0 --- /dev/null +++ b/lib/webrat/core/xml.rb @@ -0,0 +1,35 @@ +module Webrat + module XML + + def self.document(stringlike) + if defined?(Nokogiri::XML) + Webrat.nokogiri_document(stringlike) + else + return stringlike.dom if stringlike.respond_to?(:dom) + + if Hpricot::Doc === stringlike + stringlike + elsif Hpricot::Elements === stringlike + stringlike + elsif StringIO === stringlike + Hpricot(stringlike.string) + elsif stringlike.respond_to?(:body) + Hpricot(stringlike.body.to_s) + else + Hpricot(stringlike.to_s) + end + end + end + + def self.css_search(element, *searches) + if defined?(Nokogiri::XML) + element.css(*searches) + else + searches.map do |search| + element.search(search) + end.flatten.compact + end + end + + end +end \ No newline at end of file diff --git a/spec/api/selects_spec.rb b/spec/api/selects_spec.rb index e2a1d24..7da2d5f 100644 --- a/spec/api/selects_spec.rb +++ b/spec/api/selects_spec.rb @@ -1,5 +1,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") +require "merb" + describe "selects" do before do @session = Webrat::TestSession.new diff --git a/spec/webrat/core/field_spec.rb b/spec/webrat/core/field_spec.rb index 84172d4..98ff995 100644 --- a/spec/webrat/core/field_spec.rb +++ b/spec/webrat/core/field_spec.rb @@ -3,12 +3,12 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") module Webrat describe CheckboxField do it "should say it is checked if it is" do - checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("")/'input').first) + checkbox = CheckboxField.new(nil, (Webrat::XML.document("").search('input')).first) checkbox.should be_checked end it "should say it is not checked if it is not" do - checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("")/'input').first) + checkbox = CheckboxField.new(nil, (Webrat::XML.document("").search('input')).first) checkbox.should_not be_checked end end diff --git a/spec/webrat/core/session_spec.rb b/spec/webrat/core/session_spec.rb index fc7c9df..25b2b90 100644 --- a/spec/webrat/core/session_spec.rb +++ b/spec/webrat/core/session_spec.rb @@ -18,7 +18,7 @@ describe Webrat::Session do "" end - session.current_dom.should be_an_instance_of(Nokogiri::HTML::Document) + session.current_dom.should respond_to(:search) end it "should open the page in the browser" do diff --git a/spec/webrat/nokogiri_spec.rb b/spec/webrat/nokogiri_spec.rb index ccbc62d..733481c 100644 --- a/spec/webrat/nokogiri_spec.rb +++ b/spec/webrat/nokogiri_spec.rb @@ -1,75 +1,77 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") -describe "Nokogiri Extension" do - include Webrat::Matchers +if defined?(Nokogiri::XML) + describe "Nokogiri Extension" do + include Webrat::Matchers - def fail - raise_error(Spec::Expectations::ExpectationNotMetError) - end + def fail + raise_error(Spec::Expectations::ExpectationNotMetError) + end - before(:each) do - @text_and_password = <<-HTML -
- - - -
- HTML + before(:each) do + @text_and_password = <<-HTML +
+ + + +
+ HTML - @text_only = <<-HTML -
- -
- HTML + @text_only = <<-HTML +
+ +
+ HTML - @password_only = <<-HTML -
- -
- HTML - end + @password_only = <<-HTML +
+ +
+ HTML + end - describe ":text" do - it "passes have_selector(:text) if a node with type=text exists" do - @text_and_password.should have_selector(":text") - end + describe ":text" do + it "passes have_selector(:text) if a node with type=text exists" do + @text_and_password.should have_selector(":text") + end - it "passes not have_selector(:text) if no node with text=text exists" do - @password_only.should_not have_selector(":text") - end + it "passes not have_selector(:text) if no node with text=text exists" do + @password_only.should_not have_selector(":text") + end - it "fails have_selector(:text) if no node with type=text exists" do - lambda { @password_only.should have_selector(":text") }.should fail - end + it "fails have_selector(:text) if no node with type=text exists" do + lambda { @password_only.should have_selector(":text") }.should fail + end - it "fails not have_selector(:text) if a node with type=text exists" do - lambda { @text_only.should_not have_selector(":text") }.should fail - end + it "fails not have_selector(:text) if a node with type=text exists" do + lambda { @text_only.should_not have_selector(":text") }.should fail + end - it "works together with other selectors" do - @text_and_password.should have_selector("input:text[type*='te']") + it "works together with other selectors" do + @text_and_password.should have_selector("input:text[type*='te']") + end end - end - describe ":password" do - it "passes have_selector(:password) if a node with type=password exists" do - @text_and_password.should have_selector(":password") - end + describe ":password" do + it "passes have_selector(:password) if a node with type=password exists" do + @text_and_password.should have_selector(":password") + end - it "passes not have_selector(:text) if no node with text=text exists" do - @text_only.should_not have_selector(":password") - end + it "passes not have_selector(:text) if no node with text=text exists" do + @text_only.should_not have_selector(":password") + end - it "fails have_selector(:password) if no node with type=password exists" do - lambda { @text_only.should have_selector(":password") }.should fail - end + it "fails have_selector(:password) if no node with type=password exists" do + lambda { @text_only.should have_selector(":password") }.should fail + end - it "fails not have_selector(:password) if a node with type=password exists" do - lambda { @password_only.should_not have_selector(":password") }.should fail - end + it "fails not have_selector(:password) if a node with type=password exists" do + lambda { @password_only.should_not have_selector(":password") }.should fail + end - it "works together with other selectors" do - @text_and_password.should have_selector("input:password[type*='pa']") - end + it "works together with other selectors" do + @text_and_password.should have_selector("input:password[type*='pa']") + end + end end -end +end \ No newline at end of file From 255788e14b85da9aad094d3fdeb03f2df12e500c Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Tue, 11 Nov 2008 19:38:30 -0600 Subject: [PATCH 15/32] spec.opts --- spec/spec.opts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/spec.opts b/spec/spec.opts index e69de29..c49b982 100644 --- a/spec/spec.opts +++ b/spec/spec.opts @@ -0,0 +1,2 @@ +--diff +--color \ No newline at end of file From a570d40f7056e977a4583a4baecb8444a7ffcce0 Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Tue, 11 Nov 2008 20:08:17 -0600 Subject: [PATCH 16/32] core expects a response so mechanize got one --- lib/webrat/mechanize.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/webrat/mechanize.rb b/lib/webrat/mechanize.rb index e60b020..77d0f77 100644 --- a/lib/webrat/mechanize.rb +++ b/lib/webrat/mechanize.rb @@ -3,17 +3,16 @@ require "mechanize" module Webrat class MechanizeSession < Session + attr_accessor :response + alias :page :response + def initialize(mechanize = WWW::Mechanize.new) super() @mechanize = mechanize end - def page - @mechanize_page - end - def get(url, data, headers_argument_not_used = nil) - @mechanize_page = @mechanize.get(url, data) + @response = @mechanize.get(url, data) end def post(url, data, headers_argument_not_used = nil) @@ -26,15 +25,15 @@ module Webrat end memo end - @mechanize_page = @mechanize.post(url, post_data) + @response = @mechanize.post(url, post_data) end def response_body - @mechanize_page.content + @response.content end def response_code - @mechanize_page.code.to_i + @response.code.to_i end end From 57d2bd2b90868f231e1638a405819e06c4c4bf0a Mon Sep 17 00:00:00 2001 From: Low Chin Chau Date: Wed, 12 Nov 2008 14:48:18 +0800 Subject: [PATCH 17/32] fixed webrat/core/methods to recognise singular verbs --- lib/webrat/core/methods.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/webrat/core/methods.rb b/lib/webrat/core/methods.rb index e1286f6..9d3e306 100644 --- a/lib/webrat/core/methods.rb +++ b/lib/webrat/core/methods.rb @@ -23,19 +23,19 @@ module Webrat :within, :header, :http_accept, :basic_auth, :save_and_open_page, - :fill_in, - :check, - :uncheck, - :choose, - :select, - :attach_file, + :fills_in, :fill_in, + :checks, :check, + :unchecks, :uncheck, + :chooses, :choose, + :selects, :select, + :attaches_file, :attach_file, :cookies, :response, :current_page, :current_url, - :click_link, - :click_area, - :click_button, + :clicks_link, :click_link, + :clicks_area, :click_area, + :clicks_button, :click_button, :reload, :reloads, :clicks_link_within, :field_labeled From e51238e471622f2a39d3939ac9c49573f0772df3 Mon Sep 17 00:00:00 2001 From: Low Chin Chau Date: Wed, 12 Nov 2008 14:50:50 +0800 Subject: [PATCH 18/32] fixed webrat/core/methods to recognise for click link within --- lib/webrat/core/methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/webrat/core/methods.rb b/lib/webrat/core/methods.rb index 9d3e306..43f7c44 100644 --- a/lib/webrat/core/methods.rb +++ b/lib/webrat/core/methods.rb @@ -37,7 +37,7 @@ module Webrat :clicks_area, :click_area, :clicks_button, :click_button, :reload, :reloads, - :clicks_link_within, + :clicks_link_within, :click_link_within, :field_labeled end From 032a016564e32ad1d23dd464fb9f40ed6d628a1e Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Wed, 12 Nov 2008 12:38:12 -0600 Subject: [PATCH 19/32] add simple support for accessing have_tag, have_selector and have_xpath matchers from rspec proper --- lib/webrat/rspec-rails.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 lib/webrat/rspec-rails.rb diff --git a/lib/webrat/rspec-rails.rb b/lib/webrat/rspec-rails.rb new file mode 100644 index 0000000..ad81883 --- /dev/null +++ b/lib/webrat/rspec-rails.rb @@ -0,0 +1,13 @@ +# Supports using the matchers in controller, helper, and view specs if you're +# using rspec-rails. Just add a require statement to spec/spec_helper.rb: +# +# require 'webrat/rspec-rails' +# +require "webrat/core/matchers" + +Spec::Runner.configure do |config| + # rspec should support :type => [:controller, :helper, :view] - but until it does ... + config.include(Webrat::Matchers, :type => :controller) + config.include(Webrat::Matchers, :type => :helper) + config.include(Webrat::Matchers, :type => :view) +end \ No newline at end of file From 0f7a277c577a3cdf5765147066ec80e0b570cd1b Mon Sep 17 00:00:00 2001 From: Miha Filej Date: Thu, 13 Nov 2008 15:00:24 +0100 Subject: [PATCH 20/32] Make Webrat::Label#text strip out newlines --- lib/webrat/core/label.rb | 6 +++++- spec/api/field_labeled_spec.rb | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/webrat/core/label.rb b/lib/webrat/core/label.rb index 4f3d77d..7107f10 100644 --- a/lib/webrat/core/label.rb +++ b/lib/webrat/core/label.rb @@ -11,7 +11,11 @@ module Webrat end def text - @element.inner_text + str = @element.inner_text + str.gsub!("\n","") + str.strip! + str.squeeze!(" ") + str end end diff --git a/spec/api/field_labeled_spec.rb b/spec/api/field_labeled_spec.rb index 6c0e955..ebfe2f2 100644 --- a/spec/api/field_labeled_spec.rb +++ b/spec/api/field_labeled_spec.rb @@ -108,4 +108,21 @@ describe "field_labeled" do with_an_id_of "element_42", :for => "The Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" end + + describe "finding a field with it's label containing newlines" do + using_this_html <<-HTML +
+ + +
+ HTML + + should_return_a Webrat::TextField, :for => "A label with a link on it's own line" + with_an_id_of "element_42", :for => "A label with a link on it's own line" + should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" + end + end From f1df102428af22960e0925506fd3f604927d48b3 Mon Sep 17 00:00:00 2001 From: Nando Date: Mon, 11 Aug 2008 18:55:51 +0200 Subject: [PATCH 21/32] Fixed README filename --- install.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.rb b/install.rb index bd23f1b..164c9ca 100644 --- a/install.rb +++ b/install.rb @@ -1 +1 @@ -puts IO.read(File.join(File.dirname(__FILE__), 'README')) +puts IO.read(File.join(File.dirname(__FILE__), 'README.txt')) From 9b3702a7d2bac7a646bb74a38fb0a941963aa9e8 Mon Sep 17 00:00:00 2001 From: Low Chin Chau Date: Wed, 12 Nov 2008 14:50:50 +0800 Subject: [PATCH 22/32] fixed webrat/core/methods to recognise for click link within --- lib/webrat/core/methods.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/webrat/core/methods.rb b/lib/webrat/core/methods.rb index e1286f6..fc0ef7b 100644 --- a/lib/webrat/core/methods.rb +++ b/lib/webrat/core/methods.rb @@ -37,7 +37,7 @@ module Webrat :click_area, :click_button, :reload, :reloads, - :clicks_link_within, + :clicks_link_within, :click_link_within, :field_labeled end From b3ad31ee95a93e6a8ae560c0c5bd1b27541629d0 Mon Sep 17 00:00:00 2001 From: Jeremy Burks Date: Tue, 11 Nov 2008 19:38:30 -0600 Subject: [PATCH 23/32] spec.opts --- spec/spec.opts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/spec.opts b/spec/spec.opts index e69de29..c49b982 100644 --- a/spec/spec.opts +++ b/spec/spec.opts @@ -0,0 +1,2 @@ +--diff +--color \ No newline at end of file From ad8956070788cf3fa0f2ea5cbcb33867028c8605 Mon Sep 17 00:00:00 2001 From: Nando Date: Tue, 11 Nov 2008 23:38:17 +0100 Subject: [PATCH 24/32] Multiple test environments support --- init.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.rb b/init.rb index e19fb6a..d4f35a9 100644 --- a/init.rb +++ b/init.rb @@ -1,3 +1,3 @@ -if RAILS_ENV == "test" || RAILS_ENV == "selenium" +unless (RAILS_ENV =~ /^test/).nil? and RAILS_ENV != "selenium" require File.join(File.dirname(__FILE__), "lib", "webrat") end From 6ccb8a6c3a86bd163f160b321851644c4cceda41 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 13 Nov 2008 22:20:39 -0500 Subject: [PATCH 25/32] Flip compound conditional to if --- init.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.rb b/init.rb index d4f35a9..9e01cd9 100644 --- a/init.rb +++ b/init.rb @@ -1,3 +1,3 @@ -unless (RAILS_ENV =~ /^test/).nil? and RAILS_ENV != "selenium" +if (RAILS_ENV =~ /^test/) || RAILS_ENV == "selenium" require File.join(File.dirname(__FILE__), "lib", "webrat") end From db81a97e427d4e780f23ddc3f64ff3c6f521af0f Mon Sep 17 00:00:00 2001 From: Nando Date: Mon, 11 Aug 2008 20:33:01 +0200 Subject: [PATCH 26/32] Added fire_event wrapper in Selenium sessions --- lib/webrat/selenium/selenium_session.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index a32787c..237674a 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -109,7 +109,12 @@ module Webrat def dragdrop(*args) #:nodoc: @selenium.dragdrop(*args) end - + + def fire_event(field_identifier, event) + locator = "webrat=#{Regexp.escape(field_identifier)}" + @selenium.fire_event(locator, "#{event}") + end + protected def adjust_if_regexp(text_or_regexp) #:nodoc: From 92b4814392592bdc3b6fe6663f48294c583722ef Mon Sep 17 00:00:00 2001 From: Nando Date: Mon, 11 Aug 2008 20:58:01 +0200 Subject: [PATCH 27/32] Added key_down and key_up wrapper in Selenium session --- lib/webrat/selenium/selenium_session.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index 237674a..da01141 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -115,6 +115,16 @@ module Webrat @selenium.fire_event(locator, "#{event}") end + def key_down(field_identifier, key_code) + locator = "webrat=#{Regexp.escape(field_identifier)}" + @selenium.key_down(locator, key_code) + end + + def key_up(field_identifier, key_code) + locator = "webrat=#{Regexp.escape(field_identifier)}" + @selenium.key_up(locator, key_code) + end + protected def adjust_if_regexp(text_or_regexp) #:nodoc: From 5cbd5c4a1242125446b648ee06939ebfdc727061 Mon Sep 17 00:00:00 2001 From: Nando Date: Tue, 12 Aug 2008 15:29:39 +0200 Subject: [PATCH 28/32] Ignore .svn --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 1e26df0..18c8458 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ pkg doc ri email.txt +.svn + From e1444d58ef768665987bd1717e414c9463e8263f Mon Sep 17 00:00:00 2001 From: gaffo Date: Thu, 13 Nov 2008 22:33:33 -0600 Subject: [PATCH 29/32] added support for windows and cygwin --- lib/webrat/core/session.rb | 15 +++++++++++++-- spec/webrat/core/session_spec.rb | 17 ++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 2a9f5dd..c1fec8e 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -152,8 +152,13 @@ module Webrat alias_method :visits, :visit - def open_in_browser(path) #:nodoc - `open #{path}` + def open_in_browser(path) # :nodoc + platform = ruby_platform + if platform =~ /cygwin/ || platform =~ /win32/ + `rundll32 url.dll,FileProtocolHandler #{path.gsub("/", "\\\\")}` + elsif platform =~ /darwin/ + `open #{path}` + end end def rewrite_css_and_image_references(response_html) #:nodoc @@ -186,5 +191,11 @@ module Webrat def_delegators :current_scope, :should_see def_delegators :current_scope, :should_not_see def_delegators :current_scope, :field_labeled + + private + # accessor for testing + def ruby_platform + RUBY_PLATFORM + end end end diff --git a/spec/webrat/core/session_spec.rb b/spec/webrat/core/session_spec.rb index fc7c9df..f8a1e4d 100644 --- a/spec/webrat/core/session_spec.rb +++ b/spec/webrat/core/session_spec.rb @@ -21,12 +21,27 @@ describe Webrat::Session do session.current_dom.should be_an_instance_of(Nokogiri::HTML::Document) end - it "should open the page in the browser" 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.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.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.should_receive(:`).with("rundll32 url.dll,FileProtocolHandler path\\to\\file") + session.open_in_browser("path/to/file") + end + it "should provide a current_page for backwards compatibility" do session = Webrat::Session.new current_page = session.current_page From 82b155371931b3e4733ba7f152ad5cc23fa1a3c3 Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Fri, 14 Nov 2008 01:09:18 -0500 Subject: [PATCH 30/32] Added RadioField#checked? to indicated whether or not a radio button is checked. --- lib/webrat/core/field.rb | 4 ++++ spec/webrat/core/field_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 065a049..27200fa 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -241,6 +241,10 @@ module Webrat set(@element["value"] || "on") end + def checked? + @element["checked"] == "checked" + end + protected def other_options diff --git a/spec/webrat/core/field_spec.rb b/spec/webrat/core/field_spec.rb index 98ff995..6b7e015 100644 --- a/spec/webrat/core/field_spec.rb +++ b/spec/webrat/core/field_spec.rb @@ -12,4 +12,16 @@ module Webrat checkbox.should_not be_checked end end + + describe RadioField do + it "should say it is checked if it is" do + radio_button = RadioField.new(nil, (Webrat::XML.document("").search('input')).first) + radio_button.should be_checked + end + + it "should say it is not checked if it is not" do + radio_button = RadioField.new(nil, (Webrat::XML.document("").search('input')).first) + radio_button.should_not be_checked + end + end end From 2603ad405be738814f841a043ab6029afa4ec364 Mon Sep 17 00:00:00 2001 From: Ryan Briones Date: Thu, 13 Nov 2008 23:35:30 -0500 Subject: [PATCH 31/32] allow mechanize session to pass through basic auth --- lib/webrat/mechanize.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/webrat/mechanize.rb b/lib/webrat/mechanize.rb index 77d0f77..f3be0f1 100644 --- a/lib/webrat/mechanize.rb +++ b/lib/webrat/mechanize.rb @@ -35,6 +35,8 @@ module Webrat def response_code @response.code.to_i end + + def_delegators :@mechanize, :basic_auth end -end \ No newline at end of file +end From 3e70fd24ebaebabf7a2f1bb1277d2ee4a6b0fba6 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 14 Nov 2008 11:41:56 -0500 Subject: [PATCH 32/32] Make #value on Field readable --- lib/webrat/core/field.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 27200fa..7aeae82 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -20,6 +20,8 @@ module Webrat raise "Invalid field element: #{element.inspect}" end + attr_reader :value + def initialize(form, element) @form = form @element = element