steam 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -89,3 +89,10 @@ h2. Developers
89
89
 
90
90
  * "Sven Fuchs":http://github.com/svenfuchs
91
91
  * "Clemens Kofler"::http://github.com/clemens
92
+
93
+
94
+ TODO
95
+
96
+ * explain requirement to start/restart a test server
97
+ * explain log levels
98
+
@@ -9,6 +9,12 @@ module Steam
9
9
  autoload :Response, 'steam/response'
10
10
  autoload :Session, 'steam/session'
11
11
 
12
+ class ElementNotFound < StandardError
13
+ def initialize(*args)
14
+ super "could not find element: #{args.map { |arg| arg.inspect }.join(', ') }"
15
+ end
16
+ end
17
+
12
18
  class << self
13
19
  def config
14
20
  @@config ||= {
@@ -33,11 +39,26 @@ module Steam
33
39
  }
34
40
  }
35
41
  end
36
- end
37
42
 
38
- class ElementNotFound < StandardError
39
- def initialize(*args)
40
- super "could not find element: #{args.map { |arg| arg.inspect }.join(', ') }"
43
+ def save_and_open(url, response)
44
+ filename = "/tmp/steam/#{url.gsub(/[^\w\-\/]/, '_')}.html"
45
+ body = rewrite_assets(url, response.body)
46
+ FileUtils.mkdir_p(File.dirname(filename))
47
+ File.open(filename, "w") { |f| f.write body }
48
+ open_in_browser(filename)
49
+ end
50
+
51
+ def open_in_browser(filename)
52
+ require "launchy"
53
+ Launchy::Browser.run(filename)
54
+ rescue LoadError
55
+ warn "Sorry, you need to install launchy to open pages: `gem install launchy`"
56
+ end
57
+
58
+ def rewrite_assets(url, html)
59
+ url = url.gsub(URI.parse(url).path, '')
60
+ pattern = %r(<script [^>]*src=['"]+([^'"]*)|<link[^>]* href=['"]?([^'"]+))
61
+ html.gsub(pattern) { |path| path.gsub($1 || $2, "#{url}#{$1 || $2}") }
41
62
  end
42
63
  end
43
64
  end
@@ -2,17 +2,18 @@
2
2
  # Webdriver might be an interesting alternative.
3
3
 
4
4
  require 'core_ext/ruby/string/camelize'
5
+ require 'locator'
5
6
 
6
7
  module Steam
7
8
  module Browser
8
9
  autoload :HtmlUnit, 'steam/browser/html_unit'
9
-
10
+
10
11
  class << self
11
12
  def create(*args)
12
13
  options = args.last.is_a?(Hash) ? args.pop : {}
13
14
  type = args.shift if args.first.is_a?(Symbol)
14
15
  connection = args.pop
15
-
16
+
16
17
  type ||= :html_unit
17
18
  type = const_get(type.to_s.camelize)
18
19
  type = type.const_get('Drb') if options[:daemon]
@@ -17,27 +17,31 @@ module Steam
17
17
  autoload :Page, 'steam/browser/html_unit/page'
18
18
  autoload :WebResponse, 'steam/browser/html_unit/web_response'
19
19
 
20
- include Actions # Matchers
20
+ include Actions
21
21
 
22
- attr_accessor :client, :page, :connection, :request, :response
22
+ attr_accessor :client, :page, :request, :response
23
23
 
24
24
  def initialize(*args)
25
25
  @client = Client.new(*args)
26
26
  end
27
27
 
28
+ def connection
29
+ client.connection
30
+ end
31
+
28
32
  def close
29
33
  @client.closeAllWindows
30
34
  end
31
35
 
32
- def request(url)
36
+ def get(url)
33
37
  call Request.env_for(url)
34
38
  end
35
- alias :visit :request
39
+ alias :visit :get
36
40
 
37
41
  def call(env)
38
42
  respond_to do
39
43
  @request = Rack::Request.new(env)
40
- client.request(@request.url)
44
+ client.get(@request.url)
41
45
  end.to_a
42
46
  end
43
47
 
@@ -60,7 +64,7 @@ module Steam
60
64
  end
61
65
 
62
66
  def within(*args, &block)
63
- Locator.within(*args, &block)
67
+ Locator.within(response.body, *args, &block)
64
68
  end
65
69
 
66
70
  protected
@@ -59,7 +59,10 @@ module Steam
59
59
 
60
60
  # TODO implement a way to supply content_type
61
61
  def attach_file(element, path, options = {})
62
- fill_in(element, options.merge(:with => path))
62
+ respond_to do
63
+ element = locate_in_browser(:file, element, options)
64
+ element.setValueAttribute(path)
65
+ end
63
66
  end
64
67
 
65
68
  def submit_form(element, options = {})
@@ -19,7 +19,10 @@ module Steam
19
19
  def notify(message, origin); end
20
20
  end
21
21
 
22
+ attr_reader :connection
23
+
22
24
  def initialize(connection = nil, options = {})
25
+ @connection = connection
23
26
  options = Steam.config[:html_unit].merge(options)
24
27
 
25
28
  @java = WebClient.new(BrowserVersion.send(options[:browser_version]))
@@ -46,7 +49,7 @@ module Steam
46
49
  end
47
50
  end
48
51
 
49
- def request(*args)
52
+ def get(*args)
50
53
  @java.getPage(*args) # TODO use WebRequestSettings
51
54
  end
52
55
 
@@ -8,6 +8,10 @@ module Steam
8
8
  class Session
9
9
  autoload :Rails, 'steam/session/rails'
10
10
 
11
+ include Locator::Matcher
12
+ include Test::Unit::Assertions if defined?(Test::Unit)
13
+ # TODO include Rspec::Something if defined?(Rspec)
14
+
11
15
  attr_accessor :browser
12
16
 
13
17
  def initialize(browser = nil)
@@ -1,3 +1,3 @@
1
1
  module Steam
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -5,16 +5,16 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
5
5
  include Steam, HtmlFakes
6
6
 
7
7
  def setup
8
- @app = Steam::Connection::Mock.new
8
+ mock = Steam::Connection::Mock.new
9
9
  static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
10
- @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
10
+ @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, mock]))
11
11
  end
