From f7addd1d38dc85a51dc5ec4dd4e50790b398fbd4 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Tue, 25 Nov 2008 14:07:42 -0500 Subject: [PATCH 001/103] Add IRC channel to README --- README.rdoc | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rdoc b/README.rdoc index d9416a1..651eb0d 100644 --- a/README.rdoc +++ b/README.rdoc @@ -4,6 +4,7 @@ - http://groups.google.com/group/webrat - http://webrat.lighthouseapp.com/ - http://github.com/brynary/webrat +- #webrat on Freenode == Description From aecd70dd150f68001a25c211cb5206aa704d99e1 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 26 Nov 2008 08:32:22 -0500 Subject: [PATCH 002/103] Add pending spec for bug. Webrat needs to escape quotes in XPath --- spec/api/locators/field_with_id_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 spec/api/locators/field_with_id_spec.rb diff --git a/spec/api/locators/field_with_id_spec.rb b/spec/api/locators/field_with_id_spec.rb new file mode 100644 index 0000000..b42a1c2 --- /dev/null +++ b/spec/api/locators/field_with_id_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") + + +describe "field_by_xpath" do + it "should work when there is a single quote in the ID" do + pending "needs bug fix" do + with_html <<-HTML + +
+ +
+ + HTML + + field_with_id("user's name").id.should == "user's name" + end + end +end From 557d9711e52bc5f9748ffa6f2fef1488ad60fe4f Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 26 Nov 2008 13:47:36 -0500 Subject: [PATCH 003/103] Correcting specdoc --- spec/api/locators/field_with_id_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/api/locators/field_with_id_spec.rb b/spec/api/locators/field_with_id_spec.rb index b42a1c2..a495bc8 100644 --- a/spec/api/locators/field_with_id_spec.rb +++ b/spec/api/locators/field_with_id_spec.rb @@ -1,7 +1,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") -describe "field_by_xpath" do +describe "field_with_id" do it "should work when there is a single quote in the ID" do pending "needs bug fix" do with_html <<-HTML From 54de30032e7a6be5ef0321a3fcab318029dad0f8 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 26 Nov 2008 14:24:58 -0500 Subject: [PATCH 004/103] Updating Webrat require in init.rb --- init.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.rb b/init.rb index 9e01cd9..562d914 100644 --- a/init.rb +++ b/init.rb @@ -1,3 +1,3 @@ if (RAILS_ENV =~ /^test/) || RAILS_ENV == "selenium" - require File.join(File.dirname(__FILE__), "lib", "webrat") + require "webrat/rails" end From 01fcd0dea1819a0e999d5d7f7cb3b0c0e493c508 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 26 Nov 2008 14:25:42 -0500 Subject: [PATCH 005/103] Making Webrat's usage of the #path Nokogiri method work with Hpricot too --- lib/webrat/core/field.rb | 6 +++++- lib/webrat/core/form.rb | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 75c0d96..8a48b89 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -43,7 +43,11 @@ module Webrat end def path - @element.path + if Webrat.configuration.parse_with_nokogiri? + @element.path + else + @element.xpath + end end def matches_id?(id) diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 1a9eea4..8301c98 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -19,7 +19,15 @@ module Webrat end def field_by_element(element, *field_types) - fields_by_type(field_types).detect { |possible_field| possible_field.path == element.path } + if Webrat.configuration.parse_with_nokogiri? + expected_path = element.path + else + expected_path = element.xpath + end + + fields_by_type(field_types).detect do |possible_field| + possible_field.path == element.xpath + end end def find_select_option(option_text) From 4f530cfa295259a3e0a3354fae6e77f242a19309 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 26 Nov 2008 14:26:10 -0500 Subject: [PATCH 006/103] Extending ActionController::IntegrationTest instead of ActionController::Integration::Session fixes error with Webrat's select method --- lib/webrat/core/methods.rb | 5 ++--- lib/webrat/core/session.rb | 1 + lib/webrat/rails.rb | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/webrat/core/methods.rb b/lib/webrat/core/methods.rb index 0583665..e75e96c 100644 --- a/lib/webrat/core/methods.rb +++ b/lib/webrat/core/methods.rb @@ -32,8 +32,6 @@ module Webrat :chooses, :choose, :selects, :select, :attaches_file, :attach_file, - :cookies, - :response, :current_page, :current_url, :clicks_link, :click_link, @@ -47,7 +45,8 @@ module Webrat :selects_date, :selects_time, :selects_datetime, :select_date, :select_time, :select_datetime, :wait_for_page_to_load, - :field_by_xpath + :field_by_xpath, + :field_with_id end diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 33e00fb..2b52a96 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -215,6 +215,7 @@ module Webrat def_delegators :current_scope, :should_not_see def_delegators :current_scope, :field_labeled def_delegators :current_scope, :field_by_xpath + def_delegators :current_scope, :field_with_id private # accessor for testing diff --git a/lib/webrat/rails.rb b/lib/webrat/rails.rb index ec2c37e..48a008a 100644 --- a/lib/webrat/rails.rb +++ b/lib/webrat/rails.rb @@ -76,10 +76,12 @@ module ActionController #:nodoc: require "webrat/rails/redirect_actions" include Webrat::RedirectActions end - - include Webrat::Methods end end + + class IntegrationTest + include Webrat::Methods + end end Webrat.configuration.mode = :rails \ No newline at end of file From 8770ccb56386844f4b4bac64f28f3f38e98e8a98 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 26 Nov 2008 20:47:50 -0500 Subject: [PATCH 007/103] Fixing another bug in detecting fields via XPath --- lib/webrat/core/form.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 8301c98..238b145 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -26,7 +26,7 @@ module Webrat end fields_by_type(field_types).detect do |possible_field| - possible_field.path == element.xpath + possible_field.path == expected_path end end From 8a4a7a328fcf6ecc783d050e325b0fd38053aed2 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 26 Nov 2008 21:37:56 -0500 Subject: [PATCH 008/103] Revert "Changing field_named and field_with_id to use XPath" This reverts commit 4a6c6fb2fcea7e8de5cf4ecaa6e85f18d2b2ef78. Conflicts: lib/webrat/core/form.rb --- lib/webrat/core/locators.rb | 38 +++++++++++++++++++++++-------------- lib/webrat/core/scope.rb | 6 +++--- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 73602a0..3e4b96a 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -3,45 +3,55 @@ require "webrat/core_extensions/detect_mapped" module Webrat module Locators - def field_by_xpath(xpath, *field_types) + def field_by_xpath(xpath) element = dom.at(xpath) - return nil unless element - forms.detect_mapped do |form| - form.field_by_element(element, *field_types) + form.field_by_element(element) end end + def field(*args) # :nodoc: + # This is the default locator strategy + find_field_with_id(*args) || + find_field_named(*args) || + field_labeled(*args) || + raise(NotFoundError.new("Could not find field: #{args.inspect}")) + end + def field_labeled(label, *field_types) find_field_labeled(label, *field_types) || raise(NotFoundError.new("Could not find field labeled #{label.inspect}")) end def field_named(name, *field_types) - field_by_xpath("//*[@name='#{id}']", *field_types) || + find_field_named(name, *field_types) || raise(NotFoundError.new("Could not find field named #{name.inspect}")) end def field_with_id(id, *field_types) - field_by_xpath("//*[@id='#{id}']", *field_types) || + find_field_with_id(id, *field_types) || raise(NotFoundError.new("Could not find field with id #{id.inspect}")) end - def field(id, *field_types) # :nodoc: - # This is the default locator strategy - field_by_xpath("//*[@id='#{id}']", *field_types) || - field_by_xpath("//*[@name='#{id}']", *field_types) || - field_labeled(id, *field_types) || - raise(NotFoundError.new("Could not find field: #{args.inspect}")) - end - def find_field_labeled(label, *field_types) #:nodoc: forms.detect_mapped do |form| form.field_labeled(label, *field_types) end end + def find_field_named(name, *field_types) #:nodoc: + forms.detect_mapped do |form| + form.field_named(name, *field_types) + end + end + + def find_field_with_id(id, *field_types) #:nodoc: + forms.detect_mapped do |form| + form.field_with_id(id, *field_types) + end + end + def find_select_option(option_text, id_or_name_or_label) #:nodoc: if id_or_name_or_label field = field(id_or_name_or_label, SelectField) diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 1f47702..7c5eb61 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -134,7 +134,7 @@ module Webrat date_to_select : Date.parse(date_to_select) id_prefix = locate_id_prefix(options) do - year_field = field_by_xpath("//*[contains(@id, '_#{DATE_TIME_SUFFIXES[:year]}')]") + year_field = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/) raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/ $1 end @@ -168,7 +168,7 @@ module Webrat time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select) id_prefix = locate_id_prefix(options) do - hour_field = field_by_xpath("//*[contains(@id, '_#{DATE_TIME_SUFFIXES[:hour]}')]") + hour_field = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/) raise NotFoundError.new("No time fields were found") unless hour_field && hour_field.id =~ /(.*?)_4i/ $1 end @@ -190,7 +190,7 @@ module Webrat def select_datetime(time_to_select, options ={}) time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select) - options[:id_prefix] ||= (options[:from] ? field_by_xpath("//*[@id='#{options[:from]}']") : nil) + options[:id_prefix] ||= (options[:from] ? find_field_with_id(options[:from]) : nil) select_date time, options select_time time, options From 1d5ec1aff97d29ceaf75b790957bfc62ebc8986c Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 27 Nov 2008 00:27:13 -0500 Subject: [PATCH 009/103] Un-pending spec that now works --- spec/api/locators/field_with_id_spec.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/spec/api/locators/field_with_id_spec.rb b/spec/api/locators/field_with_id_spec.rb index a495bc8..0b59798 100644 --- a/spec/api/locators/field_with_id_spec.rb +++ b/spec/api/locators/field_with_id_spec.rb @@ -3,16 +3,14 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") describe "field_with_id" do it "should work when there is a single quote in the ID" do - pending "needs bug fix" do - with_html <<-HTML - -
- -
- - HTML - - field_with_id("user's name").id.should == "user's name" - end + with_html <<-HTML + +
+ +
+ + HTML + + field_with_id("user's name").id.should == "user's name" end end From 226ab89a1a46ee870ba41775589ff86a62510833 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 27 Nov 2008 00:29:20 -0500 Subject: [PATCH 010/103] Fixing up XHTML in some specs --- spec/webrat/core/field_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/webrat/core/field_spec.rb b/spec/webrat/core/field_spec.rb index 6b7e015..7179968 100644 --- a/spec/webrat/core/field_spec.rb +++ b/spec/webrat/core/field_spec.rb @@ -3,24 +3,24 @@ 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::XML.document("").search('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::XML.document("").search('input')).first) + checkbox = CheckboxField.new(nil, (Webrat::XML.document("").search('input')).first) 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 = 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 = RadioField.new(nil, (Webrat::XML.document("").search('input')).first) radio_button.should_not be_checked end end From b1ef30c71d8c3b782e36b60d5e25ec7f99ae803d Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 27 Nov 2008 00:29:47 -0500 Subject: [PATCH 011/103] Removing empty file --- lib/webrat/core/hpricot.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 lib/webrat/core/hpricot.rb diff --git a/lib/webrat/core/hpricot.rb b/lib/webrat/core/hpricot.rb deleted file mode 100644 index e69de29..0000000 From e7ddb4bdd1fbb625f6a2240a98df4547ad7b6355 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 27 Nov 2008 00:33:11 -0500 Subject: [PATCH 012/103] Extracting hpricot_document method --- lib/webrat/core/xml.rb | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index 626dc6c..b3385e1 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -5,19 +5,23 @@ module Webrat #:nodoc: if Webrat.configuration.parse_with_nokogiri? Webrat.nokogiri_document(stringlike) else - return stringlike.dom if stringlike.respond_to?(:dom) + Webrat::XML.hpricot_document(stringlike) + end + end + + def self.hpricot_document(stringlike) + 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 + 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 From 56dc8147f29547848b7e673ca3acba0ed810fb51 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 27 Nov 2008 00:36:07 -0500 Subject: [PATCH 013/103] Unifying on more usage of css_search method --- lib/webrat/core/form.rb | 2 +- lib/webrat/core/scope.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 238b145..d161d6f 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -57,7 +57,7 @@ module Webrat end def labels - @labels ||= element.search("label").map { |element| Label.new(nil, element) } + @labels ||= Webrat::XML.css_search(element, "label").map { |element| Label.new(nil, element) } end def submit diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 7c5eb61..0758bcf 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -289,7 +289,7 @@ module Webrat end def scoped_dom #:nodoc: - Webrat::XML.document(@scope.dom.search(@selector).first.to_html) + Webrat::XML.document(Webrat::XML.css_search(@scope.dom, @selector).first.to_html) end def locate_field(field_locator, *field_types) #:nodoc: From 7ef8fdf7ba0344231c2dae666ecfcb2d1ebedfb1 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 27 Nov 2008 00:40:18 -0500 Subject: [PATCH 014/103] Processing CSS searches as XPath --- lib/webrat/core/xml.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index b3385e1..465a441 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -25,9 +25,16 @@ module Webrat #:nodoc: end end + def self.xpath_search(element, *searches) + searches.flatten.map do |search| + element.xpath(search) + end.flatten.compact + end + def self.css_search(element, *searches) #:nodoc: if Webrat.configuration.parse_with_nokogiri? - element.css(*searches) + xpath_search(element, css_to_xpath(*searches)) + # element.css(*searches) else searches.map do |search| element.search(search) @@ -35,5 +42,11 @@ module Webrat #:nodoc: end end + def self.css_to_xpath(*selectors) + selectors.map do |rule| + Nokogiri::CSS.xpath_for(rule, :prefix => ".//") + end.flatten.uniq + end + end end \ No newline at end of file From 9a344fdc2bd6ed7c3a4956ab9df44e56d30a3f6c Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 27 Nov 2008 00:52:17 -0500 Subject: [PATCH 015/103] Converting usages of element attributes to use a Webrat::XML method --- lib/webrat/core/area.rb | 6 ++--- lib/webrat/core/field.rb | 46 ++++++++++++++++---------------- lib/webrat/core/form.rb | 6 ++--- lib/webrat/core/label.rb | 2 +- lib/webrat/core/link.rb | 8 +++--- lib/webrat/core/select_option.rb | 2 +- lib/webrat/core/xml.rb | 4 +++ 7 files changed, 39 insertions(+), 35 deletions(-) diff --git a/lib/webrat/core/area.rb b/lib/webrat/core/area.rb index b24283e..63f4cd9 100644 --- a/lib/webrat/core/area.rb +++ b/lib/webrat/core/area.rb @@ -18,15 +18,15 @@ module Webrat protected def href - @element["href"] + Webrat::XML.attribute(@element, "href") end def title - @element["title"] + Webrat::XML.attribute(@element, "title") end def id - @element["id"] + Webrat::XML.attribute(@element, "id") end diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 8a48b89..82e02b2 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -11,10 +11,10 @@ module Webrat def self.class_for_element(element) if element.name == "input" - if %w[submit image].include?(element["type"]) + if %w[submit image].include?(Webrat::XML.attribute(element, "type")) field_class = "button" else - field_class = element["type"] || "text" #default type; 'type' attribute is not mandatory + field_class = Webrat::XML.attribute(element, "type") || "text" #default type; 'type' attribute is not mandatory end else field_class = element.name @@ -39,7 +39,7 @@ module Webrat end def id - @element["id"] + Webrat::XML.attribute(@element, "id") end def path @@ -50,16 +50,16 @@ module Webrat end end - def matches_id?(id) - if id.is_a?(Regexp) - @element["id"] =~ id + def matches_id?(expected_id) + if expected_id.is_a?(Regexp) + id =~ expected_id else - @element["id"] == id.to_s + id == expected_id.to_s end end def matches_name?(name) - @element["name"] == name.to_s + Webrat::XML.attribute(@element, "name") == name.to_s end def matches_label?(label_text) @@ -68,11 +68,11 @@ module Webrat end def matches_alt?(alt) - @element["alt"] =~ /^\W*#{Regexp.escape(alt.to_s)}/i + Webrat::XML.attribute(@element, "alt") =~ /^\W*#{Regexp.escape(alt.to_s)}/i end def disabled? - @element.attributes.has_key?("disabled") && @element["disabled"] != 'false' + @element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false' end def raise_error_if_disabled @@ -104,7 +104,7 @@ module Webrat protected def name - @element["name"] + Webrat::XML.attribute(@element, "name") end def escaped_value @@ -136,7 +136,7 @@ module Webrat end def default_value - @element["value"] + Webrat::XML.attribute(@element, "value") end def replace_param_value(params, oval, nval) @@ -163,7 +163,7 @@ module Webrat end def matches_value?(value) - @element["value"] =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value) || matches_alt?(value) + Webrat::XML.attribute(@element, "value") =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value) || matches_alt?(value) end def to_param @@ -177,7 +177,7 @@ module Webrat def click raise_error_if_disabled - set(@element["value"]) unless @element["name"].blank? + set(Webrat::XML.attribute(@element, "value")) unless Webrat::XML.attribute(@element, "name").blank? @form.submit end @@ -216,11 +216,11 @@ module Webrat def check raise_error_if_disabled - set(@element["value"] || "on") + set(Webrat::XML.attribute(@element, "value") || "on") end def checked? - @element["checked"] == "checked" + Webrat::XML.attribute(@element, "checked") == "checked" end def uncheck @@ -231,8 +231,8 @@ module Webrat protected def default_value - if @element["checked"] == "checked" - @element["value"] || "on" + if Webrat::XML.attribute(@element, "checked") == "checked" + Webrat::XML.attribute(@element, "value") || "on" else nil end @@ -256,11 +256,11 @@ module Webrat option.set(nil) end - set(@element["value"] || "on") + set(Webrat::XML.attribute(@element, "value") || "on") end def checked? - @element["checked"] == "checked" + Webrat::XML.attribute(@element, "checked") == "checked" end protected @@ -270,8 +270,8 @@ module Webrat end def default_value - if @element["checked"] == "checked" - @element["value"] || "on" + if Webrat::XML.attribute(@element, "checked") == "checked" + Webrat::XML.attribute(@element, "value") || "on" else nil end @@ -338,7 +338,7 @@ module Webrat selected_options.map do |option| return "" if option.nil? - option["value"] || option.inner_html + Webrat::XML.attribute(option, "value") || option.inner_html end end diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index d161d6f..c9cc668 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -87,7 +87,7 @@ module Webrat end def matches_id?(id) - @element["id"] == id.to_s + Webrat::XML.attribute(@element, "id") == id.to_s end protected @@ -112,11 +112,11 @@ module Webrat end def form_method - @element["method"].blank? ? :get : @element["method"].downcase + Webrat::XML.attribute(@element, "method").blank? ? :get : Webrat::XML.attribute(@element, "method").downcase end def form_action - @element["action"].blank? ? @session.current_url : @element["action"] + Webrat::XML.attribute(@element, "action").blank? ? @session.current_url : Webrat::XML.attribute(@element, "action") end def merge(all_params, new_param) diff --git a/lib/webrat/core/label.rb b/lib/webrat/core/label.rb index 5557108..7a01521 100644 --- a/lib/webrat/core/label.rb +++ b/lib/webrat/core/label.rb @@ -19,7 +19,7 @@ module Webrat end def for_id - @element['for'] + Webrat::XML.attribute(@element, "for") end end diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index 0382213..e1cdb3a 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -49,7 +49,7 @@ module Webrat protected def id - @element['id'] + Webrat::XML.attribute(@element, "id") end def data @@ -57,11 +57,11 @@ module Webrat end def title - @element['title'] + Webrat::XML.attribute(@element, "title") end def href - @element["href"] + Webrat::XML.attribute(@element, "href") end def absolute_href @@ -81,7 +81,7 @@ module Webrat end def onclick - @element["onclick"] + Webrat::XML.attribute(@element, "onclick") end def http_method diff --git a/lib/webrat/core/select_option.rb b/lib/webrat/core/select_option.rb index 461c8ae..f61a162 100644 --- a/lib/webrat/core/select_option.rb +++ b/lib/webrat/core/select_option.rb @@ -22,7 +22,7 @@ module Webrat protected def value - @element["value"] || @element.inner_html + Webrat::XML.attribute(@element, "value") || @element.inner_html end end diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index 465a441..1a2d302 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -24,6 +24,10 @@ module Webrat #:nodoc: Hpricot(stringlike.to_s) end end + + def self.attribute(element, attribute_name) + element[attribute_name] + end def self.xpath_search(element, *searches) searches.flatten.map do |search| From 334108015fbde82c7d65194c2ad8bed657712a07 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 28 Nov 2008 00:12:21 -0500 Subject: [PATCH 016/103] Abstracting access to inner_html and inner_text to Webrat::XML methods --- lib/webrat/core/field.rb | 6 +++--- lib/webrat/core/label.rb | 2 +- lib/webrat/core/link.rb | 4 ++-- lib/webrat/core/matchers/have_content.rb | 2 +- lib/webrat/core/select_option.rb | 6 +++--- lib/webrat/core/xml.rb | 17 ++++++++++++++++- 6 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index 82e02b2..8555b8e 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -159,7 +159,7 @@ module Webrat class ButtonField < Field #:nodoc: def matches_text?(text) - @element.inner_html =~ /#{Regexp.escape(text.to_s)}/i + Webrat::XML.inner_html(@element) =~ /#{Regexp.escape(text.to_s)}/i end def matches_value?(value) @@ -284,7 +284,7 @@ module Webrat protected def default_value - @element.inner_html + Webrat::XML.inner_html(@element) end end @@ -338,7 +338,7 @@ module Webrat selected_options.map do |option| return "" if option.nil? - Webrat::XML.attribute(option, "value") || option.inner_html + Webrat::XML.attribute(option, "value") || Webrat::XML.inner_html(option) end end diff --git a/lib/webrat/core/label.rb b/lib/webrat/core/label.rb index 7a01521..e2e08c0 100644 --- a/lib/webrat/core/label.rb +++ b/lib/webrat/core/label.rb @@ -11,7 +11,7 @@ module Webrat end def text - str = @element.inner_text + str = Webrat::XML.inner_text(@element) str.gsub!("\n","") str.strip! str.squeeze!(" ") diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index e1cdb3a..272f7d9 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -40,11 +40,11 @@ module Webrat end def inner_html - @element.inner_html + Webrat::XML.inner_html(@element) end def text - @element.inner_text + Webrat::XML.inner_text(@element) end protected diff --git a/lib/webrat/core/matchers/have_content.rb b/lib/webrat/core/matchers/have_content.rb index 8344ed2..2c4f43e 100644 --- a/lib/webrat/core/matchers/have_content.rb +++ b/lib/webrat/core/matchers/have_content.rb @@ -8,7 +8,7 @@ module Webrat def matches?(stringlike) @document = Webrat::XML.document(stringlike) - @element = @document.inner_text + @element = Webrat::XML.inner_text(@document) case @content when String diff --git a/lib/webrat/core/select_option.rb b/lib/webrat/core/select_option.rb index f61a162..1a2001f 100644 --- a/lib/webrat/core/select_option.rb +++ b/lib/webrat/core/select_option.rb @@ -8,9 +8,9 @@ module Webrat def matches_text?(text) if text.is_a?(Regexp) - @element.inner_html =~ text + Webrat::XML.inner_html(@element) =~ text else - @element.inner_html == text.to_s + Webrat::XML.inner_html(@element) == text.to_s end end @@ -22,7 +22,7 @@ module Webrat protected def value - Webrat::XML.attribute(@element, "value") || @element.inner_html + Webrat::XML.attribute(@element, "value") || Webrat::XML.inner_html(@element) end end diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index 1a2d302..8a0de4e 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -6,6 +6,7 @@ module Webrat #:nodoc: Webrat.nokogiri_document(stringlike) else Webrat::XML.hpricot_document(stringlike) + # Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html) end end @@ -25,12 +26,26 @@ module Webrat #:nodoc: end end + def self.inner_html(element) + element.inner_html + end + + def self.inner_text(element) + element.inner_text + end + def self.attribute(element, attribute_name) - element[attribute_name] + # case element + # when Nokogiri::XML::Element, Hash + element[attribute_name] + # else + # element.attributes[attribute_name] + # end end def self.xpath_search(element, *searches) searches.flatten.map do |search| + # REXML::XPath.match(element, search) element.xpath(search) end.flatten.compact end From 3744009dd2cbba2da90fe605a0b1e2606bf2f3eb Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 28 Nov 2008 00:14:36 -0500 Subject: [PATCH 017/103] Only run Nokogiri spec when parsing with nokogiri --- spec/webrat/nokogiri_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/webrat/nokogiri_spec.rb b/spec/webrat/nokogiri_spec.rb index 733481c..f39b11e 100644 --- a/spec/webrat/nokogiri_spec.rb +++ b/spec/webrat/nokogiri_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") -if defined?(Nokogiri::XML) +if defined?(Nokogiri::XML) && Webrat.configuration.parse_with_nokogiri? describe "Nokogiri Extension" do include Webrat::Matchers From 31aa659a6732ca0f9637695db960a72fa45ddcdf Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 28 Nov 2008 00:34:35 -0500 Subject: [PATCH 018/103] Ensure all example HTML is wrapped in tags --- spec/api/basic_auth_spec.rb | 2 + spec/api/check_spec.rb | 12 ++++ spec/api/choose_spec.rb | 16 +++++ spec/api/click_area_spec.rb | 16 +++++ spec/api/click_button_spec.rb | 66 +++++++++++++++++++ spec/api/click_link_spec.rb | 86 +++++++++++++++++++++---- spec/api/fill_in_spec.rb | 28 ++++++++ spec/api/locators/field_labeled_spec.rb | 12 ++++ spec/api/matchers_spec.rb | 20 +++--- spec/api/select_date_spec.rb | 8 +++ spec/api/select_datetime_spec.rb | 8 +++ spec/api/select_spec.rb | 28 ++++++++ spec/api/select_time_spec.rb | 8 +++ spec/api/visit_spec.rb | 4 ++ spec/spec_helper.rb | 7 ++ spec/webrat/rails/attaches_file_spec.rb | 10 +++ 16 files changed, 308 insertions(+), 23 deletions(-) diff --git a/spec/api/basic_auth_spec.rb b/spec/api/basic_auth_spec.rb index 16f908d..02156cf 100644 --- a/spec/api/basic_auth_spec.rb +++ b/spec/api/basic_auth_spec.rb @@ -12,9 +12,11 @@ describe "Basic Auth HTTP headers" do it "should be present in form submits" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/form1", {}, {'HTTP_AUTHORIZATION' => "Basic dXNlcjpzZWNyZXQ=\n"}) click_button diff --git a/spec/api/check_spec.rb b/spec/api/check_spec.rb index 8e9bcd4..9254960 100644 --- a/spec/api/check_spec.rb +++ b/spec/api/check_spec.rb @@ -26,12 +26,14 @@ describe "check" do it "should check rails style checkboxes" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"}) @@ -86,8 +88,10 @@ end describe "uncheck" do it "should fail if no checkbox found" do with_html <<-HTML +
+ HTML lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError) @@ -95,9 +99,11 @@ describe "uncheck" do it "should fail if input is not a checkbox" do with_html <<-HTML +
+ HTML lambda { uncheck "remember_me" }.should raise_error(Webrat::NotFoundError) @@ -105,22 +111,26 @@ describe "uncheck" do it "should fail if the checkbox is disabled" do with_html <<-HTML +
+ HTML lambda { uncheck "remember_me" }.should raise_error(Webrat::DisabledFieldError) end it "should uncheck rails style checkboxes" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"}) check "TOS" @@ -130,10 +140,12 @@ describe "uncheck" do it "should result in value not being posted" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", {}) uncheck "remember_me" diff --git a/spec/api/choose_spec.rb b/spec/api/choose_spec.rb index 9137388..31df4ae 100644 --- a/spec/api/choose_spec.rb +++ b/spec/api/choose_spec.rb @@ -3,8 +3,10 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "choose" do it "should fail if no radio buttons found" do with_html <<-HTML +
+ HTML lambda { choose "first option" }.should raise_error(Webrat::NotFoundError) @@ -12,9 +14,11 @@ describe "choose" do it "should fail if input is not a radio button" do with_html <<-HTML +
+ HTML lambda { choose "first_option" }.should raise_error(Webrat::NotFoundError) @@ -22,6 +26,7 @@ describe "choose" do it "should check rails style radio buttons" do with_html <<-HTML +
@@ -29,6 +34,7 @@ describe "choose" do
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "M"}) choose "Male" @@ -37,6 +43,7 @@ describe "choose" do it "should only submit last chosen value" do with_html <<-HTML +
@@ -44,6 +51,7 @@ describe "choose" do
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "M"}) choose "Female" @@ -53,10 +61,12 @@ describe "choose" do it "should fail if the radio button is disabled" do with_html <<-HTML +
+ HTML lambda { choose "first_option" }.should raise_error(Webrat::DisabledFieldError) @@ -64,10 +74,12 @@ describe "choose" do it "should result in the value on being posted if not specified" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "first_option" => "on") choose "first_option" @@ -76,10 +88,12 @@ describe "choose" do it "should result in the value on being posted if not specified and checked by default" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "first_option" => "on") click_button @@ -87,6 +101,7 @@ describe "choose" do it "should result in the value of the selected radio button being posted when a subsequent one is checked by default" do with_html <<-HTML +
@@ -94,6 +109,7 @@ describe "choose" do
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"gender" => "M"}) choose "Male" diff --git a/spec/api/click_area_spec.rb b/spec/api/click_area_spec.rb index 791817f..65bfbbe 100644 --- a/spec/api/click_area_spec.rb +++ b/spec/api/click_area_spec.rb @@ -3,9 +3,11 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "click_area" do it "should use get by default" do with_html <<-HTML + Berlin + HTML webrat_session.should_receive(:get).with("/page", {}) click_area "Berlin" @@ -13,9 +15,11 @@ describe "click_area" do it "should assert valid response" do with_html <<-HTML + Berlin + HTML webrat_session.response_code = 501 lambda { click_area "Berlin" }.should raise_error(Webrat::PageLoadError) @@ -24,9 +28,11 @@ describe "click_area" do [200, 300, 400, 499].each do |status| it "should consider the #{status} status code as success" do with_html <<-HTML + Berlin + HTML webrat_session.response_code = status lambda { click_area "Berlin" }.should_not raise_error @@ -35,9 +41,11 @@ describe "click_area" do it "should fail if the area doesn't exist" do with_html <<-HTML + Berlin + HTML lambda { @@ -47,9 +55,11 @@ describe "click_area" do it "should not be case sensitive" do with_html <<-HTML + Berlin + HTML webrat_session.should_receive(:get).with("/page", {}) click_area "berlin" @@ -59,9 +69,11 @@ describe "click_area" do it "should follow relative links" do webrat_session.stub!(:current_url => "/page") with_html <<-HTML + Berlin + HTML webrat_session.should_receive(:get).with("/page/sub", {}) click_area "Berlin" @@ -69,9 +81,11 @@ describe "click_area" do it "should follow fully qualified local links" do with_html <<-HTML + Berlin + HTML webrat_session.should_receive(:get).with("http://www.example.com/page", {}) click_area "Berlin" @@ -79,9 +93,11 @@ describe "click_area" do it "should follow query parameters" do with_html <<-HTML + Berlin + HTML webrat_session.should_receive(:get).with("/page?foo=bar", {}) click_area "Berlin" diff --git a/spec/api/click_button_spec.rb b/spec/api/click_button_spec.rb index 5e55ad1..ab6913d 100644 --- a/spec/api/click_button_spec.rb +++ b/spec/api/click_button_spec.rb @@ -3,7 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "click_button" do it "should fail if no buttons" do with_html <<-HTML +
+ HTML lambda { click_button }.should raise_error(Webrat::NotFoundError) @@ -11,9 +13,11 @@ describe "click_button" do it "should fail if input is not a submit button" do with_html <<-HTML +
+ HTML lambda { click_button }.should raise_error(Webrat::NotFoundError) @@ -22,9 +26,11 @@ describe "click_button" do it "should fail if button is disabled" do with_html <<-HTML +
+ HTML lambda { click_button }.should raise_error(Webrat::DisabledFieldError) @@ -32,9 +38,11 @@ describe "click_button" do it "should default to get method" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get) click_button @@ -42,9 +50,11 @@ describe "click_button" do it "should assert valid response" do with_html <<-HTML +
+ HTML webrat_session.response_code = 501 lambda { click_button }.should raise_error(Webrat::PageLoadError) @@ -53,9 +63,11 @@ describe "click_button" do [200, 300, 400, 499].each do |status| it "should consider the #{status} status code as success" do with_html <<-HTML +
+ HTML webrat_session.response_code = status lambda { click_button }.should_not raise_error @@ -64,12 +76,14 @@ describe "click_button" do it "should submit the first form by default" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/form1", {}) click_button @@ -77,10 +91,12 @@ describe "click_button" do it "should not explode on file fields" do with_html <<-HTML +
+ HTML click_button end @@ -102,9 +118,11 @@ describe "click_button" do it "should use action from form" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", {}) click_button @@ -112,9 +130,11 @@ describe "click_button" do it "should use method from form" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post) click_button @@ -122,10 +142,12 @@ describe "click_button" do it "should send button as param if it has a name" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "login" => "Login") click_button("Login") @@ -133,10 +155,12 @@ describe "click_button" do it "should not send button as param if it has no name" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", {}) click_button("Login") @@ -144,10 +168,12 @@ describe "click_button" do it "should send default password field values" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"password" => "mypass"}) click_button @@ -155,10 +181,12 @@ describe "click_button" do it "should send default hidden field values" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"}) click_button @@ -166,10 +194,12 @@ describe "click_button" do it "should send default text field values" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"}) click_button @@ -177,6 +207,7 @@ describe "click_button" do it "should not send disabled field values" do with_html <<-HTML +
@@ -185,6 +216,7 @@ describe "click_button" do
+ HTML webrat_session.should_receive(:get).with("/login", {}) click_button @@ -192,10 +224,12 @@ describe "click_button" do it "should send default checked fields" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"}) click_button @@ -203,6 +237,7 @@ describe "click_button" do it "should send default radio options" do with_html <<-HTML +
@@ -210,6 +245,7 @@ describe "click_button" do
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "F"}) click_button @@ -217,11 +253,13 @@ describe "click_button" do it "should send correct data for rails style unchecked fields" do with_html <<-HTML +
TOS
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"}) click_button @@ -229,11 +267,13 @@ describe "click_button" do it "should send correct data for rails style checked fields" do with_html <<-HTML +
TOS
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"}) click_button @@ -241,6 +281,7 @@ describe "click_button" do it "should send default collection fields" do with_html <<-HTML +
@@ -254,6 +295,7 @@ describe "click_button" do
+ HTML webrat_session.should_receive(:post).with("/login", "options" => ["burger", "fries", "soda", "soda", "dessert"], @@ -263,10 +305,12 @@ describe "click_button" do it "should not send default unchecked fields" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", {}) click_button @@ -274,10 +318,12 @@ describe "click_button" do it "should send default textarea values" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Post body here!"}) click_button @@ -285,6 +331,7 @@ describe "click_button" do it "should send default selected option value from select" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "month" => "2") click_button @@ -299,6 +347,7 @@ describe "click_button" do it "should send default selected option inner html from select when no value attribute" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "month" => "February") click_button @@ -313,6 +363,7 @@ describe "click_button" do it "should send first select option value when no option selected" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "month" => "1") click_button @@ -327,11 +379,13 @@ describe "click_button" do it "should handle nested properties" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}}) click_button @@ -339,10 +393,12 @@ describe "click_button" do it "should send default empty text field values" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""}) click_button @@ -350,10 +406,12 @@ describe "click_button" do it "should recognize button tags" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""}) click_button "Login" diff --git a/spec/api/click_link_spec.rb b/spec/api/click_link_spec.rb index a7aace4..f602d94 100644 --- a/spec/api/click_link_spec.rb +++ b/spec/api/click_link_spec.rb @@ -3,7 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "click_link" do it "should click links with ampertands" do with_html <<-HTML + Save & go back + HTML webrat_session.should_receive(:get).with("/page", {}) click_link "Save & go back" @@ -11,7 +13,9 @@ describe "click_link" do it "should use get by default" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:get).with("/page", {}) click_link "Link text" @@ -19,7 +23,9 @@ describe "click_link" do it "should click get links" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:get).with("/page", {}) click_link "Link text", :method => :get @@ -27,7 +33,9 @@ describe "click_link" do it "should click link on substring" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:get).with("/page", {}) click_link "ink tex", :method => :get @@ -35,7 +43,9 @@ describe "click_link" do it "should click delete links" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:delete).with("/page", {}) click_link "Link text", :method => :delete @@ -44,7 +54,9 @@ describe "click_link" do it "should click post links" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:post).with("/page", {}) click_link "Link text", :method => :post @@ -52,7 +64,9 @@ describe "click_link" do it "should click put links" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:put).with("/page", {}) click_link "Link text", :method => :put @@ -60,7 +74,9 @@ describe "click_link" do it "should click links by regexp" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:get).with("/page", {}) click_link /link [a-z]/i @@ -68,7 +84,9 @@ describe "click_link" do it "should click links by id" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:get).with("/page", {}) click_link "link_text_link" @@ -76,7 +94,9 @@ describe "click_link" do it "should click links by id regexp" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:get).with("/page", {}) click_link /_text_/ @@ -84,6 +104,7 @@ describe "click_link" do it "should click rails javascript links with authenticity tokens" do with_html <<-HTML + Posts + HTML webrat_session.should_receive(:post).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62") click_link "Posts" @@ -103,6 +125,7 @@ describe "click_link" do it "should click rails javascript delete links" do with_html <<-HTML + Delete + HTML webrat_session.should_receive(:delete).with("/posts/1", {}) click_link "Delete" @@ -122,6 +146,7 @@ describe "click_link" do it "should click rails javascript post links" do with_html <<-HTML + Posts + HTML webrat_session.should_receive(:post).with("/posts", {}) click_link "Posts" @@ -136,6 +162,7 @@ describe "click_link" do it "should click rails javascript post links without javascript" do with_html <<-HTML + Posts + HTML webrat_session.should_receive(:get).with("/posts", {}) click_link "Posts", :javascript => false @@ -150,6 +178,7 @@ describe "click_link" do it "should click rails javascript put links" do with_html <<-HTML + Put + HTML webrat_session.should_receive(:put).with("/posts", {}) click_link "Put" @@ -169,6 +199,7 @@ describe "click_link" do it "should fail if the javascript link doesn't have a value for the _method input" do with_html <<-HTML + Link + HTML lambda { @@ -189,7 +221,9 @@ describe "click_link" do it "should assert valid response" do with_html <<-HTML + Link text + HTML webrat_session.response_code = 501 lambda { click_link "Link text" }.should raise_error(Webrat::PageLoadError) @@ -198,7 +232,9 @@ describe "click_link" do [200, 300, 400, 499].each do |status| it "should consider the #{status} status code as success" do with_html <<-HTML + Link text + HTML webrat_session.response_code = status lambda { click_link "Link text" }.should_not raise_error @@ -207,7 +243,9 @@ describe "click_link" do it "should fail is the link doesn't exist" do with_html <<-HTML + Link text + HTML lambda { @@ -217,7 +255,9 @@ describe "click_link" do it "should not be case sensitive" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:get).with("/page", {}) click_link "LINK TEXT" @@ -225,7 +265,9 @@ describe "click_link" do it "should match link substrings" do with_html <<-HTML + This is some cool link text, isn't it? + HTML webrat_session.should_receive(:get).with("/page", {}) click_link "Link text" @@ -233,7 +275,9 @@ describe "click_link" do it "should work with elements in the link" do with_html <<-HTML + Link text + HTML webrat_session.should_receive(:get).with("/page", {}) click_link "Link text" @@ -241,8 +285,10 @@ describe "click_link" do it "should match the first matching link" do with_html <<-HTML + Link text Link text + HTML webrat_session.should_receive(:get).with("/page1", {}) click_link "Link text" @@ -250,10 +296,10 @@ describe "click_link" do it "should choose the shortest link text match" do with_html <<-HTML - - Linkerama - Link - + + Linkerama + Link + HTML webrat_session.should_receive(:get).with("/page2", {}) @@ -262,9 +308,9 @@ describe "click_link" do it "should treat non-breaking spaces as spaces" do with_html <<-HTML - - This is a link - + + This is a link + HTML webrat_session.should_receive(:get).with("/page1", {}) @@ -274,8 +320,10 @@ describe "click_link" do it "should not match on non-text contents" do pending "needs fix" do with_html <<-HTML - My house - Location + + My house + Location + HTML webrat_session.should_receive(:get).with("/page2", {}) @@ -286,10 +334,10 @@ describe "click_link" do it "should click link within a selector" do with_html <<-HTML - Link -
- Link -
+ Link +
+ Link +
HTML @@ -299,7 +347,9 @@ describe "click_link" do it "should not make request when link is local anchor" do with_html <<-HTML - Jump to Section 1 + + 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 @@ -309,7 +359,9 @@ describe "click_link" do it "should follow relative links" do webrat_session.stub!(:current_url => "/page") with_html <<-HTML + Jump to sub page + HTML webrat_session.should_receive(:get).with("/page/sub", {}) click_link "Jump to sub page" @@ -318,7 +370,9 @@ describe "click_link" do it "should follow fully qualified local links" do webrat_session.stub!(:current_url => "/page") with_html <<-HTML + Jump to sub page + HTML webrat_session.should_receive(:get).with("http://subdomain.example.com/page/sub", {}) click_link "Jump to sub page" @@ -326,7 +380,9 @@ describe "click_link" do it "should follow fully qualified local links to example.com" do with_html <<-HTML + Jump to sub page + HTML webrat_session.should_receive(:get).with("http://www.example.com/page/sub", {}) click_link "Jump to sub page" @@ -335,7 +391,9 @@ describe "click_link" do it "should follow query parameters" do webrat_session.stub!(:current_url => "/page") with_html <<-HTML + Jump to foo bar + HTML webrat_session.should_receive(:get).with("/page?foo=bar", {}) click_link "Jump to foo bar" diff --git a/spec/api/fill_in_spec.rb b/spec/api/fill_in_spec.rb index 2d6a1c4..d3bb4a6 100644 --- a/spec/api/fill_in_spec.rb +++ b/spec/api/fill_in_spec.rb @@ -3,11 +3,13 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "fill_in" do it "should work with textareas" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"text" => "filling text area"}) fill_in "User Text", :with => "filling text area" @@ -16,10 +18,12 @@ describe "fill_in" do it "should work with password fields" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"text" => "pass"}) fill_in "user_text", :with => "pass" @@ -28,8 +32,10 @@ describe "fill_in" do it "should fail if input not found" do with_html <<-HTML +
+ HTML lambda { fill_in "Email", :with => "foo@example.com" }.should raise_error(Webrat::NotFoundError) @@ -37,11 +43,13 @@ describe "fill_in" do it "should fail if input is disabled" do with_html <<-HTML +
+ HTML lambda { fill_in "Email", :with => "foo@example.com" }.should raise_error(Webrat::DisabledFieldError) @@ -49,11 +57,13 @@ describe "fill_in" do it "should allow overriding default form values" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) fill_in "user[email]", :with => "foo@example.com" @@ -62,6 +72,7 @@ describe "fill_in" do it "should choose the shortest label match" do with_html <<-HTML +
@@ -69,6 +80,7 @@ describe "fill_in" do
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "", "mail2" => "value"}) @@ -78,6 +90,7 @@ describe "fill_in" do it "should choose the first label match if closest is a tie" do with_html <<-HTML +
@@ -85,6 +98,7 @@ describe "fill_in" do
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"mail1" => "value", "mail2" => ""}) @@ -94,10 +108,12 @@ describe "fill_in" do it "should anchor label matches to start of label" do with_html <<-HTML +
+ HTML lambda { fill_in "mail", :with => "value" }.should raise_error(Webrat::NotFoundError) @@ -105,10 +121,12 @@ describe "fill_in" do it "should anchor label matches to word boundaries" do with_html <<-HTML +
+ HTML lambda { fill_in "Email", :with => "value" }.should raise_error(Webrat::NotFoundError) @@ -116,6 +134,7 @@ describe "fill_in" do it "should work with inputs nested in labels" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) fill_in "Email", :with => "foo@example.com" @@ -131,10 +151,12 @@ describe "fill_in" do it "should work with full input names" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) fill_in "user[email]", :with => "foo@example.com" @@ -143,10 +165,12 @@ describe "fill_in" do it "should work if the input type is not set" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) fill_in "user[email]", :with => "foo@example.com" @@ -155,11 +179,13 @@ describe "fill_in" do it "should work with symbols" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "user" => {"email" => "foo@example.com"}) fill_in :email, :with => "foo@example.com" @@ -168,11 +194,13 @@ describe "fill_in" do it "should escape field values" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/users", "user" => {"phone" => "+1 22 33"}) fill_in 'Phone', :with => "+1 22 33" diff --git a/spec/api/locators/field_labeled_spec.rb b/spec/api/locators/field_labeled_spec.rb index 414af51..33066b3 100644 --- a/spec/api/locators/field_labeled_spec.rb +++ b/spec/api/locators/field_labeled_spec.rb @@ -43,10 +43,12 @@ describe "field_labeled" do describe "finding a text field" do using_this_html <<-HTML +
+ HTML should_return_a Webrat::TextField, :for => "The Label" @@ -56,10 +58,12 @@ describe "field_labeled" do describe "finding a hidden field" do using_this_html <<-HTML +
+ HTML should_return_a Webrat::HiddenField, :for => "The Label" @@ -69,10 +73,12 @@ describe "field_labeled" do describe "finding a checkbox" do using_this_html <<-HTML +
+ HTML should_return_a Webrat::CheckboxField, :for => "The Label" @@ -82,10 +88,12 @@ describe "field_labeled" do describe "finding a radio button" do using_this_html <<-HTML +
+ HTML should_return_a Webrat::RadioField, :for => "The Label" @@ -96,10 +104,12 @@ describe "field_labeled" do describe "finding a text area" do using_this_html <<-HTML +
+ HTML should_return_a Webrat::TextareaField, :for => "The Label" @@ -109,6 +119,7 @@ describe "field_labeled" do 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" diff --git a/spec/api/matchers_spec.rb b/spec/api/matchers_spec.rb index 1cfbe5c..c879ff6 100644 --- a/spec/api/matchers_spec.rb +++ b/spec/api/matchers_spec.rb @@ -5,15 +5,17 @@ describe Webrat::Matchers do include Webrat::HaveTagMatcher before(:each) do - @body = <<-EOF -
-
hello, world!
-
    -
  • First
  • -
  • Second
  • -
