steam 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. data/MIT-LICENSE +21 -0
  2. data/README.textile +91 -0
  3. data/Rakefile +23 -0
  4. data/TODO +7 -0
  5. data/lib/core_ext/ruby/array/flatten_once.rb +9 -0
  6. data/lib/core_ext/ruby/hash/except.rb +11 -0
  7. data/lib/core_ext/ruby/hash/slice.rb +14 -0
  8. data/lib/core_ext/ruby/kernel/silence_warnings.rb +8 -0
  9. data/lib/core_ext/ruby/process/daemon.rb +23 -0
  10. data/lib/core_ext/ruby/string/camelize.rb +5 -0
  11. data/lib/core_ext/ruby/string/underscore.rb +5 -0
  12. data/lib/steam.rb +43 -0
  13. data/lib/steam/browser.rb +24 -0
  14. data/lib/steam/browser/html_unit.rb +87 -0
  15. data/lib/steam/browser/html_unit/actions.rb +176 -0
  16. data/lib/steam/browser/html_unit/client.rb +74 -0
  17. data/lib/steam/browser/html_unit/connection.rb +79 -0
  18. data/lib/steam/browser/html_unit/drb.rb +45 -0
  19. data/lib/steam/browser/html_unit/matchers.rb +57 -0
  20. data/lib/steam/browser/html_unit/page.rb +51 -0
  21. data/lib/steam/browser/html_unit/web_response.rb +116 -0
  22. data/lib/steam/connection.rb +9 -0
  23. data/lib/steam/connection/mock.rb +57 -0
  24. data/lib/steam/connection/net_http.rb +42 -0
  25. data/lib/steam/connection/open_uri.rb +24 -0
  26. data/lib/steam/connection/rails.rb +20 -0
  27. data/lib/steam/connection/static.rb +33 -0
  28. data/lib/steam/java.rb +74 -0
  29. data/lib/steam/process.rb +53 -0
  30. data/lib/steam/request.rb +49 -0
  31. data/lib/steam/response.rb +13 -0
  32. data/lib/steam/session.rb +30 -0
  33. data/lib/steam/session/rails.rb +33 -0
  34. data/lib/steam/version.rb +3 -0
  35. data/test/all.rb +3 -0
  36. data/test/browser/html_unit/actions_test.rb +183 -0
  37. data/test/browser/html_unit/javascript_test.rb +60 -0
  38. data/test/browser/html_unit/rails_actions_test.rb +151 -0
  39. data/test/browser/html_unit_test.rb +97 -0
  40. data/test/connection/cascade_test.rb +42 -0
  41. data/test/connection/mock_test.rb +58 -0
  42. data/test/connection/rails_test.rb +16 -0
  43. data/test/connection/static_test.rb +14 -0
  44. data/test/fixtures/html_fakes.rb +191 -0
  45. data/test/java_test.rb +29 -0
  46. data/test/playground/connection.rb +19 -0
  47. data/test/playground/dragdrop_behavior.rb +60 -0
  48. data/test/playground/drb.rb +55 -0
  49. data/test/playground/java_signature.rb +22 -0
  50. data/test/playground/nokogiri.rb +15 -0
  51. data/test/playground/put_headers.rb +83 -0
  52. data/test/playground/rack.rb +11 -0
  53. data/test/playground/rjb_bind.rb +42 -0
  54. data/test/playground/stack_level_problem.rb +129 -0
  55. data/test/playground/thread_problem.rb +57 -0
  56. data/test/playground/web_response_data.rb +21 -0
  57. data/test/playground/webrat.rb +48 -0
  58. data/test/playground/xhr_accept_headers.rb +61 -0
  59. data/test/process_test.rb +55 -0
  60. data/test/session_test.rb +15 -0
  61. data/test/test_helper.rb +56 -0
  62. metadata +135 -0
