Permalink
Browse files

Locator now has Matchers, so let's add them

  • Loading branch information...
1 parent c221251 commit 6d9b40e6086fe6282dca5c9497506fb98d6ef974 Sven Fuchs committed Feb 27, 2010
View
@@ -89,3 +89,10 @@ h2. Developers
* "Sven Fuchs":http://github.com/svenfuchs
* "Clemens Kofler"::http://github.com/clemens
+
+
+TODO
+
+* explain requirement to start/restart a test server
+* explain log levels
+
@@ -2,17 +2,18 @@
# Webdriver might be an interesting alternative.
require 'core_ext/ruby/string/camelize'
+require 'locator'
module Steam
module Browser
autoload :HtmlUnit, 'steam/browser/html_unit'
-
+
class << self
def create(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
type = args.shift if args.first.is_a?(Symbol)
connection = args.pop
-
+
type ||= :html_unit
type = const_get(type.to_s.camelize)
type = type.const_get('Drb') if options[:daemon]
@@ -17,7 +17,7 @@ class HtmlUnit
autoload :Page, 'steam/browser/html_unit/page'
autoload :WebResponse, 'steam/browser/html_unit/web_response'
- include Actions # Matchers
+ include Actions
attr_accessor :client, :page, :connection, :request, :response
@@ -1,57 +0,0 @@
-# module Steam
-# module HtmlUnit
-# module Matchers
-# class HasContent #:nodoc:
-# def initialize(content)
-# @content = content
-# end
-#
-# def matches?(target)
-# @target = target
-# !!@target.locate_element(@content)
-# end
-#
-# def failure_message
-# "expected the following element's content to #{content_message}:\n#{squeeze_space(@target.page.body)}"
-# end
-#
-# def negative_failure_message
-# "expected the following element's content to not #{content_message}:\n#{squeeze_space(@target.page.body)}"
-# end
-#
-# def squeeze_space(inner_text)
-# (inner_text || '').gsub(/^\s*$/, "").squeeze("\n")
-# end
-#
-# def content_message
-# case @content
-# when String
-# "include \"#{@content}\""
-# when Regexp
-# "match #{@content.inspect}"
-# end
-# end
-# end
-#
-# # Matches the contents of an HTML document with
-# # whatever string is supplied
-# def contain(content)
-# HasContent.new(content)
-# end
-#
-# # Asserts that the body of the response contain
-# # the supplied string or regexp
-# def assert_contain(content)
-# hc = HasContent.new(content)
-# assert hc.matches?(response.body), hc.failure_message
-# end
-#
-# # Asserts that the body of the response
-# # does not contain the supplied string or regepx
-# def assert_not_contain(content)
-# hc = HasContent.new(content)
-# assert !hc.matches?(response.body), hc.negative_failure_message
-# end
-# end
-# end
-# end
@@ -8,6 +8,10 @@ module Steam
class Session
autoload :Rails, 'steam/session/rails'
+ include Locator::Matcher
+ include Test::Unit::Assertions if defined?(Test::Unit)
+ # TODO include Rspec::Something if defined?(Rspec)
+
attr_accessor :browser
def initialize(browser = nil)
@@ -5,16 +5,16 @@ class HtmlUnitActionsTest < Test::Unit::TestCase
include Steam, HtmlFakes
def setup
- @app = Steam::Connection::Mock.new
+ @connection = Steam::Connection::Mock.new
static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
- @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
+ @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @connection]))
end
test "click_on clicks on an element" do
perform :get, 'http://localhost:3000/', html
assert_response_contains('LINK') do
- @app.mock :get, 'http://localhost:3000/link', 'LINK'
+ @connection.mock :get, 'http://localhost:3000/link', 'LINK'
@browser.click_on('link')
end
end
@@ -23,7 +23,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :text)
assert_response_contains('FORM') do
- @app.mock :get, 'http://localhost:3000/form?field=', 'FORM'
+ @connection.mock :get, 'http://localhost:3000/form?field=', 'FORM'
@browser.click_on(:button, 'button')
end
end
@@ -32,7 +32,7 @@ def setup
perform :get, 'http://localhost:3000/', html
assert_response_contains('LINK') do
- @app.mock :get, 'http://localhost:3000/link', 'LINK'
+ @connection.mock :get, 'http://localhost:3000/link', 'LINK'
@browser.click_link('link')
end
end
@@ -41,7 +41,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :text)
assert_response_contains('FORM') do
- @app.mock :get, 'http://localhost:3000/form?field=', 'FORM'
+ @connection.mock :get, 'http://localhost:3000/form?field=', 'FORM'
@browser.click_button('button')
end
end
@@ -50,7 +50,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :text)
assert_response_contains('FIELD') do
- @app.mock :get, 'http://localhost:3000/form?field=text', 'FIELD'
+ @connection.mock :get, 'http://localhost:3000/form?field=text', 'FIELD'
@browser.fill_in('field', :with => 'text')
@browser.click_button('button')
end
@@ -60,7 +60,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :textarea)
assert_response_contains('TEXTAREA') do
- @app.mock :get, 'http://localhost:3000/form?textarea=text', 'TEXTAREA'
+ @connection.mock :get, 'http://localhost:3000/form?textarea=text', 'TEXTAREA'
@browser.fill_in('textarea', :with => 'text')
@browser.click_button('button')
end
@@ -70,7 +70,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :checkbox)
assert_response_contains('CHECKED') do
- @app.mock :get, 'http://localhost:3000/form?checkbox=1', 'CHECKED'
+ @connection.mock :get, 'http://localhost:3000/form?checkbox=1', 'CHECKED'
@browser.check('checkbox')
@browser.click_button('button')
end
@@ -80,7 +80,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :checkbox)
assert_response_contains('FORM') do
- @app.mock :get, 'http://localhost:3000/form', 'FORM'
+ @connection.mock :get, 'http://localhost:3000/form', 'FORM'
@browser.check('checkbox')
@browser.uncheck('checkbox')
@browser.click_button('button')
@@ -91,7 +91,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :radio)
assert_response_contains('RADIO') do
- @app.mock :get, 'http://localhost:3000/form?radio=radio', 'RADIO'
+ @connection.mock :get, 'http://localhost:3000/form?radio=radio', 'RADIO'
@browser.choose('radio')
@browser.click_button('button')
end
@@ -101,7 +101,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :select)
assert_response_contains('SELECT') do
- @app.mock :get, 'http://localhost:3000/form?select=foo', 'SELECT'
+ @connection.mock :get, 'http://localhost:3000/form?select=foo', 'SELECT'
@browser.select('foo', :from => 'select')
@browser.click_button('button')
end
@@ -111,7 +111,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :hidden)
assert_response_contains('SELECT') do
- @app.mock :get, 'http://localhost:3000/form?hidden=foo', 'SELECT'
+ @connection.mock :get, 'http://localhost:3000/form?hidden=foo', 'SELECT'
@browser.set_hidden_field('hidden', :to => 'foo')
@browser.click_button('button')
end
@@ -121,7 +121,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :file)
assert_response_contains('FILE') do
- @app.mock :get, 'http://localhost:3000/form?file=rails.png', 'FILE'
+ @connection.mock :get, 'http://localhost:3000/form?file=rails.png', 'FILE'
@browser.attach_file('file', "#{TEST_ROOT}/fixtures/rails.png")
@browser.click_button('button')
end
@@ -131,7 +131,7 @@ def setup
perform :get, 'http://localhost:3000/', html(:fields => :text)
assert_response_contains('FORM') do
- @app.mock :get, 'http://localhost:3000/form?field=', 'FORM'
+ @connection.mock :get, 'http://localhost:3000/form?field=', 'FORM'
@browser.submit_form('form')
end
end
@@ -5,9 +5,9 @@ class HtmlUnitJavascriptTest < Test::Unit::TestCase
include Steam, HtmlFakes
def setup
- @app = Steam::Connection::Mock.new
+ @connection = Steam::Connection::Mock.new
static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
- @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
+ @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @connection]))
end
test "jquery: div:not([id]) selector" do
@@ -5,13 +5,13 @@ class HtmlUnitRailsActionsTest < Test::Unit::TestCase
include Steam, HtmlFakes
def setup
- @app = Steam::Connection::Mock.new
+ @connection = Steam::Connection::Mock.new
static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
- @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
+ @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @connection]))
- @app.mock :get, "http://localhost:3000/form?event_date(1i)=2009&event_date(2i)=11&event_date(3i)=7", 'DATE'
- @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'
- @app.mock :get, "http://localhost:3000/form?event_time(4i)=19&event_time(5i)=0", 'TIME'
+ @connection.mock :get, "http://localhost:3000/form?event_date(1i)=2009&event_date(2i)=11&event_date(3i)=7", 'DATE'
+ @connection.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'
+ @connection.mock :get, "http://localhost:3000/form?event_time(4i)=19&event_time(5i)=0", 'TIME'
end
def test_select_date
@@ -3,61 +3,62 @@
require 'locator'
module HtmlUnitTests
+ include Steam
+
def init
- @app = Steam::Connection::Mock.new
- static = Steam::Connection::Static.new(:root => FIXTURES_PATH)
- @browser = Steam::Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
+ @connection = Connection::Mock.new
+ @browser = Browser::HtmlUnit.new(@connection)
perform(:get, 'http://localhost:3000/', '<div id="foo"><div id="bar"><a id="buz" href="">bar!</a></div></div>')
end
-
+
def locate(*args, &block)
@browser.locate(*args, &block)
end
-
+
def within(*args, &block)
@browser.within(*args, &block)
end
-
+
test 'locate with node type' do
element = locate(:a)
assert_equal 'a', element.name
end
-
+
test 'locate with attributes' do
element = locate(:id => 'buz')
assert_equal 'a', element.name
end
-
+
test 'locate with search text' do
element = locate(:a, 'bar!')
assert_equal 'a', element.name
end
-
+
test 'locate with xpath' do
element = locate(:xpath => '//div/div/a')
assert_equal 'a', element.name
end
-
+
test 'locate with css' do
element = locate(:css => '#foo #bar a')
assert_equal 'a', element.name
end
-
+
test 'within with node type' do
element = within(:div) { within(:div) { locate(:a) } }
assert_equal 'a', element.name
end
-
+
test 'within with attributes' do
element = within(:id => 'foo') { within(:id => 'bar') { locate(:a) } }
assert_equal 'a', element.name
end
-
+
test 'within with xpath' do
element = within('//div/div') { locate(:a) }
assert_equal 'a', element.name
end
-
+
test 'within with css' do
element = within('#foo #bar') { locate(:a) }
assert_equal 'a', element.name
@@ -82,16 +83,16 @@ def setup
end
end
-class HtmlUnitWithHtmlUnitAdapterTest < Test::Unit::TestCase
- include Steam, HtmlUnitTests
-
- def setup
- @old_adapter, Locator::Dom.adapter = Locator::Dom.adapter, Locator::Dom::Htmlunit
- init
- end
-
- def teardown
- Locator::Dom.adapter = @old_adapter
- end
-end
+# class HtmlUnitWithHtmlUnitAdapterTest < Test::Unit::TestCase
+# include Steam, HtmlUnitTests
+#
+# def setup
+# @old_adapter, Locator::Dom.adapter = Locator::Dom.adapter, Locator::Dom::Htmlunit
+# init
+# end
+#
+# def teardown
+# Locator::Dom.adapter = @old_adapter
+# end
+# end
@@ -6,10 +6,10 @@
Steam.config[:html_unit][:java_path] = File.expand_path("../../lib/htmlunit-2.7/", __FILE__)
-@app = Connection::Mock.new
+@connection = Connection::Mock.new
root = File.expand_path("../fixtures/public", __FILE__)
static = Connection::Static.new(:root => root, :urls => %w(/ /javascripts /stylesheets))
-@browser = Browser::HtmlUnit.new(Rack::Cascade.new([static, @app]))
+@browser = Browser::HtmlUnit.new(Rack::Cascade.new([static, @connection]))
def drag_to(target)
@browser.page.executeJavaScript('$("#log").text("")')
Oops, something went wrong.

0 comments on commit 6d9b40e

Please sign in to comment.