-
- EOF + @body = <<-HTML + +
+
hello, world!
+
    +
  • First
  • +
  • Second
  • +
+
+ + HTML end describe "#have_xpath" do diff --git a/spec/api/select_date_spec.rb b/spec/api/select_date_spec.rb index 11089cb..3fd14fa 100644 --- a/spec/api/select_date_spec.rb +++ b/spec/api/select_date_spec.rb @@ -3,6 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "select_date" do it "should send the values for each individual date component" do with_html <<-HTML +

+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) @@ -25,6 +27,7 @@ describe "select_date" do it "should accept a date object" do with_html <<-HTML +

+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) @@ -47,6 +51,7 @@ describe "select_date" do it "should work when no label is specified" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) @@ -68,10 +74,12 @@ describe "select_date" do it "should fail if the specified label is not found" do with_html <<-HTML +
+ HTML lambda { select_date "December 25, 2003", :from => "date" }.should raise_error(Webrat::NotFoundError) diff --git a/spec/api/select_datetime_spec.rb b/spec/api/select_datetime_spec.rb index ade53f8..1173835 100644 --- a/spec/api/select_datetime_spec.rb +++ b/spec/api/select_datetime_spec.rb @@ -3,6 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "select_datetime" do it "should send the values for each individual date and time components" do with_html <<-HTML +