12
12
 
13
13
  test "click_on clicks on an element" do
14
14
  perform :get, 'http://localhost:3000/', html
15
15
 
16
16
  assert_response_contains('LINK') do
17
- @app.mock :get, 'http://localhost:3000/link', 'LINK'
17
+ mock :get, 'http://localhost:3000/link', 'LINK'
18
18
  @browser.click_on('link')
19
19
  end
20
20
  end
@@ -23,7 +23,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
23
23
  perform :get, 'http://localhost:3000/', html(:fields => :text)
24
24
 
25
25
  assert_response_contains('FORM') do
26
- @app.mock :get, 'http://localhost:3000/form?field=', 'FORM'
26
+ mock :get, 'http://localhost:3000/form?field=', 'FORM'
27
27
  @browser.click_on(:button, 'button')
28
28
  end
29
29
  end
@@ -32,7 +32,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
32
32
  perform :get, 'http://localhost:3000/', html
33
33
 
34
34
  assert_response_contains('LINK') do
35
- @app.mock :get, 'http://localhost:3000/link', 'LINK'
35
+ mock :get, 'http://localhost:3000/link', 'LINK'
36
36
  @browser.click_link('link')
37
37
  end
38
38
  end
@@ -41,7 +41,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
41
41
  perform :get, 'http://localhost:3000/', html(:fields => :text)
42
42
 
43
43
  assert_response_contains('FORM') do
44
- @app.mock :get, 'http://localhost:3000/form?field=', 'FORM'
44
+ mock :get, 'http://localhost:3000/form?field=', 'FORM'
45
45
  @browser.click_button('button')
46
46
  end
47
47
  end
@@ -50,7 +50,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
50
50
  perform :get, 'http://localhost:3000/', html(:fields => :text)
51
51
 
52
52
  assert_response_contains('FIELD') do
53
- @app.mock :get, 'http://localhost:3000/form?field=text', 'FIELD'
53
+ mock :get, 'http://localhost:3000/form?field=text', 'FIELD'
54
54
  @browser.fill_in('field', :with => 'text')
55
55
  @browser.click_button('button')
56
56
  end
@@ -60,7 +60,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
60
60
  perform :get, 'http://localhost:3000/', html(:fields => :textarea)
61
61
 
62
62
  assert_response_contains('TEXTAREA') do
63
- @app.mock :get, 'http://localhost:3000/form?textarea=text', 'TEXTAREA'
63
+ mock :get, 'http://localhost:3000/form?textarea=text', 'TEXTAREA'
64
64
  @browser.fill_in('textarea', :with => 'text')
65
65
  @browser.click_button('button')
66
66
  end
@@ -70,7 +70,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
70
70
  perform :get, 'http://localhost:3000/', html(:fields => :checkbox)
71
71
 
72
72
  assert_response_contains('CHECKED') do
73
- @app.mock :get, 'http://localhost:3000/form?checkbox=1', 'CHECKED'
73
+ mock :get, 'http://localhost:3000/form?checkbox=1', 'CHECKED'
74
74
  @browser.check('checkbox')
75
75
  @browser.click_button('button')
76
76
  end
@@ -80,7 +80,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
80
80
  perform :get, 'http://localhost:3000/', html(:fields => :checkbox)
81
81
 
82
82
  assert_response_contains('FORM') do
83
- @app.mock :get, 'http://localhost:3000/form', 'FORM'
83
+ mock :get, 'http://localhost:3000/form', 'FORM'
84
84
  @browser.check('checkbox')
85
85
  @browser.uncheck('checkbox')
86
86
  @browser.click_button('button')