@@ -0,0 +1,57 @@
1
+ # require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ $: << File.expand_path(File.dirname(__FILE__) + "/../../lib")
4
+ require 'steam'
5
+ include Steam
6
+
7
+ Java.import 'com.gargoylesoftware.htmlunit.WebClient'
8
+ Java.import 'com.gargoylesoftware.htmlunit.BrowserVersion'
9
+ Java.import 'com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException'
10
+
11
+ page = <<-erb
12
+ <html>
13
+ <head>
14
+ <script>
15
+ document.title = 'pre ajax call';
16
+ function some_ajax(url) {
17
+ http = new XMLHttpRequest();
18
+ http.open("GET", url, true);
19
+ http.onreadystatechange = function() {
20
+ document.title = document.title + ' ' + http.readyState
21
+ if(http.readyState == 4) document.title = http.responseText
22
+ }
23
+ http.send(null);
24
+ }
25
+ some_ajax('/ajax/1');
26
+ // some_ajax('/ajax/2');
27
+ </script>
28
+ </head>
29
+ </html>
30
+ erb
31
+
32
+ client = Java::WebClient.new(Java::BrowserVersion.FIREFOX_3)
33
+ client.setCssEnabled(true)
34
+ client.setJavaScriptEnabled(true)
35
+
36
+ mock = Connection::Mock.new
37
+ mock.mock :get, 'http://localhost:3000/', page
38
+ mock.mock :get, 'http://localhost:3000/ajax/1', 'post ajax call 1!', 'Content-Type' => 'application/javascript'
39
+ # mock.mock :get, 'http://localhost:3000/ajax/2', 'post ajax call 2!', 'Content-Type' => 'application/javascript'
40
+
41
+ connection = Browser::HtmlUnit::Connection.new(mock)
42
+ client.setWebConnection(Rjb::bind(connection, 'com.gargoylesoftware.htmlunit.WebConnection'))
43
+
44
+ page = client.getPage('http://localhost:3000/')
45
+ puts page.asXml
46
+ puts page.getTitleText
47
+
48
+ # mock = Connection::Mock.new
49
+ # mock.mock :get, 'http://localhost:3000/', page
50
+ # mock.mock :get, 'http://localhost:3000/ajax', 'post ajax call!', 'Content-Type' => 'application/javascript'
51
+ #
52
+ # browser = Browser::HtmlUnit.new(mock)
53
+ # # client.setWebConnection(Rjb::bind(connection, 'com.gargoylesoftware.htmlunit.WebConnection'))
54
+ #
55
+ # response = browser.request('http://localhost:3000/').last
56
+ # puts browser.page.asXml
57
+
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'rjb'
3
+
4
+ class_path = Dir[File.expand_path(File.dirname(__FILE__) + '/../../lib/htmlunit') + '/*.jar'].join(':')
5
+ Rjb::load(class_path)
6
+
7
+
8
+ # klass = Rjb::import('java.lang.String')
9
+ Arrays = Rjb::import('java.util.Arrays')
10
+ WebResponseData = Rjb::import('com.gargoylesoftware.htmlunit.WebResponseData')
11
+
12
+ # WebResponseData.getConstructors.each do |method|
13
+ # params = method.getParameterTypes.map { |type| type.getName }.join(', ')
14
+ # puts "#{method.getName}(#{params})"
15
+ # end
16
+
17
+ headers = Arrays.asList([])
18
+
19
+ # com.gargoylesoftware.htmlunit.WebResponseData([B, int, java.lang.String, java.util.List)
20
+ signature = '[B;I;Ljava.lang.String;Ljava.util.List;'
21
+ WebResponseData.new_with_sig(signature, 'foo'.split(/./), 1, "", headers)
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'webrat'
3
+
4
+
5
+ class Browser
6
+ include Webrat::Locators
7
+
8
+ class Cache
9
+ def elements
10
+ @elements ||= {}
11
+ end
12
+
13
+ def clear
14
+ @elements.clear
15
+ end
16
+ end
17
+
18
+ def initialize
19
+ @cache = Cache.new
20
+ end
21
+
22
+ def find_link(text_or_title_or_id)
23
+ element = LinkLocator.new(@cache, dom, text_or_title_or_id).locate!
24
+ end
25
+
26
+ def dom
27
+ Webrat::XML.xml_document(html)
28
+ end
29
+
30
+ def html
31
+ <<-html
32
+ <html>
33
+ <head>
34
+ </head>
35
+ <body>
36
+ <a href="/">Home</a>
37
+ <p>Welcome!</p>
38
+ <p>Click here to <a href="/foo">foo</a>.</p>
39
+ </body>
40
+ </html>
41
+ html
42
+ end
43
+ end
44
+
45
+ browser = Browser.new
46
+ # p Webrat::XML.xpath_to(browser.find_link_element('foo'))
47
+ p browser.find_link('foo').path
48
+
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'rjb'
3
+
4
+ class_path = Dir[File.expand_path(File.dirname(__FILE__) + '/../../lib/htmlunit') + '/*.jar'].join(':')
5
+
6
+ Rjb::load(class_path)
7
+
8
+ Url = Rjb::import('java.net.URL')
9
+ WebClient = Rjb::import('com.gargoylesoftware.htmlunit.WebClient')
10
+ HtmlUnitMockWebConnection = Rjb::import('com.gargoylesoftware.htmlunit.MockWebConnection')
11
+
12
+ class MockConnection
13
+ attr_reader :connection
14
+
15
+ def initialize
16
+ @connection = HtmlUnitMockWebConnection.new
17
+ end
18
+
19
+ def method_missing(method, *args)
20
+ p [method, args.first.toString]
21
+ @connection.send(method, *args)
22
+ end
23
+ end
24
+
25
+ connection = MockConnection.new
26
+ # connection = HtmlUnitMockWebConnection.new
27
+
28
+ content = <<-html
29
+ <html>
30
+ <head>
31
+ <script>
32
+ document.title = 'pre ajax call';
33
+ function some_ajax(url) {
34
+ xhr = new XMLHttpRequest();
35
+ xhr.open("GET", url, true);
36
+ xhr.setRequestHeader("Accept", "application/json, text/javascript, */*");
37
+ xhr.onreadystatechange = function() {
38
+ document.title = document.title + ' ' + xhr.readyState
39
+ if(xhr.readyState == 4) document.title = xhr.responseText
40
+ }
41
+ xhr.send(null);
42
+ }
43
+ some_ajax('/ajax/1');
44
+ </script>
45
+ </head>
46
+ </html>
47
+ html
48
+ url = Url.new('http://localhost:3000/foo')
49
+ connection.setResponse(url, content, 'text/html')
50
+
51
+ content = 'document.title = "FOO";'
52
+ url = Url.new('http://localhost:3000/ajax/1')
53
+ connection.setResponse(url, content, 'application/javascript')
54
+
55
+ client = WebClient.new
56
+ client.setCssEnabled(true)
57
+ client.setJavaScriptEnabled(true)
58
+ client.setWebConnection(Rjb::bind(connection, 'com.gargoylesoftware.htmlunit.WebConnection'))
59
+ # client.setWebConnection(connection)
60
+ page = client.getPage('http://localhost:3000/foo')
61
+ # puts page.asXml
@@ -0,0 +1,55 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class ProcessTest < Test::Unit::TestCase
4
+ include Steam
5
+
6
+ def setup
7
+ delete_pid
8
+ end
9
+
10
+ def teardown
11
+ delete_pid
12
+ end
13
+
14
+ def write_pid(pid)
15
+ File.open('/tmp/steam.pid', 'w') { |f| f.write(pid) }
16
+ end
17
+
18
+ def delete_pid
19
+ File.delete('/tmp/steam.pid') rescue Errno::ENOENT
20
+ end
21
+
22
+ test "new instance recalls an existing pid" do
23
+ write_pid(1234)
24
+ assert_equal 1234, Process.new.pid
25
+ end
26
+
27
+ test "pid? returns true if pid was recalled" do
28
+ write_pid(1234)
29
+ assert_equal true, Process.new.pid?
30
+ end
31
+
32
+ test "pid? returns false if pid could not be recalled" do
33
+ assert_equal false, Process.new.pid?
34
+ end
35
+
36
+ test "running? returns true if pid refers to an existing process" do
37
+ write_pid(::Process.pid)
38
+ assert_equal true, Process.new.running?
39
+ end
40
+
41
+ test "running? returns false if pid does not refer to an existing process" do
42
+ write_pid(1073741823)
43
+ assert_equal false, Process.new.running?
44
+ end
45
+
46
+ # # WTF the child process actually writes to /tmp/out.txt but File.read('/tmp/out.txt')
47
+ # # returns an empty string
48
+ # test "fork forks the process, reopens given streams and yields the given block" do
49
+ # File.delete('/tmp/out.txt') rescue Errno::ENOENT
50
+ # io = File.open('/tmp/out.txt', 'w+')
51
+ # io.sync = true
52
+ # Process.new.fork(:out => io) { puts 'foo' }
53
+ # assert_equal 'foo', File.open('/tmp/out.txt', 'r') { |f| f.read }
54
+ # end
55
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class SessionTest < Test::Unit::TestCase
4
+ include Steam
5
+
6
+ def setup
7
+ connection = Connection::Mock.new
8
+ browser = Browser::HtmlUnit.new(connection)
9
+ @session = Session.new(browser)
10
+ end
11
+
12
+ def test_session_responds_to_browser_methods
13
+ assert @session.respond_to?(:call)
14
+ end
15
+ end
@@ -0,0 +1,56 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+ require 'erb'
4
+
5
+ TEST_ROOT = File.expand_path("../", __FILE__)
6
+
7
+ $: << File.expand_path("../lib", TEST_ROOT)
8
+ $: << TEST_ROOT
9
+
10
+ FIXTURES_PATH = File.expand_path("../fixtures", __FILE__)
11
+
12
+ require 'steam'
13
+
14
+ # Steam::Java.import('com.gargoylesoftware.htmlunit.StringWebResponse')
15
+ # Steam::Java.import('com.gargoylesoftware.htmlunit.WebClient')
16
+ # Steam::Java.import('com.gargoylesoftware.htmlunit.TopLevelWindow')
17
+ # Steam::Java.import('com.gargoylesoftware.htmlunit.DefaultPageCreator')
18
+
19
+ module TestMethod
20
+ def self.included(base)
21
+ base.class_eval do
22
+ def test(name, &block)
23
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
24
+ defined = instance_method(test_name) rescue false
25
+ raise "#{test_name} is already defined in #{self}" if defined
26
+ if block_given?
27
+ define_method(test_name, &block)
28
+ else
29
+ define_method(test_name) do
30
+ flunk "No implementation provided for #{name}"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ class Module
39
+ include TestMethod
40
+ end
41
+
42
+ class Test::Unit::TestCase
43
+ include TestMethod
44
+
45
+ def perform(method, url, response)
46
+ @app.mock(method, url, response)
47
+ @status, @headers, @response = @browser.call(Steam::Request.env_for(url))
48
+ end
49
+
50
+ def assert_response_contains(text, options = {})
51
+ tag_name = options[:in] || 'body'
52
+ response = yield
53
+ assert_equal 200, response.status
54
+ assert_match %r(<#{tag_name}>\s*#{text}\s*<\/#{tag_name}>), response.body
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: steam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Sven Fuchs
8
+ - Clemens Kofler
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-02-21 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rjb
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.2.0
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: locator
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.0.4
35
+ version:
36
+ description: Steam is a headless integration testing tool driving HtmlUnit to enable testing JavaScript-driven web sites.
37
+ email: svenfuchs@artweb-design.de
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - README.textile
44
+ - TODO
45
+ files:
46
+ - MIT-LICENSE
47
+ - README.textile
48
+ - Rakefile
49
+ - TODO
50
+ - lib/core_ext/ruby/array/flatten_once.rb
51
+ - lib/core_ext/ruby/hash/except.rb
52
+ - lib/core_ext/ruby/hash/slice.rb
53
+ - lib/core_ext/ruby/kernel/silence_warnings.rb
54
+ - lib/core_ext/ruby/process/daemon.rb
55
+ - lib/core_ext/ruby/string/camelize.rb
56
+ - lib/core_ext/ruby/string/underscore.rb
57
+ - lib/steam.rb
58
+ - lib/steam/browser.rb
59
+ - lib/steam/browser/html_unit.rb
60
+ - lib/steam/browser/html_unit/actions.rb
61
+ - lib/steam/browser/html_unit/client.rb
62
+ - lib/steam/browser/html_unit/connection.rb
63
+ - lib/steam/browser/html_unit/drb.rb
64
+ - lib/steam/browser/html_unit/matchers.rb
65
+ - lib/steam/browser/html_unit/page.rb
66
+ - lib/steam/browser/html_unit/web_response.rb
67
+ - lib/steam/connection.rb
68
+ - lib/steam/connection/mock.rb
69
+ - lib/steam/connection/net_http.rb
70
+ - lib/steam/connection/open_uri.rb
71
+ - lib/steam/connection/rails.rb
72
+ - lib/steam/connection/static.rb
73
+ - lib/steam/java.rb
74
+ - lib/steam/process.rb
75
+ - lib/steam/request.rb
76
+ - lib/steam/response.rb
77
+ - lib/steam/session.rb
78
+ - lib/steam/session/rails.rb
79
+ - lib/steam/version.rb
80
+ has_rdoc: true
81
+ homepage: http://github.com/svenfuchs/steam
82
+ licenses: []
83
+
84
+ post_install_message:
85
+ rdoc_options:
86
+ - --charset=UTF-8
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ version:
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.3.5
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: "Headless integration testing w/ HtmlUnit: enables testing JavaScript-driven web sites"
108
+ test_files:
109
+ - test/all.rb
110
+ - test/browser/html_unit/actions_test.rb
111
+ - test/browser/html_unit/javascript_test.rb
112
+ - test/browser/html_unit/rails_actions_test.rb
113
+ - test/browser/html_unit_test.rb
114
+ - test/connection/cascade_test.rb
115
+ - test/connection/mock_test.rb
116
+ - test/connection/rails_test.rb
117
+ - test/connection/static_test.rb
118
+ - test/fixtures/html_fakes.rb
119
+ - test/java_test.rb
120
+ - test/playground/connection.rb
121
+ - test/playground/dragdrop_behavior.rb
122
+ - test/playground/drb.rb
123
+ - test/playground/java_signature.rb
124
+ - test/playground/nokogiri.rb
125
+ - test/playground/put_headers.rb
126
+ - test/playground/rack.rb
127
+ - test/playground/rjb_bind.rb
128
+ - test/playground/stack_level_problem.rb
129
+ - test/playground/thread_problem.rb
130
+ - test/playground/web_response_data.rb
131
+ - test/playground/webrat.rb
132
+ - test/playground/xhr_accept_headers.rb
133
+ - test/process_test.rb
134
+ - test/session_test.rb
135
+ - test/test_helper.rb