Permalink
Browse files

last weeks work on integrating Locator and removing lots of cruft

  • Loading branch information...
1 parent e28ad70 commit 8f9bb5c304e77a620022a74ad659568dd7257d04 Sven Fuchs committed Feb 9, 2010
Showing with 904 additions and 2,698 deletions.
  1. 0 { → _backup}/app/contollers/test/sessions_controller.rb
  2. 0 { → _backup}/config/routes.rb
  3. 0 { → _backup}/lib/steam/forker.rb
  4. 0 { → _backup}/lib/steam/rails/reloader.rb
  5. 0 { → _backup}/lib/steam/rails/test_case.rb
  6. 0 { → _backup}/test/all.rb
  7. 0 { → _backup}/test/features/steam.feature
  8. 0 { → _backup}/test/features/step_definitions/steam_steps.rb
  9. 0 { → _backup}/test/features/step_definitions/webrat_steps.rb
  10. 0 { → _backup}/test/features/support/env.rb
  11. 0 { → _backup}/test/features/support/hooks.rb
  12. 0 { → _backup}/test/features/support/paths.rb
  13. 0 { → _backup}/test/features/support/responses.rb
  14. 0 { → _backup}/test/fixtures/page.html
  15. BIN { → _backup}/test/fixtures/rails.png
  16. 0 { → _backup}/test/fixtures/rails/Rakefile
  17. 0 { → _backup}/test/fixtures/rails/app/controllers/application_controller.rb
  18. 0 { → _backup}/test/fixtures/rails/app/controllers/users_controller.rb
  19. 0 { → _backup}/test/fixtures/rails/app/models/user.rb
  20. 0 { → _backup}/test/fixtures/rails/app/views/layouts/application.html.erb
  21. 0 { → _backup}/test/fixtures/rails/app/views/users/index.html.erb
  22. 0 { → _backup}/test/fixtures/rails/app/views/users/show.html.erb
  23. 0 { → _backup}/test/fixtures/rails/config/boot.rb
  24. 0 { → _backup}/test/fixtures/rails/config/database.yml
  25. 0 { → _backup}/test/fixtures/rails/config/environment.rb
  26. 0 { → _backup}/test/fixtures/rails/config/environments/development.rb
  27. 0 { → _backup}/test/fixtures/rails/config/environments/production.rb
  28. 0 { → _backup}/test/fixtures/rails/config/environments/test.rb
  29. 0 { → _backup}/test/fixtures/rails/config/initializers/backtrace_silencers.rb
  30. 0 { → _backup}/test/fixtures/rails/config/initializers/inflections.rb
  31. 0 { → _backup}/test/fixtures/rails/config/initializers/mime_types.rb
  32. 0 { → _backup}/test/fixtures/rails/config/initializers/new_rails_defaults.rb
  33. 0 { → _backup}/test/fixtures/rails/config/initializers/session_store.rb
  34. 0 { → _backup}/test/fixtures/rails/config/locales/en.yml
  35. 0 { → _backup}/test/fixtures/rails/config/routes.rb
  36. BIN { → _backup}/test/fixtures/rails/db/development.sqlite3
  37. 0 { → _backup}/test/fixtures/rails/db/migrate/20090926101010_create_users.rb
  38. 0 { → _backup}/test/fixtures/rails/db/schema.rb
  39. BIN { → _backup}/test/fixtures/rails/db/test.sqlite3
  40. BIN { → _backup}/test/fixtures/rails/public/images/rails.png
  41. 0 { → _backup}/test/fixtures/rails/public/javascripts/ajax_call.js
  42. 0 { → _backup}/test/fixtures/rails/public/javascripts/application.js
  43. 0 { → _backup}/test/fixtures/rails/public/javascripts/jquery-ui.js
  44. 0 { → _backup}/test/fixtures/rails/public/javascripts/jquery.js
  45. 0 { → _backup}/test/fixtures/rails/public/stylesheets/application.css
  46. 0 { → _backup}/test/playground/connection.rb
  47. 0 { → _backup}/test/playground/drb/daemonized_client.rb
  48. 0 { → _backup}/test/playground/drb/daemons.rb
  49. 0 { → _backup}/test/playground/drb/forker.rb
  50. 0 { → _backup}/test/playground/drb/server.rb
  51. 0 { → _backup}/test/playground/java_signature.rb
  52. 0 { → _backup}/test/playground/nokogiri.rb
  53. 0 { → _backup}/test/playground/put_headers.rb
  54. 0 { → _backup}/test/playground/rack.rb
  55. 0 { → _backup}/test/playground/rjb_bind.rb
  56. 0 { → _backup}/test/playground/stack_level_problem.rb
  57. 0 { → _backup}/test/playground/thread_problem.rb
  58. 0 { → _backup}/test/playground/web_response_data.rb
  59. 0 { → _backup}/test/playground/webrat.rb
  60. 0 { → _backup}/test/playground/xhr_accept_headers.rb
  61. +154 −0 _backup/test/test_helper.rb
  62. +1 −1 example/cucumber/step_definitions/webrat_compatible_steps.rb
  63. +0 −26 init.rb
  64. +11 −0 lib/core_ext/ruby/hash/except.rb
  65. +14 −0 lib/core_ext/ruby/hash/slice.rb
  66. +43 −15 lib/steam.rb
  67. +1 −2 lib/steam/browser.rb
  68. +31 −20 lib/steam/browser/html_unit.rb
  69. +86 −108 lib/steam/browser/html_unit/actions.rb
  70. +9 −68 lib/steam/browser/html_unit/connection.rb
  71. +0 −10 lib/steam/browser/html_unit/drb.rb
  72. +0 −20 lib/steam/browser/html_unit/drb/client.rb
  73. +0 −27 lib/steam/browser/html_unit/drb/service.rb
  74. +57 −0 lib/steam/browser/html_unit/matchers.rb
  75. +2 −2 lib/steam/browser/html_unit/web_response.rb
  76. +0 −32 lib/steam/configuration.rb
  77. +3 −5 lib/steam/connection.rb
  78. +3 −1 lib/steam/connection/mock.rb
  79. +0 −22 lib/steam/connection/patron.rb
  80. +0 −8 lib/steam/dom.rb
  81. +0 −10 lib/steam/dom/nokogiri.rb
  82. +0 −45 lib/steam/dom/nokogiri/element.rb
  83. +0 −26 lib/steam/dom/nokogiri/page.rb
  84. +7 −0 lib/steam/exceptions.rb
  85. +1 −1 lib/steam/java.rb
  86. +0 −90 lib/steam/locators.rb
  87. +0 −13 lib/steam/locators/area.rb
  88. +0 −95 lib/steam/locators/base.rb
  89. +0 −13 lib/steam/locators/button.rb
  90. +0 −13 lib/steam/locators/element.rb
  91. +0 −39 lib/steam/locators/field.rb
  92. +0 −13 lib/steam/locators/form.rb
  93. +0 −13 lib/steam/locators/label.rb
  94. +0 −9 lib/steam/locators/link.rb
  95. +0 −31 lib/steam/locators/select.rb
  96. +0 −13 lib/steam/locators/select_option.rb
  97. +0 −13 lib/steam/locators/text_area.rb
  98. +0 −5 lib/steam/matchers.rb
  99. +0 −57 lib/steam/matchers/html_unit.rb
  100. +0 −7 lib/steam/not_found_error.rb
  101. +3 −3 lib/steam/request.rb
  102. +7 −29 lib/steam/session.rb
  103. +1 −1 lib/steam/session/rails.rb
  104. +183 −0 test/browser/html_unit/actions_test.rb
  105. +52 −59 test/browser/{html_unit_rails_actions_test.rb → html_unit/rails_actions_test.rb}
  106. +0 −221 test/browser/html_unit_elements_test.rb
  107. +0 −259 test/browser/html_unit_test.rb
  108. +0 −1 test/connection/cascade_test.rb
  109. +0 −34 test/connection/patron_test.rb
  110. +16 −17 test/connection/rails_test.rb
  111. +0 −1 test/connection/static_test.rb
  112. +191 −0 test/fixtures/html_fakes.rb
  113. 0 test/fixtures/rails/log/production.log
  114. 0 test/fixtures/rails/log/server.log
  115. +0 −3 test/locators/all.rb
  116. +0 −3 test/locators/html_unit/all.rb
  117. +0 −43 test/locators/html_unit/area_locator_test.rb
  118. +0 −3 test/locators/html_unit/button_locator/all.rb
  119. +0 −49 test/locators/html_unit/button_locator/button_test.rb
  120. +0 −43 test/locators/html_unit/button_locator/input_button_test.rb
  121. +0 −49 test/locators/html_unit/button_locator/input_image.rb
  122. +0 −49 test/locators/html_unit/button_locator/input_submit_test.rb
  123. +0 −67 test/locators/html_unit/element_locator_test.rb
  124. +0 −89 test/locators/html_unit/field_locator_test.rb
  125. +0 −36 test/locators/html_unit/form_locator_test.rb
  126. +0 −49 test/locators/html_unit/label_locator_test.rb
  127. +0 −43 test/locators/html_unit/link_locator_test.rb
  128. +0 −15 test/locators/html_unit/locate_within_test.rb
  129. +0 −44 test/locators/html_unit/select_option_locator_test.rb
  130. +0 −44 test/locators/html_unit/text_area_locator_test.rb
  131. +0 −3 test/locators/nokogiri/all.rb
  132. +0 −42 test/locators/nokogiri/area_test.rb
  133. +0 −74 test/locators/nokogiri/element_test.rb
  134. +0 −88 test/locators/nokogiri/field_test.rb
  135. +0 −36 test/locators/nokogiri/form_test.rb
  136. +0 −49 test/locators/nokogiri/label_test.rb
  137. +0 −49 test/locators/nokogiri/link_test.rb
  138. +0 −44 test/locators/nokogiri/select_option_test.rb
  139. +0 −44 test/locators/nokogiri/text_area_test.rb
  140. +0 −4 test/session_test.rb
  141. +28 −138 test/test_helper.rb
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,154 @@
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+require 'pp'
+
+TEST_ROOT = File.dirname(__FILE__)
+PLUGIN_ROOT = File.expand_path(TEST_ROOT + "/..")
+
+$: << PLUGIN_ROOT + '/lib'
+
+require 'steam'
+
+Steam::Java.import('com.gargoylesoftware.htmlunit.StringWebResponse')
+Steam::Java.import('com.gargoylesoftware.htmlunit.WebClient')
+Steam::Java.import('com.gargoylesoftware.htmlunit.TopLevelWindow')
+Steam::Java.import('com.gargoylesoftware.htmlunit.DefaultPageCreator')
+
+module TestHelper
+ attr_reader :page
+
+ def assert_response_contains(text, options = {})
+ tag_name = options[:in] || 'body'
+ status, headers, response = yield
+ assert_equal 200, status
+ assert_match %r(<#{tag_name}>\s*#{text}\s*<\/#{tag_name}>), response.body.join
+ end
+
+ def dom(html)
+ Steam::Dom::Nokogiri::Page.new(html)
+ end
+
+ def patron_response(body)
+ @response = Patron::Response.new
+ @response.instance_eval do
+ @body = body
+ end
+ @response
+ end
+end
+
+module HtmlUnitHelper
+ def html(*elements)
+ scripts = ''
+ fields = ''
+ elements.each do |element|
+ case element
+ when :field
+ fields << '<label for="field">Label for field</label><input type="text" id="field" name="field" />'
+ when :textarea
+ fields << '<label for="textarea">Label for textarea</label><textarea id="textarea" name="textarea"></textarea>'
+ when :checkbox
+ fields << '<label for="checkbox">Label for checkbox</label><input id="checkbox" type="checkbox" name="checkbox" value="1" />'
+ when :radio
+ fields << '<label for="radio1">Label for radio</label><input id="radio1" type="radio" name="radio" value="radio" />' \
+ << '<label for="radio2">Label for tv</label><input id="radio2" type="radio" name="radio" value="tv" />'
+ when :select
+ fields << '<label for="select">Label for select</label><select id="select" name="select"><option value=""></option><option value="foo">foo</option></select>'
+ when :file
+ fields << '<label for="file">Label for file</label><input id="file" type="file" name="file" />'
+ when :date
+ fields << '<label for="event_date">Date</label>' \
+ << '<select id="event_date_1i" name="event_date(1i)"><option value="2008">2008</option><option value="2009">2009</option><option value="2010">2010</option></select>' \
+ << '<select id="event_date_2i" name="event_date(2i)"><option value="10">October</option><option value="11">November</option><option value="12">December</option></select>' \
+ << '<select id="event_date_3i" name="event_date(3i)"><option value="6">6</option><option value="7">7</option><option value="8">8</option></select>'
+ when :datetime
+ fields << '<label for="event_datetime">Datetime</label>' \
+ << '<select id="event_datetime_1i" name="event_datetime(1i)"><option value="2008">2008</option><option value="2009">2009</option><option value="2010">2010</option></select>' \
+ << '<select id="event_datetime_2i" name="event_datetime(2i)"><option value="10">October</option><option value="11">November</option><option value="12">December</option></select>' \
+ << '<select id="event_datetime_3i" name="event_datetime(3i)"><option value="6">6</option><option value="7">7</option><option value="8">8</option></select> : ' \
+ << '<select id="event_datetime_4i" name="event_datetime(4i)"><option value="18">18</option><option value="19">19</option><option value="20">20</option></select>' \
+ << '<select id="event_datetime_5i" name="event_datetime(5i)"><option value="0">00</option><option value="1">01</option><option value="2">02</option></select>'
+ when :time
+ fields << '<label for="event_time">Time</label>' \
+ << '<select id="event_time_4i" name="event_time(4i)"><option value="18">18</option><option value="19">19</option><option value="20">20</option></select>' \
+ << '<select id="event_time_5i" name="event_time(5i)"><option value="0">00</option><option value="1">01</option><option value="2">02</option></select>'
+ when :jquery
+ scripts << '<script src="/javascripts/jquery.js" type="text/javascript"></script>'
+ when :jquery_ui
+ scripts << '<script src="/javascripts/jquery-ui.js" type="text/javascript"></script>'
+ when :foo
+ scripts << '<script src="/javascripts/foo.js" type="text/javascript"></script>'
+ when :hover
+ scripts << '<script src="/javascripts/hover.js" type="text/javascript"></script>'
+ when :blur
+ scripts << '<script src="/javascripts/blur.js" type="text/javascript"></script>'
+ when :focus
+ scripts << '<script src="/javascripts/focus.js" type="text/javascript"></script>'
+ when :double_click
+ scripts << '<script src="/javascripts/double_click.js" type="text/javascript"></script>'
+ when :drag
+ scripts << '<script src="/javascripts/drag.js" type="text/javascript"></script>'
+ end
+ end
+
+ html = <<-erb
+ <html>
+ <head><%= scripts %></head>
+ <body>
+ <p id="paragraph"></p>
+ <p><a href="/link" id="link">link</a></p>
+ <form action="/form" method="get" id="form" enctype="multipart/form-data">
+ <%= fields %>
+ <input type="submit" value="button" />
+ </form>
+ </body>
+ </html>
+ erb
+
+ ERB.new(html).result(binding)
+ end
+
+ def setup
+ @mock = Steam::Connection::Mock.new
+
+ root = File.expand_path(TEST_ROOT + '/fixtures')
+ static = Steam::Connection::Static.new(:root => root)
+
+ connection = Rack::Cascade.new([static, @mock])
+ @browser = Steam::Browser::HtmlUnit.new(connection)
+ end
+
+ def perform(method, url, response)
+ @mock.mock(method, url, response)
+ @status, @headers, @response = @browser.call(Steam::Request.env_for(url))
+ end
+end
+
+class LocatorTest < Test::Unit::TestCase
+ include Steam
+ include TestHelper
+
+ attr_reader :response, :page
+
+ def setup
+ @response = Rack::Response.new
+ end
+
+ def default_test
+ end
+end
+
+class NokogiriLocatorTest < LocatorTest
+ def dom(html)
+ Dom::Nokogiri::Page.build(nil, html)
+ end
+end
+
+class HtmlUnitLocatorTest < LocatorTest
+
+ def dom(html)
+ Dom::Nokogiri::Page.build(nil, html)
+ end
+end
+
@@ -130,7 +130,7 @@
Then /^(?:|I )should see "([^\"]*)"$/ do |text|
if defined?(Steam)
- locate_element(text).should_not be_nil
+ locate(text).should_not be_nil
else
response.should contain(text)
end
View
26 init.rb
@@ -9,29 +9,3 @@
# use Steam::Reloader
# end
-Steam.configure do |config|
- # jvm load params - lib/steam/java.rb
- #:javaloadparams: "'-Xms256M', '-Xmx2048M'"
- config.java_load_params = "-Xmx1024M"
-
- # common params - lib/steam/request.rb
- config.server_name = "localhost"
- config.server_port = "3000"
- config.rack_url_scheme = "http"
-
- # char params - lib/steam/browser/html_unit/connection.rb + web_response.rb
- config.charset = "utf-8"
-
- # drb params - lib/steam/browser/html_unit/drb/service.rb
- config.drb_uri = "druby://127.0.0.1:9000"
-
- # JS params - lib/steam/browser/html_unit/html_unit.rb
- #@client.waitForBackgroundJavaScript(5000) # FIXME should probably use some block syntax
-
- # env params - lib/steam/connection/patron.rb
- #headers = {} # FIXME ... request.header, cookies?
- #options = { :data => '' } # FIXME ... body
-
- # env params - lib/steam/session/rails.rb
- config.request_env_for = "http://localhost"
-end
@@ -0,0 +1,11 @@
+class Hash
+ def except!(*keys)
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
+ keys.each { |key| delete(key) }
+ self
+ end
+
+ def except(*keys)
+ dup.except!(*keys)
+ end
+end unless Hash.method_defined?(:slice)
@@ -0,0 +1,14 @@
+class Hash
+ def slice!(*keys)
+ omit = slice(*self.keys - keys)
+ hash = slice(*keys)
+ replace(hash)
+ omit
+ end
+
+ def slice(*keys)
+ hash = self.class.new
+ keys.each { |k| hash[k] = self[k] if has_key?(k) }
+ hash
+ end
+end unless Hash.method_defined?(:slice)
View
@@ -1,19 +1,47 @@
-require 'rubygems'
require 'rack'
module Steam
- autoload :Browser, 'steam/browser'
- autoload :Connection, 'steam/connection'
- autoload :Dom, 'steam/dom'
- autoload :Forker, 'steam/forker'
- autoload :Java, 'steam/java'
- autoload :Locators, 'steam/locators'
- autoload :Matchers, 'steam/matchers'
- autoload :Reloader, 'steam/reloader'
- autoload :Request, 'steam/request'
- autoload :Session, 'steam/session'
-# autoload :Configuration, 'steam/configuration'
- require 'steam/configuration'
- # autoload :TestCase, 'steam/test_case'
- # autoload :Utils, 'steam/utils'
+ autoload :Browser, 'steam/browser'
+ autoload :Connection, 'steam/connection'
+ autoload :ElementNotFound, 'steam/exceptions'
+ autoload :Java, 'steam/java'
+ autoload :Reloader, 'steam/reloader'
+ autoload :Request, 'steam/request'
+ autoload :Session, 'steam/session'
+
+ class << self
+ def config
+ @@config ||= {
+ :request_env_for => 'http://localhost',
+ :server_name => 'localhost',
+ :server_port => '3000',
+ :rack_url_scheme => 'http',
+ :charset => 'utf-8',
+ :java_load_params => '-Xmx1024M'
+ }
+ end
+ end
+
+ # Steam.configure do |config|
+ # # jvm load params - lib/steam/java.rb
+ # # :javaloadparams: "'-Xms256M', '-Xmx2048M'"
+ # config.java_load_params = "-Xmx1024M"
+ #
+ # # common params - lib/steam/request.rb
+ # config.server_name = "localhost"
+ # config.server_port = "3000"
+ # config.rack_url_scheme = "http"
+ #
+ # # char params - lib/steam/browser/html_unit/connection.rb + web_response.rb
+ # config.charset = "utf-8"
+ #
+ # # drb params - lib/steam/browser/html_unit/drb/service.rb
+ # config.drb_uri = "druby://127.0.0.1:9000"
+ #
+ # # JS params - lib/steam/browser/html_unit/html_unit.rb
+ # # @client.waitForBackgroundJavaScript(5000) # FIXME should probably use some block syntax
+ #
+ # # env params - lib/steam/session/rails.rb
+ # config.request_env_for = "http://localhost"
+ # end
end
View
@@ -1,6 +1,5 @@
module Steam
module Browser
- autoload :HtmlUnit, 'steam/browser/html_unit'
- autoload :Connection, 'steam/browser/html_unit/connection'
+ autoload :HtmlUnit, 'steam/browser/html_unit'
end
end
@@ -1,28 +1,21 @@
+require 'locator'
+
module Steam
module Browser
class HtmlUnit
autoload :Actions, 'steam/browser/html_unit/actions'
- autoload :Drb, 'steam/browser/html_unit/drb'
- autoload :Page, 'steam/browser/html_unit/page'
autoload :Client, 'steam/browser/html_unit/client'
autoload :Connection, 'steam/browser/html_unit/connection'
+ autoload :Matchers, 'steam/browser/html_unit/matchers'
+ autoload :Page, 'steam/browser/html_unit/page'
autoload :WebResponse, 'steam/browser/html_unit/web_response'
- include Actions
- include Locators
- include Matchers::HtmlUnit
+ include Actions # Matchers
attr_accessor :client, :page, :connection, :request, :response
def initialize(*args)
- options = args.last.is_a?(Hash) ? args.pop : {}
- connection = args.pop
-
- @client = options[:drb] ? Drb::Client.new : Client.new(connection, options)
- end
-
- def html
- response.body.join
+ @client = Client.new(*args)
end
def request(url)
@@ -31,25 +24,47 @@ def request(url)
alias :visit :request
def call(env)
- @dom = nil
@request = Rack::Request.new(env)
@page = client.request(@request.url)
respond!
end
def execute(javascript)
- @page.executeJavaScript(javascript)
+ @page.executeJavaScript(javascript) # FIXME doesn't respond?
end
def current_url
page.getWebResponse.getRequestSettings.getUrl.toString
end
+
+ def html
+ response.body.join
+ end
+
+ def locate(*args, &block)
+ return args.first if args.first.respond_to?(:_classname)
+
+ # FIXME why not just use self.html?
+ element = Locator.locate(@page.as_xml, *args, &block)
+ element || raise(ElementNotFound.new(*args))
+ # FIXME remove silence_warnings
+ # FIXME raise something meaningful
+ silence_warnings { page.getFirstByXPath(element.xpath) }
+ end
+
+ def within(*args, &block)
+ Locator.within(html, *args, &block)
+ end
protected
+ def action
+ @page = yield || raise('Action did not return a dom.gargoylesoftware.htmlunit.html.HtmlPage.')
+ respond!
+ end
+
def respond!
@client.waitForBackgroundJavaScript(5000) # FIXME should probably use some block syntax
-
body = @page.asXml
status = @page.getWebResponse.getStatusCode
headers = @page.getWebResponse.getResponseHeaders.toArray.inject({}) do |headers, pair|
@@ -58,10 +73,6 @@ def respond!
end
@response = Rack::Response.new(body, status, headers)
@response.to_a
- # rescue Exception => e
- # puts e.message
- # e.backtrace.each { |line| puts line }
- # nil
end
end
end
Oops, something went wrong.

0 comments on commit 8f9bb5c

Please sign in to comment.