+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) @@ -31,6 +33,7 @@ describe "select_datetime" do it "should accept a time object" do with_html <<-HTML +

+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) @@ -59,6 +63,7 @@ describe "select_datetime" do it "should work when no label is specified" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) @@ -86,10 +92,12 @@ describe "select_datetime" do it "should fail if the specified label is not found" do with_html <<-HTML +
+ HTML lambda { select_datetime "December 25, 2003 9:30", :from => "Time" }.should raise_error(Webrat::NotFoundError) diff --git a/spec/api/select_spec.rb b/spec/api/select_spec.rb index 2437128..7af4b3a 100644 --- a/spec/api/select_spec.rb +++ b/spec/api/select_spec.rb @@ -3,9 +3,11 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "select" do it "should fail with a helpful message when option not found" do with_html <<-HTML +
+ HTML lambda { select "February", :from => "month" }.should raise_error(Webrat::NotFoundError, @@ -14,10 +16,12 @@ describe "select" do it "should fail if option not found in list specified by element name" do with_html <<-HTML +
+ HTML lambda { select "February", :from => "year" }.should raise_error(Webrat::NotFoundError) @@ -25,9 +29,11 @@ describe "select" do it "should fail if specified list not found" do with_html <<-HTML +
+ HTML lambda { select "February", :from => "year" }.should raise_error(Webrat::NotFoundError) @@ -36,10 +42,12 @@ describe "select" do it "should fail if the select is disabled" do with_html <<-HTML +
+ HTML lambda { select "January", :from => "month" }.should raise_error(Webrat::DisabledFieldError) @@ -47,10 +55,12 @@ describe "select" do it "should send value from option" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "month" => "1") select "January", :from => "month" @@ -59,10 +69,12 @@ describe "select" do it "should send values with HTML encoded ampersands" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "encoded" => "A & B") select "Encoded", :from => "encoded" @@ -71,10 +83,12 @@ describe "select" do it "should work with empty select lists" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", 'month' => '') click_button @@ -82,10 +96,12 @@ describe "select" do it "should work without specifying the field name or label" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "month" => "1") select "January" @@ -94,11 +110,13 @@ describe "select" do it "should send value from option in list specified by name" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") select "January", :from => "end_month" @@ -107,6 +125,7 @@ describe "select" do it "should send value from option in list specified by label" do with_html <<-HTML +
@@ -114,6 +133,7 @@ describe "select" do
+ HTML webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") select "January", :from => "End Month" @@ -122,10 +142,12 @@ describe "select" do it "should use option text if no value" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "month" => "January") select "January", :from => "month" @@ -134,10 +156,12 @@ describe "select" do it "should find option by regexp" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/login", "month" => "January") select /jan/i @@ -146,10 +170,12 @@ describe "select" do it "should fail if no option matching the regexp exists" do with_html <<-HTML +
+ HTML lambda { @@ -159,6 +185,7 @@ describe "select" do it "should find option by regexp in list specified by label" do with_html <<-HTML +
@@ -166,6 +193,7 @@ describe "select" do
+ HTML webrat_session.should_receive(:post).with("/login", "start_month" => "s1", "end_month" => "e1") select /jan/i, :from => "End Month" diff --git a/spec/api/select_time_spec.rb b/spec/api/select_time_spec.rb index 7acea40..d0001ae 100644 --- a/spec/api/select_time_spec.rb +++ b/spec/api/select_time_spec.rb @@ -3,6 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "select_time" do it "should send the values for each individual time component" do with_html <<-HTML +

+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) @@ -22,6 +24,7 @@ describe "select_time" do it "should accept a time object" do with_html <<-HTML +

