/
html_unit_test.rb
98 lines (79 loc) · 2.25 KB
/
html_unit_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require File.expand_path('../../test_helper', __FILE__)
require 'fixtures/html_fakes'
require 'locator'
module HtmlUnitTests
include Steam
def init
@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
end
test 'nesting locate' do
element = locate(:id => 'foo') { locate(:id => 'bar') { locate(:a) } }
assert_equal 'a', element.name
end
test 'nesting within' do
element = within(:id => 'foo') { within(:id => 'bar') { locate(:a) } }
assert_equal 'a', element.name
end
end
class HtmlUnitWithNokogiriAdapterTest < Test::Unit::TestCase
include Steam, HtmlUnitTests
def setup
init
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