@@ -91,7 +91,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
91
91
  perform :get, 'http://localhost:3000/', html(:fields => :radio)
92
92
 
93
93
  assert_response_contains('RADIO') do
94
- @app.mock :get, 'http://localhost:3000/form?radio=radio', 'RADIO'
94
+ mock :get, 'http://localhost:3000/form?radio=radio', 'RADIO'
95
95
  @browser.choose('radio')
96
96
  @browser.click_button('button')
97
97
  end
@@ -101,7 +101,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
101
101
  perform :get, 'http://localhost:3000/', html(:fields => :select)
102
102
 
103
103
  assert_response_contains('SELECT') do
104
- @app.mock :get, 'http://localhost:3000/form?select=foo', 'SELECT'
104
+ mock :get, 'http://localhost:3000/form?select=foo', 'SELECT'
105
105
  @browser.select('foo', :from => 'select')
106
106
  @browser.click_button('button')
107
107
  end
@@ -111,7 +111,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
111
111
  perform :get, 'http://localhost:3000/', html(:fields => :hidden)
112
112
 
113
113
  assert_response_contains('SELECT') do
114
- @app.mock :get, 'http://localhost:3000/form?hidden=foo', 'SELECT'
114
+ mock :get, 'http://localhost:3000/form?hidden=foo', 'SELECT'
115
115
  @browser.set_hidden_field('hidden', :to => 'foo')
116
116
  @browser.click_button('button')
117
117
  end
@@ -121,7 +121,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
121
121
  perform :get, 'http://localhost:3000/', html(:fields => :file)
122
122
 
123
123
  assert_response_contains('FILE') do
124
- @app.mock :get, 'http://localhost:3000/form?file=rails.png', 'FILE'
124
+ mock :get, 'http://localhost:3000/form?file=rails.png', 'FILE'
125
125
  @browser.attach_file('file', "#{TEST_ROOT}/fixtures/rails.png")
126
126
  @browser.click_button('button')
127
127
  end
@@ -131,7 +131,7 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
131
131
  perform :get, 'http://localhost:3000/', html(:fields => :text)
132
132
 
133
133
  assert_response_contains('FORM') do
134
- @app.mock :get, 'http://localhost:3000/form?field=', 'FORM'
134
+ mock :get, 'http://localhost:3000/form?field=', 'FORM'
135
135
  @browser.submit_form('form')
136
136
  end
137
137
  end
@@ -5,9 +5,9 @@ class HtmlUnitJavascriptTest < Test::Unit::TestCase
5
5
  include Steam, HtmlFakes
6
6
 
7
7
  def setup
8
- @app = Steam::Connection::Mock.new
8
+ @connection = Steam::Connection::Mock.new
9
9
  static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
10
- @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
10
+ @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @connection]))
11
11
  end
12
12
 
13
13
  test "jquery: div:not([id]) selector" do
@@ -5,13 +5,12 @@ class HtmlUnitRailsActionsTest < Test::Unit::TestCase
5
5
  include Steam, HtmlFakes
6
6
 
7
7
  def setup
8
- @app = Steam::Connection::Mock.new
9
- static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
10
- @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
8
+ connection = Steam::Connection::Mock.new
9
+ @browser = Steam::Browser::HtmlUnit.new(connection)
11
10
 
12
- @app.mock :get, "http://localhost:3000/form?event_date(1i)=2009&event_date(2i)=11&event_date(3i)=7", 'DATE'
13
- @app.mock :get, "http://localhost:3000/form?event_datetime(1i)=2009&event_datetime(2i)=11&event_datetime(3i)=7&event_datetime(4i)=19&event_datetime(5i)=0", 'DATETIME'
14
- @app.mock :get, "http://localhost:3000/form?event_time(4i)=19&event_time(5i)=0", 'TIME'
11
+ mock :get, "http://localhost:3000/form?event_date(1i)=2009&event_date(2i)=11&event_date(3i)=7", 'DATE'
12
+ mock :get, "http://localhost:3000/form?event_datetime(1i)=2009&event_datetime(2i)=11&event_datetime(3i)=7&event_datetime(4i)=19&event_datetime(5i)=0", 'DATETIME'
13
+ mock :get, "http://localhost:3000/form?event_time(4i)=19&event_time(5i)=0", 'TIME'
15
14
  end
16
15
 
17
16
  def test_select_date
@@ -3,61 +3,62 @@ require 'fixtures/html_fakes'
3
3
  require 'locator'
4
4
 
5
5
  module HtmlUnitTests
6
+ include Steam
7
+
6
8
  def init
7
- @app = Steam::Connection::Mock.new
8
- static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
9
- @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
9
+ @connection = Connection::Mock.new
10
+ @browser = Browser::HtmlUnit.new(@connection)
10
11
  perform(:get, 'http://localhost:3000/', '<div id="foo"><div id="bar"><a id="buz" href="">bar!</a></div></div>')
