butternut 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{butternut}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeremy Stephens"]
12
- s.date = %q{2009-12-14}
12
+ s.date = %q{2009-12-15}
13
13
  s.description = %q{Based on Cucumber's HTML formatter, Butternut uses Celerity to capture page sources after each step.}
14
14
  s.email = %q{viking415@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
33
33
  "spec/butternut/formatter_spec.rb",
34
34
  "spec/butternut/helpers_spec.rb",
35
35
  "spec/butternut_spec.rb",
36
+ "spec/fixtures/blargh.html",
36
37
  "spec/fixtures/css/bar.css",
37
38
  "spec/fixtures/facepalm.jpg",
38
39
  "spec/fixtures/foo.css",
@@ -10,11 +10,19 @@ module Butternut
10
10
  end
11
11
 
12
12
  def current_url
13
- browser.page.getWebResponse.getRequestUrl.toString
13
+ browser.page.web_response.request_url.to_string
14
14
  end
15
15
 
16
16
  def current_page_source
17
- browser.page ? browser.page.as_xml : nil
17
+ return nil unless browser.page
18
+
19
+ string_writer = java.io.StringWriter.new
20
+ print_writer = java.io.PrintWriter.new(string_writer)
21
+
22
+ root = browser.page.document_element
23
+ node_to_xml(root, print_writer)
24
+ print_writer.close
25
+ string_writer.to_string
18
26
  end
19
27
 
20
28
  # Fill in a text field with a value
@@ -47,5 +55,49 @@ module Butternut
47
55
  elt = browser.send(type, :label, label_or_name)
48
56
  elt.exist? ? elt : browser.send(type, :name, label_or_name)
49
57
  end
58
+
59
+ private
60
+ # NOTE: I have to do this because HtmlUnit's asXml likes to put spaces
61
+ # and newlines in the middle of whitespace-sensitive tags (like
62
+ # <a> and <textarea>). Fail.
63
+ def node_to_xml(node, print_writer)
64
+ closing_tag = false
65
+
66
+ case node
67
+ when HtmlUnit::Html::DomText
68
+ # just print out the text
69
+ print_writer.write(HtmlUnit::Util::StringUtils.escape_xml_chars(node.data))
70
+ when HtmlUnit::Html::DomCDataSection, HtmlUnit::Html::DomProcessingInstruction
71
+ # use default printXml here
72
+ node.print_xml("", print_writer)
73
+ when HtmlUnit::Html::DomComment
74
+ print_writer.write("<!-- #{node.data} -->")
75
+ when HtmlUnit::Html::HtmlTextArea
76
+ node_print_opening_tag(node, print_writer)
77
+ print_writer.write(node.text)
78
+ closing_tag = true
79
+ else
80
+ node_print_opening_tag(node, print_writer)
81
+ closing_tag = true
82
+ end
83
+
84
+ child = node.first_child
85
+ while child
86
+ node_to_xml(child, print_writer)
87
+ child = child.next_sibling
88
+ end
89
+
90
+ node_print_closing_tag(node, print_writer) if closing_tag
91
+ end
92
+
93
+ def node_print_opening_tag(node, print_writer)
94
+ print_writer.write("<")
95
+ node.print_opening_tag_content_as_xml(print_writer)
96
+ print_writer.write(">")
97
+ end
98
+
99
+ def node_print_closing_tag(node, print_writer)
100
+ print_writer.write("</#{node.tag_name}>")
101
+ end
50
102
  end
51
103
  end
@@ -111,9 +111,9 @@ module Butternut
111
111
  end
112
112
 
113
113
  it "turns off links" do
114
- @page_doc.css('a').each do |link|
115
- link['href'].should == "#"
116
- end
114
+ link = @page_doc.at('a')
115
+ link['href'].should == "#"
116
+ link.inner_html.should == "Picard song"
117
117
  end
118
118
 
119
119
  it "turns off scripts" do
@@ -6,11 +6,11 @@ module Butternut
6
6
 
7
7
  def define_stub_chain
8
8
  # This is what I hate about RSpec.
9
- @stub_request_url = stub("fake request url", :toString => "http://example.com")
10
- @stub_response = stub("fake web response", :getRequestUrl => @stub_request_url)
9
+ @stub_request_url = stub("fake request url", :to_string => "http://example.com")
10
+ @stub_response = stub("fake web response", :request_url => @stub_request_url)
11
11
  @stub_page = stub("fake html page", {
12
12
  :as_xml => "<cheese>pepperjack</cheese>",
13
- :getWebResponse => @stub_response
13
+ :web_response => @stub_response
14
14
  })
15
15
  @stub_element = stub("fake element", :value= => nil, :select => nil, :click => nil, :exist? => true)
16
16
  @stub_empty = stub("fake empty element", :exist? => false)
@@ -48,21 +48,25 @@ module Butternut
48
48
  describe "#current_url" do
49
49
  before(:each) { define_stub_chain }
50
50
  it do
51
- @stub_request_url.should_receive(:toString).and_return("http://google.com")
51
+ @stub_request_url.should_receive(:to_string).and_return("http://google.com")
52
52
  current_url.should == "http://google.com"
53
53
  end
54
54
  end
55
55
 
56
56
  describe "#current_page_source" do
57
- before(:each) { define_stub_chain }
57
+ before(:each) do
58
+ @browser = browser
59
+ visit("file://" + File.expand_path(File.dirname(__FILE__) + "/../fixtures/blargh.html"))
60
+ end
58
61
 
59
- it "returns the current page's source" do
60
- @stub_page.should_receive(:as_xml).and_return("pants")
61
- current_page_source.should == "pants"
62
+ it "constructs the current page's source" do
63
+ # HtmlUnit's text node parsing it a little strange
64
+ expected = "<html><head>\n <title>Blargh</title>\n </head><body>\n <p>Foo</p>\n <p>Bar</p>\n \n</body></html>"
65
+ current_page_source.should == expected
62
66
  end
63
67
 
64
68
  it "returns nil if page is nil" do
65
- @stub_browser.stub!(:page).and_return(nil)
69
+ @browser.stub!(:page).and_return(nil)
66
70
  current_page_source.should be_nil
67
71
  end
68
72
  end
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <head>
3
+ <title>Blargh</title>
4
+ </head>
5
+ <body>
6
+ <p>Foo</p>
7
+ <p>Bar</p>
8
+ </body>
9
+ </html>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: butternut
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Stephens
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-14 00:00:00 Z
12
+ date: 2009-12-15 00:00:00 Z
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -78,6 +78,7 @@ files:
78
78
  - spec/butternut/formatter_spec.rb
79
79
  - spec/butternut/helpers_spec.rb
80
80
  - spec/butternut_spec.rb
81
+ - spec/fixtures/blargh.html
81
82
  - spec/fixtures/css/bar.css
82
83
  - spec/fixtures/facepalm.jpg
83
84
  - spec/fixtures/foo.css