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
This commit is contained in:
Bryan Helmkamp 2008-11-28 02:02:17 -05:00
parent 18e65bfa44
commit 87211f260d
9 changed files with 91 additions and 47 deletions

View File

@ -11,7 +11,7 @@ module Webrat
end end
def text def text
str = Webrat::XML.inner_text(@element) str = Webrat::XML.all_inner_text(@element)
str.gsub!("\n","") str.gsub!("\n","")
str.strip! str.strip!
str.squeeze!(" ") str.squeeze!(" ")

View File

@ -44,7 +44,7 @@ module Webrat
end end
def text def text
Webrat::XML.inner_text(@element) Webrat::XML.all_inner_text(@element)
end end
protected protected

View File

@ -4,7 +4,7 @@ module Webrat
module Locators module Locators
def field_by_xpath(xpath) def field_by_xpath(xpath)
element = dom.at(xpath) element = Webrat::XML.xpath_search(dom, xpath).first
forms.detect_mapped do |form| forms.detect_mapped do |form|
form.field_by_element(element) form.field_by_element(element)

View File

@ -7,7 +7,12 @@ module Webrat
end end
def matches?(stringlike) 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) @element = Webrat::XML.inner_text(@document)
case @content case @content

View File

@ -289,7 +289,10 @@ module Webrat
end end
def scoped_dom #:nodoc: 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("<html>" + Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first) + "</html>")
end end
def locate_field(field_locator, *field_types) #:nodoc: def locate_field(field_locator, *field_types) #:nodoc:

View File

@ -5,8 +5,8 @@ module Webrat #:nodoc:
if Webrat.configuration.parse_with_nokogiri? if Webrat.configuration.parse_with_nokogiri?
Webrat.nokogiri_document(stringlike) Webrat.nokogiri_document(stringlike)
else else
Webrat::XML.hpricot_document(stringlike) # Webrat::XML.hpricot_document(stringlike)
# Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html) Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html)
end end
end end
@ -26,39 +26,71 @@ module Webrat #:nodoc:
end end
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) 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 end
def self.inner_text(element) 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 end
def self.attribute(element, attribute_name) def self.attribute(element, attribute_name)
# case element return element[attribute_name] if element.is_a?(Hash)
# when Nokogiri::XML::Element, Hash
if Webrat.configuration.parse_with_nokogiri?
element[attribute_name] element[attribute_name]
# else else
# element.attributes[attribute_name] element.attributes[attribute_name]
# end end
end end
def self.xpath_search(element, *searches) def self.xpath_search(element, *searches)
searches.flatten.map do |search| searches.flatten.map do |search|
# REXML::XPath.match(element, search) if Webrat.configuration.parse_with_nokogiri?
element.xpath(search) element.xpath(search)
else
REXML::XPath.match(element, search)
end
end.flatten.compact end.flatten.compact
end end
def self.css_search(element, *searches) #:nodoc: 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)) xpath_search(element, css_to_xpath(*searches))
# element.css(*searches) # element.css(*searches)
else # else
searches.map do |search| # searches.map do |search|
element.search(search) # element.search(search)
end.flatten.compact # end.flatten.compact
end # end
end end
def self.css_to_xpath(*selectors) def self.css_to_xpath(*selectors)

View File

@ -50,12 +50,12 @@ describe "field_labeled" do
</form> </form>
</html> </html>
HTML HTML
should_return_a Webrat::TextField, :for => "The Label" should_return_a Webrat::TextField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label" with_an_id_of "element_42", :for => "The Label"
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
end end
describe "finding a hidden field" do describe "finding a hidden field" do
using_this_html <<-HTML using_this_html <<-HTML
<html> <html>
@ -65,12 +65,12 @@ describe "field_labeled" do
</form> </form>
</html> </html>
HTML HTML
should_return_a Webrat::HiddenField, :for => "The Label" should_return_a Webrat::HiddenField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label" with_an_id_of "element_42", :for => "The Label"
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
end end
describe "finding a checkbox" do describe "finding a checkbox" do
using_this_html <<-HTML using_this_html <<-HTML
<html> <html>
@ -80,12 +80,12 @@ describe "field_labeled" do
</form> </form>
</html> </html>
HTML HTML
should_return_a Webrat::CheckboxField, :for => "The Label" should_return_a Webrat::CheckboxField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label" with_an_id_of "element_42", :for => "The Label"
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
end end
describe "finding a radio button" do describe "finding a radio button" do
using_this_html <<-HTML using_this_html <<-HTML
<html> <html>
@ -95,13 +95,13 @@ describe "field_labeled" do
</form> </form>
</html> </html>
HTML HTML
should_return_a Webrat::RadioField, :for => "The Label" should_return_a Webrat::RadioField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label" with_an_id_of "element_42", :for => "The Label"
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label"
end end
describe "finding a text area" do describe "finding a text area" do
using_this_html <<-HTML using_this_html <<-HTML
<html> <html>
@ -111,7 +111,7 @@ describe "field_labeled" do
</form> </form>
</html> </html>
HTML HTML
should_return_a Webrat::TextareaField, :for => "The Label" should_return_a Webrat::TextareaField, :for => "The Label"
with_an_id_of "element_42", :for => "The Label" with_an_id_of "element_42", :for => "The Label"
should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other 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 describe "finding a field with it's label containing newlines" do
using_this_html <<-HTML using_this_html <<-HTML
<html> <html>
<form> <form>
<label for="element_42"> <label for="element_42">
A label with A label with
<a>a link on it's own line</a> <a>a link on it's own line</a>
</label> </label>
<input type="text" id="element_42"> <input type="text" id="element_42">
</form> </form>
</html> </html>
HTML HTML

View File

@ -40,14 +40,14 @@ describe "within" do
it "should submit forms within a scope" do it "should submit forms within a scope" do
with_html <<-HTML with_html <<-HTML
<html> <html>
<form id="form1" action="/form1"> <form id="form1" action="/form1">
<label>Email: <input type="text" name="email" /> <label>Email: <input type="text" name="email" />
<input type="submit" value="Add" /> <input type="submit" value="Add" />
</form> </form>
<form id="form2" action="/form2"> <form id="form2" action="/form2">
<label>Email: <input type="text" name="email" /> <label>Email: <input type="text" name="email" />
<input type="submit" value="Add" /> <input type="submit" value="Add" />
</form> </form>
</html> </html>
HTML HTML

View File

@ -12,6 +12,10 @@ require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session")
require "merb-core" require "merb-core"
Webrat.configure do |config|
config.parse_with_nokogiri = false
end
Spec::Runner.configure do |config| Spec::Runner.configure do |config|
include Webrat::Methods include Webrat::Methods