11
12
  end
12
-
13
+
13
14
  def locate(*args, &block)
14
15
  @browser.locate(*args, &block)
15
16
  end
16
-
17
+
17
18
  def within(*args, &block)
18
19
  @browser.within(*args, &block)
19
20
  end
20
-
21
+
21
22
  test 'locate with node type' do
22
23
  element = locate(:a)
23
24
  assert_equal 'a', element.name
24
25
  end
25
-
26
+
26
27
  test 'locate with attributes' do
27
28
  element = locate(:id => 'buz')
28
29
  assert_equal 'a', element.name
29
30
  end
30
-
31
+
31
32
  test 'locate with search text' do
32
33
  element = locate(:a, 'bar!')
33
34
  assert_equal 'a', element.name
34
35
  end
35
-
36
+
36
37
  test 'locate with xpath' do
37
38
  element = locate(:xpath => '//div/div/a')
38
39
  assert_equal 'a', element.name
39
40
  end
40
-
41
+
41
42
  test 'locate with css' do
42
43
  element = locate(:css => '#foo #bar a')
43
44
  assert_equal 'a', element.name
44
45
  end
45
-
46
+
46
47
  test 'within with node type' do
47
48
  element = within(:div) { within(:div) { locate(:a) } }
48
49
  assert_equal 'a', element.name
49
50
  end
50
-
51
+
51
52
  test 'within with attributes' do
52
53
  element = within(:id => 'foo') { within(:id => 'bar') { locate(:a) } }
53
54
  assert_equal 'a', element.name
54
55
  end
55
-
56
+
56
57
  test 'within with xpath' do
57
58
  element = within('//div/div') { locate(:a) }
58
59
  assert_equal 'a', element.name
59
60
  end
60
-
61
+
61
62
  test 'within with css' do
62
63
  element = within('#foo #bar') { locate(:a) }
63
64
  assert_equal 'a', element.name
@@ -82,16 +83,16 @@ class HtmlUnitWithNokogiriAdapterTest < Test::Unit::TestCase
82
83
  end
83
84
  end
84
85
 
85
- class HtmlUnitWithHtmlUnitAdapterTest < Test::Unit::TestCase
86
- include Steam, HtmlUnitTests
87
-
88
- def setup
89
- @old_adapter, Locator::Dom.adapter = Locator::Dom.adapter, Locator::Dom::Htmlunit
90
- init
91
- end
92
-
93
- def teardown
94
- Locator::Dom.adapter = @old_adapter
95
- end
96
- end
86
+ # class HtmlUnitWithHtmlUnitAdapterTest < Test::Unit::TestCase
87
+ # include Steam, HtmlUnitTests
88
+ #
89
+ # def setup
90
+ # @old_adapter, Locator::Dom.adapter = Locator::Dom.adapter, Locator::Dom::Htmlunit
91
+ # init
92
+ # end
93
+ #
94
+ # def teardown
95
+ # Locator::Dom.adapter = @old_adapter
96
+ # end
97
+ # end
97
98
 