+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) @@ -41,6 +45,7 @@ describe "select_time" do it "should work when no label is specified" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/appointments", "appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) @@ -59,10 +65,12 @@ describe "select_time" do it "should fail if the specified label is not found" do with_html <<-HTML +
+ HTML lambda { select_time "9:30", :from => "Time" }.should raise_error(Webrat::NotFoundError) diff --git a/spec/api/visit_spec.rb b/spec/api/visit_spec.rb index a147756..a0e823d 100644 --- a/spec/api/visit_spec.rb +++ b/spec/api/visit_spec.rb @@ -3,7 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe "visit" do before do with_html <<-HTML + Hello world + HTML end @@ -33,7 +35,9 @@ describe "visit with referer" do before do webrat_session.instance_variable_set(:@current_url, "/old_url") with_html <<-HTML + Hello world + HTML end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 23900b3..1ddf33a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,8 +16,15 @@ Spec::Runner.configure do |config| include Webrat::Methods def with_html(html) + raise "This doesn't look like HTML. Wrap it in a tag" unless html =~ /^\s*<[^Hh>]*html/i webrat_session.response_body = html end + + def with_xml(xml) + raise "This looks like HTML" if xml =~ /^\s*<[^Hh>]*html/i + webrat_session.response_body = xml + end + end module Webrat diff --git a/spec/webrat/rails/attaches_file_spec.rb b/spec/webrat/rails/attaches_file_spec.rb index 610f6d4..e4b25bd 100644 --- a/spec/webrat/rails/attaches_file_spec.rb +++ b/spec/webrat/rails/attaches_file_spec.rb @@ -9,18 +9,22 @@ describe "attach_file" do it "should fail if no file field found" do with_html <<-HTML +
+ HTML lambda { attach_file("Doc", "/some/path") }.should raise_error(Webrat::NotFoundError) end it "should submit empty strings for blank file fields" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => "" } }) click_button @@ -28,11 +32,13 @@ describe "attach_file" do it "should submit the attached file" do with_html <<-HTML +
+ HTML webrat_session.should_receive(:post).with("/widgets", { "widget" => { "file" => @uploaded_file } }) attach_file "Document", @filename @@ -41,6 +47,7 @@ describe "attach_file" do it "should support collections" do with_html <<-HTML +
@@ -48,6 +55,7 @@ describe "attach_file" do
+ HTML webrat_session.should_receive(:post).with("/widgets", { "widget" => { "files" => [@uploaded_file, @uploaded_file] } }) attach_file "Document", @filename @@ -57,11 +65,13 @@ describe "attach_file" do it "should allow the content type to be specified" do with_html <<-HTML +
+ HTML ActionController::TestUploadedFile.should_receive(:new).with(@filename, "image/png").any_number_of_times attach_file "Picture", @filename, "image/png" From 18e65bfa4457b3e3a25f39f846f14cf8255cf454 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 28 Nov 2008 00:55:45 -0500 Subject: [PATCH 019/103] Cleaning up some more specs --- spec/api/click_button_spec.rb | 6 ++--- spec/api/locators/field_by_xpath_spec.rb | 4 ++- spec/api/matchers_spec.rb | 2 -- spec/webrat/core/field_spec.rb | 34 +++++++++++++++++++++--- spec/webrat/core/session_spec.rb | 2 +- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/spec/api/click_button_spec.rb b/spec/api/click_button_spec.rb index ab6913d..70f5b15 100644 --- a/spec/api/click_button_spec.rb +++ b/spec/api/click_button_spec.rb @@ -209,10 +209,10 @@ describe "click_button" do with_html <<-HTML
- - + + - +
diff --git a/spec/api/locators/field_by_xpath_spec.rb b/spec/api/locators/field_by_xpath_spec.rb index e92d081..c63b560 100644 --- a/spec/api/locators/field_by_xpath_spec.rb +++ b/spec/api/locators/field_by_xpath_spec.rb @@ -12,6 +12,8 @@ describe "field_by_xpath" do HTML - field_by_xpath(".//input").id.should == "element_42" + field = field_by_xpath(".//input") + field.should_not be_nil + field.id.should == "element_42" end end diff --git a/spec/api/matchers_spec.rb b/spec/api/matchers_spec.rb index c879ff6..af885b3 100644 --- a/spec/api/matchers_spec.rb +++ b/spec/api/matchers_spec.rb @@ -6,7 +6,6 @@ describe Webrat::Matchers do before(:each) do @body = <<-HTML -
hello, world!
    @@ -14,7 +13,6 @@ describe Webrat::Matchers do
  • Second
