Skip to content

Commit

Permalink
Use REXML and Hpricot together when Nokogiri is not available
Browse files Browse the repository at this point in the history
This gets us the ability to use XPath consistently, as well as Hpricot's
forgiving parser, when using JRuby
  • Loading branch information
brynary committed Nov 28, 2008
1 parent 18e65bf commit 87211f2
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/webrat/core/label.rb
Expand Up @@ -11,7 +11,7 @@ def matches_text?(label_text)
end

def text
str = Webrat::XML.inner_text(@element)
str = Webrat::XML.all_inner_text(@element)
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/link.rb
Expand Up @@ -44,7 +44,7 @@ def inner_html
end

def text
Webrat::XML.inner_text(@element)
Webrat::XML.all_inner_text(@element)
end

protected
Expand Down
2 changes: 1 addition & 1 deletion lib/webrat/core/locators.rb
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion lib/webrat/core/matchers/have_content.rb
Expand Up @@ -7,7 +7,12 @@ def initialize(content)
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
Expand Down
5 changes: 4 additions & 1 deletion lib/webrat/core/scope.rb
Expand Up @@ -289,7 +289,10 @@ def page_dom #:nodoc:
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("<html>" + Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first) + "</html>")
end

def locate_field(field_locator, *field_types) #:nodoc:
Expand Down
66 changes: 49 additions & 17 deletions lib/webrat/core/xml.rb
Expand Up @@ -5,8 +5,8 @@ def self.document(stringlike) #: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

Expand All @@ -26,39 +26,71 @@ def self.hpricot_document(stringlike)
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)
Expand Down
34 changes: 17 additions & 17 deletions spec/api/locators/field_labeled_spec.rb
Expand Up @@ -50,12 +50,12 @@ def match_id(id)
</form>
</html>
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
<html>
Expand All @@ -65,12 +65,12 @@ def match_id(id)
</form>
</html>
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
<html>
Expand All @@ -80,12 +80,12 @@ def match_id(id)
</form>
</html>
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
<html>
Expand All @@ -95,13 +95,13 @@ def match_id(id)
</form>
</html>
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
<html>
Expand All @@ -111,7 +111,7 @@ def match_id(id)
</form>
</html>
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"
Expand All @@ -120,13 +120,13 @@ def match_id(id)
describe "finding a field with it's label containing newlines" do
using_this_html <<-HTML
<html>
<form>
<label for="element_42">
A label with
<a>a link on it's own line</a>
</label>
<input type="text" id="element_42">
</form>
<form>
<label for="element_42">
A label with
<a>a link on it's own line</a>
</label>
<input type="text" id="element_42">
</form>
</html>
HTML

Expand Down
16 changes: 8 additions & 8 deletions spec/api/within_spec.rb
Expand Up @@ -40,14 +40,14 @@
it "should submit forms within a scope" do
with_html <<-HTML
<html>
<form id="form1" action="/form1">
<label>Email: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
<form id="form2" action="/form2">
<label>Email: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
<form id="form1" action="/form1">
<label>Email: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
<form id="form2" action="/form2">
<label>Email: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
</html>
HTML

Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -12,6 +12,10 @@

require "merb-core"

Webrat.configure do |config|
config.parse_with_nokogiri = false
end

Spec::Runner.configure do |config|
include Webrat::Methods

Expand Down

0 comments on commit 87211f2

Please sign in to comment.