@@ -0,0 +1,307 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+ # $: << File.expand_path("../../../lib", __FILE__)
3
+
4
+ require 'cucumber'
5
+ require 'steam'
6
+
7
+ module PathTo
8
+ def path_to(page_name)
9
+ "http://localhost:3000/#{page_name}"
10
+ end
11
+ end
12
+ include PathTo
13
+
14
+ connection = Steam::Connection::Mock.new
15
+ $browser = Steam::Browser.create(connection)
16
+
17
+ $step_mother = Cucumber::StepMother.new
18
+ $step_mother.log = Logger.new(File.open('/dev/null', 'w'))
19
+ steps_file = File.expand_path('../../../example/cucumber/webrat_compatible_steps.rb', __FILE__)
20
+ $step_mother.load_code_file(steps_file)
21
+
22
+ ruby = $step_mother.load_programming_language('rb')
23
+ ruby.build_rb_world_factory([PathTo], lambda { Steam::Session.new($browser) })
24
+ ruby.send(:create_world)
25
+ ruby.send(:extend_world)
26
+
27
+ class WebratCompatStepsTest < Test::Unit::TestCase
28
+ def setup
29
+ @browser = $browser
30
+ end
31
+
32
+ def invoke(step)
33
+ $step_mother.invoke(step)
34
+ end
35
+
36
+ def response
37
+ $browser.response
38
+ end
39
+
40
+ def title
41
+ $browser.page.getTitleText
42
+ end
43
+
44
+ def locate(*args)
45
+ $browser.locate(*args)
46
+ end
47
+
48
+ test 'I go to page' do
49
+ mock :get, path_to('foo'), 'FOO'
50
+ invoke 'I go to foo'
51
+ assert_match 'FOO', response.body
52
+ end
53
+
54
+ test 'I press "button"' do
55
+ perform :get, path_to('foo'), %(<input type="button" id="button" onclick="document.title='OK'">)
56
+ invoke 'I press "button"'
57
+ assert_match 'OK', title
58
+ end
59
+
60
+ test 'I click on "button"' do
61
+ perform :get, path_to('foo'), %(<input type="button" id="button" onclick="document.title='OK'">)
62
+ invoke 'I click on "button"'
63
+ assert_match 'OK', title
64
+ end
65
+
66
+ test 'I follow "link"' do
67
+ perform :get, path_to('foo'), %(<a href="javascript:document.title='OK'">link</a>)
68
+ invoke 'I follow "link"'
69
+ assert_match 'OK', title
70
+ end
71
+
72
+ test 'I follow "link" within "foo"' do
73
+ perform :get, path_to('foo'), %(<a>link</a><div id="foo"><a href="javascript:document.title='OK'">link</a></div>)
74
+ invoke 'I follow "link" within "#foo"'
75
+ assert_match 'OK', title
76
+ end
77
+
78
+ test 'I fill in "foo" with "OK"' do
79
+ perform :get, path_to('foo'), %(<input type="text" name="foo" value="bar">)
80
+ invoke 'I fill in "foo" with "OK"'
81
+ assert_match 'OK', locate(:input).attribute('value')
82
+ end
83
+
84
+ test 'I fill in "foo" for "OK"' do
85
+ perform :get, path_to('foo'), %(<input type="text" name="foo" value="bar">)
86
+ invoke 'I fill in "OK" for "foo"'
87
+ assert_match 'OK', locate(:input).attribute('value')
88
+ end
89
+
90
+ # TODO
91
+ # When /^(?:|I )fill in the following:$/ do |fields|
92
+ # fields.rows_hash.each do |name, value|
93
+ # When %{I fill in "#{name}" with "#{value}"}
94
+ # end
95
+ # end
96
+
97
+ test 'I select "OK" from "foo"' do
98
+ perform :get, path_to('foo'), %(<select name="foo"><option>bar</option><option>OK</option></select>)
99
+ invoke 'I select "OK" from "foo"'
100
+ assert_equal 'OK', locate(:select).value
101
+ end
102
+
103
+ # TODO
104
+ # # When I select "December 25, 2008 10:00" as the date and time
105
+ # When /^(?:|I )select "([^\"]*)" as the date and time$/ do |time|
106
+ # select_datetime(time)
107
+ # end
108
+ #
109
+ # When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date and time$/ do |datetime, select|
110
+ # select_datetime(datetime, :from => select)
111
+ # end
112
+ #
113
+ # # Note: Rail's default time helper provides 24-hour time-- not 12 hour time. Webrat
114
+ # # will convert the 2:20PM to 14:20 and then select it.
115
+ # When /^(?:|I )select "([^\"]*)" as the time$/ do |time|
116
+ # select_time(time)
117
+ # end
118
+ #
119
+ # # When I select "7:30AM" as the "Gym" time
120
+ # When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" time$/ do |time, time_label|
121
+ # select_time(time, :from => time_label)
122
+ # end
123
+ #
124
+ # # When I select "February 20, 1981" as the date
125
+ # When /^(?:|I )select "([^\"]*)" as the date$/ do |date|
126
+ # # reformat_date!(date)
127
+ # select_date(date)
128
+ # end
129
+ #
130
+ # # When I select "April 26, 1982" as the "Date of Birth" date
131
+ # When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date$/ do |date, date_label|
132
+ # # reformat_date!(date)
133
+ # select_date(date, :from => date_label)
134
+ # end
135
+
136
+ test 'I check "foo"' do
137
+ perform :get, path_to('foo'), %(<input type="checkbox" name="foo">)
138
+ invoke 'I check "foo"'
139
+ assert_equal 'checked', locate(:input, :type => 'checkbox').checked
140
+ end
141
+
142
+ test 'I uncheck "foo"' do
143
+ perform :get, path_to('foo'), %(<input type="checkbox" name="foo" checked="checked">)
144
+ invoke 'I uncheck "foo"'
145
+ assert_nil locate(:check_box).checked
146
+ end
147
+
148
+ test 'I choose "foo"' do
149
+ perform :get, path_to('foo'), %(<input type="radio" name="foo">)
150
+ invoke 'I choose "foo"'
151
+ assert_equal 'checked', locate(:radio_button).checked
152
+ end
153
+
154
+ test 'I attach the file at "path" to "foo"' do
155
+ perform :get, path_to('foo'), %(<input type="file" name="foo">)
156
+ invoke 'I attach the file at "path" to "foo"'
157
+ assert_equal 'path', locate(:file).value
158
+ end
159
+
160
+ # I should see
161
+
162
+ test 'I should see "foo" (passes)' do
163
+ perform :get, path_to('foo'), %(foo)
164
+ assert_passes { invoke 'I should see "foo"' }
165
+ end
166
+
167
+ test 'I should see "foo" (fails)' do
168
+ perform :get, path_to('foo'), %(bar)
169
+ assert_fails { invoke 'I should see "foo"' }
170
+ end
171
+
172
+ test 'I should see /foo/ (passes)' do
173
+ perform :get, path_to('foo'), %(foo)
174
+ assert_passes { invoke 'I should see /foo/' }
175
+ end
176
+
177
+ test 'I should see /foo/ (fails)' do
178
+ perform :get, path_to('foo'), %(bar)
179
+ assert_fails { invoke 'I should see /foo/' }
180
+ end
181
+
182
+ # I should not see
183
+
184
+ test 'I should not see "foo" (passes)' do
185
+ perform :get, path_to('foo'), %(bar)
186
+ assert_passes { invoke 'I should not see "foo"' }
187
+ end
188
+
189
+ test 'I should not see "foo" (fails)' do
190
+ perform :get, path_to('foo'), %(foo)
191
+ assert_fails { invoke 'I should not see "foo"' }
192
+ end
193
+
194
+ test 'I should not see /foo/ (passes)' do
195
+ perform :get, path_to('foo'), %(bar)
196
+ assert_passes { invoke 'I should not see /foo/' }
197
+ end
198
+
199
+ test 'I should not see /foo/ (fails)' do
200
+ perform :get, path_to('foo'), %(foo)
201
+ assert_fails { invoke 'I should not see /foo/' }
202
+ end
203
+
204
+ # I should see within
205
+
206
+ test 'I should see "bar" within "foo" (passes)' do
207
+ perform :get, path_to('foo'), %(<div id="foo"><span>bar</span></div>)
208
+ assert_passes { invoke 'I should see "bar" within "#foo"' }
209
+ end
210
+
211
+ test 'I should see "bar" within "foo" (fails)' do
212
+ perform :get, path_to('foo'), %(<div id="foo"></div><span>bar</span>)
213
+ assert_fails { invoke 'I should see "bar" within "#foo"' }
214
+ end
215
+
216
+ test 'I should see /bar/ within "foo" (passes)' do
217
+ perform :get, path_to('foo'), %(<div id="foo"><span>foobar</span></div>)
218
+ assert_passes { invoke 'I should see /bar/ within "#foo"' }
219
+ end
220
+
221
+ test 'I should see /bar/ within "foo" (fails)' do
222
+ perform :get, path_to('foo'), %(<div id="foo"></div><span>foobar</span>)
223
+ assert_fails { invoke 'I should see /bar/ within "#foo"' }
224
+ end
225
+
226
+ # I should not see within
227
+
228
+ test 'I should not see "bar" within "foo" (passes)' do
229
+ perform :get, path_to('foo'), %(<div id="foo"></div><span>bar</span>)
230
+ assert_passes { invoke 'I should not see "bar" within "#foo"' }
231
+ end
232
+
233
+ test 'I should not see "bar" within "foo" (fails)' do
234
+ perform :get, path_to('foo'), %(<div id="foo"><span>bar</span></div>)
235
+ assert_fails { invoke 'I should not see "bar" within "#foo"' }
236
+ end
237
+
238
+ test 'I should not see /bar/ within "foo" (passes)' do
239
+ perform :get, path_to('foo'), %(<div id="foo"></div><span>bar</span>)
240
+ assert_passes { invoke 'I should not see /bar/ within "#foo"' }
241
+ end
242
+
243
+ test 'I should not see /bar/ within "foo" (fails)' do
244
+ perform :get, path_to('foo'), %(<div id="foo"><span>foobar</span></div>)
245
+ assert_fails { invoke 'I should not see /bar/ within "#foo"' }
246
+ end
247
+
248
+ # field should contain
249
+
250
+ test 'the "foo" field should contain "bar" (passes)' do
251
+ perform :get, path_to('foo'), %(<input type="text" name="foo" value="bar">)
252
+ assert_passes { invoke 'the "foo" field should contain "bar"' }
253
+ end
254
+
255
+ test 'the "foo" field should contain "bar" (fails)' do
256
+ perform :get, path_to('foo'), %(<input type="text" name="foo" value="foo">)
257
+ assert_fails { invoke 'the "foo" field should contain "bar"' }
258
+ end
259
+
260
+ # field should not contain
261
+
262
+ test 'the "foo" field should not contain "bar" (passes)' do
263
+ perform :get, path_to('foo'), %(<input type="text" name="foo" value="foo">)
264
+ assert_passes { invoke 'the "foo" field should not contain "bar"' }
265
+ end
266
+
267
+ test 'the "foo" field should not contain "bar" (fails)' do
268
+ perform :get, path_to('foo'), %(<input type="text" name="foo" value="bar">)
269
+ assert_fails { invoke 'the "foo" field should not contain "bar"' }
270
+ end
271
+
272
+ # checkbox should be checked
273
+
274
+ test 'the "foo" checkbox should be checked (passes)' do
275
+ perform :get, path_to('foo'), %(<input type="checkbox" name="foo" checked="checked">)
276
+ assert_passes { invoke 'the "foo" checkbox should be checked' }
277
+ end
278
+
279
+ test 'the "foo" checkbox should be checked (fails)' do
280
+ perform :get, path_to('foo'), %(<input type="checkbox" name="foo">)
281
+ assert_fails { invoke 'the "foo" checkbox should be checked' }
282
+ end
283
+
284
+ # checkbox should not be checked
285
+
286
+ test 'the "foo" checkbox should not be checked (passes)' do
287
+ perform :get, path_to('foo'), %(<input type="checkbox" name="foo">)
288
+ assert_passes { invoke 'the "foo" checkbox should not be checked' }
289
+ end
290
+
291
+ test 'the "foo" checkbox should not be checked (fails)' do
292
+ perform :get, path_to('foo'), %(<input type="checkbox" name="foo" checked="checked">)
293
+ assert_fails { invoke 'the "foo" checkbox should not be checked' }
294
+ end
295
+
296
+ # I should be on
297
+
298
+ test 'I should be on foo (passes)' do
299
+ perform :get, path_to('foo'), ''
300
+ assert_passes { invoke 'I should be on foo' }
301
+ end
302
+
303
+ test 'I should be on foo (fails)' do
304
+ perform :get, path_to('bar'), ''
305
+ assert_fails { invoke 'I should be on foo' }
306
+ end
307
+ end
@@ -6,10 +6,10 @@ include Steam
6
6
 