- HTML end diff --git a/spec/webrat/core/field_spec.rb b/spec/webrat/core/field_spec.rb index 7179968..fa741f9 100644 --- a/spec/webrat/core/field_spec.rb +++ b/spec/webrat/core/field_spec.rb @@ -3,24 +3,50 @@ 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::XML.document("").search('input')).first) + html = <<-HTML + + + + HTML + + element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first + checkbox = CheckboxField.new(nil, element) checkbox.should be_checked end it "should say it is not checked if it is not" do - checkbox = CheckboxField.new(nil, (Webrat::XML.document("").search('input')).first) + html = <<-HTML + + + + HTML + + element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first + checkbox = CheckboxField.new(nil, element) 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) + html = <<-HTML + + + + HTML + + element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first + radio_button = RadioField.new(nil, element) 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) + html = <<-HTML + + HTML + + element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first + radio_button = RadioField.new(nil, element) radio_button.should_not be_checked end end diff --git a/spec/webrat/core/session_spec.rb b/spec/webrat/core/session_spec.rb index 6fdcc48..c0a5974 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 respond_to(:search) + session.should respond_to(:current_dom) end it "should open the page in the browser in MacOSX" do From 87211f260df42ed97c22364db6801b80e0c6b4c8 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 28 Nov 2008 02:02:17 -0500 Subject: [PATCH 020/103] Use REXML and Hpricot together when Nokogiri is not available This gets us the ability to use XPath consistently, as well as Hpricot's forgiving parser, when using JRuby --- lib/webrat/core/label.rb | 2 +- lib/webrat/core/link.rb | 2 +- lib/webrat/core/locators.rb | 2 +- lib/webrat/core/matchers/have_content.rb | 7 ++- lib/webrat/core/scope.rb | 5 +- lib/webrat/core/xml.rb | 66 ++++++++++++++++++------ spec/api/locators/field_labeled_spec.rb | 34 ++++++------ spec/api/within_spec.rb | 16 +++--- spec/spec_helper.rb | 4 ++ 9 files changed, 91 insertions(+), 47 deletions(-) diff --git a/lib/webrat/core/label.rb b/lib/webrat/core/label.rb index e2e08c0..7131bd0 100644 --- a/lib/webrat/core/label.rb +++ b/lib/webrat/core/label.rb @@ -11,7 +11,7 @@ module Webrat end def text - str = Webrat::XML.inner_text(@element) + str = Webrat::XML.all_inner_text(@element) str.gsub!("\n","") str.strip! str.squeeze!(" ") diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index 272f7d9..81653bc 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -44,7 +44,7 @@ module Webrat end def text - Webrat::XML.inner_text(@element) + Webrat::XML.all_inner_text(@element) end protected diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 3e4b96a..ea8361c 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -4,7 +4,7 @@ module Webrat module Locators def field_by_xpath(xpath) - element = dom.at(xpath) + element = Webrat::XML.xpath_search(dom, xpath).first forms.detect_mapped do |form| form.field_by_element(element) diff --git a/lib/webrat/core/matchers/have_content.rb b/lib/webrat/core/matchers/have_content.rb index 2c4f43e..99a4f69 100644 --- a/lib/webrat/core/matchers/have_content.rb +++ b/lib/webrat/core/matchers/have_content.rb @@ -7,7 +7,12 @@ module Webrat end def matches?(stringlike) - @document = Webrat::XML.document(stringlike) + if Webrat.configuration.parse_with_nokogiri? + @document = Webrat.nokogiri_document(stringlike) + else + @document = Webrat::XML.hpricot_document(stringlike) + end + @element = Webrat::XML.inner_text(@document) case @content diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 0758bcf..3a25d3d 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -289,7 +289,10 @@ module Webrat end def scoped_dom #:nodoc: - Webrat::XML.document(Webrat::XML.css_search(@scope.dom, @selector).first.to_html) + # if @selector == "#form2" + # require "rubygems"; require "ruby-debug"; Debugger.start; debugger + # end + Webrat::XML.document("" + Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first) + "") end def locate_field(field_locator, *field_types) #:nodoc: diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index 8a0de4e..4572edc 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -5,8 +5,8 @@ module Webrat #:nodoc: if Webrat.configuration.parse_with_nokogiri? Webrat.nokogiri_document(stringlike) else - Webrat::XML.hpricot_document(stringlike) - # Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html) + # Webrat::XML.hpricot_document(stringlike) + Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html) end end @@ -26,39 +26,71 @@ module Webrat #:nodoc: end end + def self.to_html(element) + if Webrat.configuration.parse_with_nokogiri? + element.to_html + else + element.to_s + end + end + def self.inner_html(element) - element.inner_html + if Webrat.configuration.parse_with_nokogiri? + element.inner_html + else + element.text + end + end + + def self.all_inner_text(element) + if Webrat.configuration.parse_with_nokogiri? + element.inner_text + else + Hpricot(element.to_s).children.first.inner_text + end end def self.inner_text(element) - element.inner_text + if Webrat.configuration.parse_with_nokogiri? + element.inner_text + else + if defined?(Hpricot::Doc) && element.is_a?(Hpricot::Doc) + element.inner_text + else + element.text + end + end end def self.attribute(element, attribute_name) - # case element - # when Nokogiri::XML::Element, Hash + return element[attribute_name] if element.is_a?(Hash) + + if Webrat.configuration.parse_with_nokogiri? element[attribute_name] - # else - # element.attributes[attribute_name] - # end + else + element.attributes[attribute_name] + end end def self.xpath_search(element, *searches) searches.flatten.map do |search| - # REXML::XPath.match(element, search) - element.xpath(search) + if Webrat.configuration.parse_with_nokogiri? + element.xpath(search) + else + REXML::XPath.match(element, search) + end end.flatten.compact end def self.css_search(element, *searches) #:nodoc: - if Webrat.configuration.parse_with_nokogiri? + # if Webrat.configuration.parse_with_nokogiri? xpath_search(element, css_to_xpath(*searches)) # element.css(*searches) - else - searches.map do |search| - element.search(search) - end.flatten.compact - end + # else + # searches.map do |search| + # element.search(search) + # end.flatten.compact + # end end def self.css_to_xpath(*selectors) diff --git a/spec/api/locators/field_labeled_spec.rb b/spec/api/locators/field_labeled_spec.rb index 33066b3..82cdba0 100644 --- a/spec/api/locators/field_labeled_spec.rb +++ b/spec/api/locators/field_labeled_spec.rb @@ -50,12 +50,12 @@ describe "field_labeled" do HTML - + should_return_a Webrat::TextField, :for => "The Label" 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 hidden field" do using_this_html <<-HTML @@ -65,12 +65,12 @@ describe "field_labeled" do HTML - + should_return_a Webrat::HiddenField, :for => "The Label" 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 checkbox" do using_this_html <<-HTML @@ -80,12 +80,12 @@ describe "field_labeled" do HTML - + should_return_a Webrat::CheckboxField, :for => "The Label" 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 radio button" do using_this_html <<-HTML @@ -95,13 +95,13 @@ describe "field_labeled" do HTML - + should_return_a Webrat::RadioField, :for => "The Label" 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 text area" do using_this_html <<-HTML @@ -111,7 +111,7 @@ describe "field_labeled" do HTML - + should_return_a Webrat::TextareaField, :for => "The Label" with_an_id_of "element_42", :for => "The Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" @@ -120,13 +120,13 @@ describe "field_labeled" do describe "finding a field with it's label containing newlines" do using_this_html <<-HTML -
- - -
+
+ + +
HTML diff --git a/spec/api/within_spec.rb b/spec/api/within_spec.rb index 0af74ed..7892970 100644 --- a/spec/api/within_spec.rb +++ b/spec/api/within_spec.rb @@ -40,14 +40,14 @@ describe "within" do it "should submit forms within a scope" do with_html <<-HTML -
-