7
7
  Steam.config[:html_unit][:java_path] = File.expand_path("../../lib/htmlunit-2.7/", __FILE__)
8
8
 
9
- @app = Connection::Mock.new
9
+ @connection = Connection::Mock.new
10
10
  root = File.expand_path("../fixtures/public", __FILE__)
11
11
  static = Connection::Static.new(:root => root, :urls => %w(/ /javascripts /stylesheets))
12
- @browser = Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
12
+ @browser = Browser::HtmlUnit.new(Rack::Cascade.new([static, @connection]))
13
13
 
14
14
  def drag_to(target)
15
15
  @browser.page.executeJavaScript('$("#log").text("")')
@@ -37,14 +37,14 @@ connection = nil
37
37
  url = 'http://localhost:3000'
38
38
 
39
39
  def request_without_drb(browser, url)
40
- browser.request(url)
40
+ browser.get(url)
41
41
  browser.response
42
42
  end
43
43
 
44
44
  def request_with_drb(browser, url)
45
45
  DRb.start_service(Steam.config[:drb_uri], browser)
46
46
  drb = DRbObject.new(nil, Steam.config[:drb_uri])
47
- drb.request(url)
47
+ drb.get(url)
48
48
  drb.response
49
49
  end
50
50
 
@@ -52,6 +52,6 @@ puts page.getTitleText
52
52
  # browser = Browser::HtmlUnit.new(mock)
53
53
  # # client.setWebConnection(Rjb::bind(connection, 'com.gargoylesoftware.htmlunit.WebConnection'))
54
54
  #
55
- # response = browser.request('http://localhost:3000/').last
55
+ # response = browser.get('http://localhost:3000/').last
56
56
  # puts browser.page.asXml
57
57
 
@@ -20,12 +20,12 @@ class ProcessTest < Test::Unit::TestCase
20
20
  end
21
21
 
22
22
  test "new instance recalls an existing pid" do
23
- write_pid(1234)
24
- assert_equal 1234, Process.new.pid
23
+ write_pid(::Process.pid)
24
+ assert_equal ::Process.pid, Process.new.pid
25
25
  end
26
26
 
27
27
  test "pid? returns true if pid was recalled" do
28
- write_pid(1234)
28
+ write_pid(::Process.pid)
29
29
  assert_equal true, Process.new.pid?
30
30
  end
31
31
 
@@ -1,15 +1,40 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/test_helper')
1
+ require File.expand_path('../test_helper', __FILE__)
2
2
 
3
3
  class SessionTest < Test::Unit::TestCase
4
4
  include Steam
5
5
 
6
6
  def setup
7
- connection = Connection::Mock.new
8
- browser = Browser::HtmlUnit.new(connection)
9
- @session = Session.new(browser)
7
+ @browser = Browser::HtmlUnit.new(Connection::Mock.new)
8
+ @session = Session.new(@browser)
9
+
10
+ perform(:get, 'http://localhost:3000/', '<div id="foo"><div id="bar"><a id="buz" href="">bar!</a></div></div>')
11
+ end
12
+
13
+ test 'session responds to browser methods' do
14
+ assert @session.respond_to?(:response)
15
+ end
16
+
17
+ test 'assert_contain' do
18
+ @session.assert_contain(@session.response.body, 'bar!')
19
+ end
20
+
21
+ test 'assert_not_contain' do
22
+ @session.assert_not_contain(@session.response.body, 'bar!!')
23
+ end
24
+
25
+ test 'assert_have_tag' do
26
+ @session.assert_have_tag(@session.response.body, :a, 'bar!', :id => 'buz')
27
+ end
28
+
29
+ test 'assert_have_no_tag' do
30
+ @session.assert_have_no_tag(@session.response.body, :a, 'bar!', :class => 'buz')
31
+ end
32
+
33
+ test 'assert_have_xpath' do
34
+ @session.assert_have_xpath(@session.response.body, '//div/a[@id="buz"]')
10
35
  end
11
36
 
12
- def test_session_responds_to_browser_methods
13
- assert @session.respond_to?(:call)
37
+ test 'assert_have_no_xpath' do
38
+ @session.assert_have_no_xpath(@session.response.body, '//div/a[@class="buz"]')
14
39
  end
15
40
  end
@@ -42,8 +42,14 @@ end
42
42
  class Test::Unit::TestCase
43
43
  include TestMethod
44
44
 
45
+ def mock(method, url, response)
46
+ connection = @browser.connection
47
+ connection = connection.apps.last if connection.is_a?(Rack::Cascade)
48
+ connection.mock(method, url, response)
49
+ end
50
+
45
51
  def perform(method, url, response)
46
- @app.mock(method, url, response)
52
+ mock(method, url, response)
47
53
  @status, @headers, @response = @browser.call(Steam::Request.env_for(url))
48
54
  end
49
55
 
@@ -53,4 +59,22 @@ class Test::Unit::TestCase
53
59
  assert_equal 200, response.status
54
60
  assert_match %r(<#{tag_name}>\s*#{text}\s*<\/#{tag_name}>), response.body
55
61
  end
62
+
63
+ def assert_passes
64
+ begin
65
+ yield
66
+ rescue Test::Unit::AssertionFailedError => e
67
+ ensure
68
+ assert_nil e
69
+ end
70
+ end
71
+
72
+ def assert_fails
73
+ begin
74
+ yield
75
+ rescue Test::Unit::AssertionFailedError => e
76
+ ensure
77
+ assert_not_nil e
78
+ end
79
+ end
56
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-21 00:00:00 +01:00
13
+ date: 2010-02-28 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -61,7 +61,6 @@ files:
61
61
  - lib/steam/browser/html_unit/client.rb
62
62
  - lib/steam/browser/html_unit/connection.rb
63
63
  - lib/steam/browser/html_unit/drb.rb
64
- - lib/steam/browser/html_unit/matchers.rb
65
64
  - lib/steam/browser/html_unit/page.rb
66
65
  - lib/steam/browser/html_unit/web_response.rb
67
66
  - lib/steam/connection.rb
@@ -115,6 +114,7 @@ test_files:
115
114
  - test/connection/mock_test.rb
116
115
  - test/connection/rails_test.rb
117
116
  - test/connection/static_test.rb
117
+ - test/example/webrat_compat_steps_test.rb
118
118
  - test/fixtures/html_fakes.rb
119
119
  - test/java_test.rb
120
120
  - test/playground/connection.rb
@@ -1,57 +0,0 @@
1
- # module Steam
2
- # module HtmlUnit
3
- # module Matchers
4
- # class HasContent #:nodoc:
5
- # def initialize(content)
6
- # @content = content
7
- # end
8
- #
9
- # def matches?(target)
10
- # @target = target
11
- # !!@target.locate_element(@content)
12
- # end
13
- #
14
- # def failure_message
15
- # "expected the following element's content to #{content_message}:\n#{squeeze_space(@target.page.body)}"
16
- # end
17
- #
18
- # def negative_failure_message
19
- # "expected the following element's content to not #{content_message}:\n#{squeeze_space(@target.page.body)}"
20
- # end
21
- #
22
- # def squeeze_space(inner_text)
23
- # (inner_text || '').gsub(/^\s*$/, "").squeeze("\n")
24
- # end
25
- #
26
- # def content_message
27
- # case @content
28
- # when String
29
- # "include \"#{@content}\""
30
- # when Regexp
31
- # "match #{@content.inspect}"
32
- # end
33
- # end
34
- # end
35
- #
36
- # # Matches the contents of an HTML document with
37
- # # whatever string is supplied
38
- # def contain(content)
39
- # HasContent.new(content)
40
- # end
41
- #
42
- # # Asserts that the body of the response contain
43
- # # the supplied string or regexp
44
- # def assert_contain(content)
45
- # hc = HasContent.new(content)
46
- # assert hc.matches?(response.body), hc.failure_message
47
- # end
48
- #
49
- # # Asserts that the body of the response
50
- # # does not contain the supplied string or regepx
51
- # def assert_not_contain(content)
52
- # hc = HasContent.new(content)
53
- # assert !hc.matches?(response.body), hc.negative_failure_message
54
- # end
55
- # end
56
- # end
57
- # end