test-unit-capybara 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ # -*- ruby -*-
2
+ #
3
+ # Copyright (C) 2011-2012 Kouhei Sutou <kou@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ module Test
20
+ module Unit
21
+ module Capybara
22
+ VERSION = "1.0.1"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ $VERBOSE = true
20
+
21
+ base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
22
+ lib_dir = File.join(base_dir, "lib")
23
+ test_dir = File.join(base_dir, "test")
24
+
25
+ $LOAD_PATH.unshift(lib_dir)
26
+ $LOAD_PATH.unshift(test_dir)
27
+
28
+ require "test-unit-capybara-test-utils"
29
+
30
+ exit Test::Unit::AutoRunner.run(true, test_dir)
@@ -0,0 +1,278 @@
1
+ # -*- ruby -*-
2
+ #
3
+ # Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ require "test-unit-capybara-test-utils"
20
+
21
+ class AssertionsTest < Test::Unit::TestCase
22
+ include Capybara::DSL
23
+
24
+ class BodyTest < self
25
+ setup do
26
+ Capybara.app = lambda do |environment|
27
+ [
28
+ 200,
29
+ {"Content-Type" => "application/json"},
30
+ [JSON.generate({"status" => true})],
31
+ ]
32
+ end
33
+ end
34
+
35
+ def test_with_shortcut_content_type
36
+ visit("/")
37
+ assert_body({"status" => true}, :content_type => :json)
38
+ end
39
+
40
+ def test_without_content_type
41
+ visit("/")
42
+ assert_body({"status" => true})
43
+ end
44
+ end
45
+
46
+ class AllTest < self
47
+ setup do
48
+ @html = <<-HTML
49
+ <html>
50
+ <body>
51
+ <h1>Hello</h1>
52
+ <h2>Yay!</h2>
53
+ <div class="section">
54
+ <h2>World</h2>
55
+ </div>
56
+ </body>
57
+ </html>
58
+ HTML
59
+ Capybara.app = lambda do |environment|
60
+ [
61
+ 200,
62
+ {"Content-Type" => "text/html"},
63
+ [@html],
64
+ ]
65
+ end
66
+ end
67
+
68
+ def test_no_kind
69
+ visit("/")
70
+ h2s = assert_all("h2")
71
+ assert_equal(["Yay!", "World"], h2s.collect(&:text))
72
+ end
73
+
74
+ def test_css
75
+ visit("/")
76
+ h2s = assert_all(:css, "h2")
77
+ assert_equal(["Yay!", "World"], h2s.collect(&:text))
78
+ end
79
+
80
+ def test_xpath
81
+ visit("/")
82
+ h2s = assert_all(:xpath, "//h2")
83
+ assert_equal(["Yay!", "World"], h2s.collect(&:text))
84
+ end
85
+
86
+ def test_node
87
+ visit("/")
88
+ section = find("div.section")
89
+ h2s = assert_all(section, "h2")
90
+ assert_equal(["World"], h2s.collect(&:text))
91
+ end
92
+
93
+ def test_fail_no_context
94
+ visit("/")
95
+ message = <<-EOM.strip
96
+ <"h3">(:css) expected to find one or more elements in
97
+ <#{@html}>
98
+ EOM
99
+ exception = Test::Unit::AssertionFailedError.new(message)
100
+ assert_raise(exception) do
101
+ assert_all("h3")
102
+ end
103
+ end
104
+
105
+ def test_fail_in_context
106
+ visit("/")
107
+ section_html = @html.scan(/<div class="section">.*?<\/div>/m)[0]
108
+ message = <<-EOM.strip
109
+ <"h1">(:css) expected to find one or more elements in
110
+ <#{section_html}>
111
+ EOM
112
+ exception = Test::Unit::AssertionFailedError.new(message)
113
+ within("div.section") do
114
+ assert_raise(exception) do
115
+ assert_all("h1")
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ class NotFindTest < self
122
+ setup do
123
+ @html = <<-HTML
124
+ <html>
125
+ <body>
126
+ <h1>Hello</h1>
127
+ <h2>Yay!</h2>
128
+ <div class="section">
129
+ <h2>World</h2>
130
+ </div>
131
+ </body>
132
+ </html>
133
+ HTML
134
+ Capybara.app = lambda do |environment|
135
+ [
136
+ 200,
137
+ {"Content-Type" => "text/html"},
138
+ [@html],
139
+ ]
140
+ end
141
+ end
142
+
143
+ class WithoutNodeTest < self
144
+ def test_no_kind
145
+ visit("/")
146
+ assert_not_find("h3")
147
+ end
148
+
149
+ def test_css
150
+ visit("/")
151
+ assert_not_find(:css, "h3")
152
+ end
153
+
154
+ def test_xpath
155
+ visit("/")
156
+ assert_not_find(:xpath, "//h3")
157
+ end
158
+
159
+ def test_block
160
+ visit("/")
161
+ within("div.section") do
162
+ assert_not_find("h3")
163
+ end
164
+ end
165
+
166
+ def test_fail_no_context
167
+ visit("/")
168
+ h1_html = @html.scan(/<h1>.*?<\/h1>/m)[0]
169
+ message = <<-EOM.strip
170
+ <"h1">(:css) expected to not find a element but was
171
+ <#{h1_html}> in
172
+ <#{@html}>
173
+ EOM
174
+ exception = Test::Unit::AssertionFailedError.new(message)
175
+ assert_raise(exception) do
176
+ assert_not_find("h1")
177
+ end
178
+ end
179
+
180
+ def test_fail_in_context
181
+ visit("/")
182
+ section_html = @html.scan(/<div class="section">.*?<\/div>/m)[0]
183
+ h2_in_section_html = section_html.scan(/<h2>.*?<\/h2>/m)[0]
184
+ message = <<-EOM.strip
185
+ <"h2">(:css) expected to not find a element but was
186
+ <#{h2_in_section_html}> in
187
+ <#{section_html}>
188
+ EOM
189
+ exception = Test::Unit::AssertionFailedError.new(message)
190
+ within("div.section") do
191
+ assert_raise(exception) do
192
+ assert_not_find("h2")
193
+ end
194
+ end
195
+ end
196
+ end
197
+
198
+ class WithNodeTest < self
199
+ def test_no_kind
200
+ visit("/")
201
+ section = find("div.section")
202
+ assert_not_find(section, "h1")
203
+ end
204
+
205
+ def test_css
206
+ visit("/")
207
+ section = find("div.section")
208
+ assert_not_find(section, :css, "h1")
209
+ end
210
+
211
+ def test_xpath
212
+ visit("/")
213
+ section = find("div.section")
214
+ assert_not_find(section, :xpath, ".//h1")
215
+ end
216
+
217
+ def test_fail
218
+ visit("/")
219
+ section = find("div.section")
220
+ section_html = @html.scan(/<div class="section">.*?<\/div>/m)[0]
221
+ h2_html = section_html.scan(/<h2>.*?<\/h2>/m)[0]
222
+ message = <<-EOM.strip
223
+ <"h2">(:css) expected to not find a element but was
224
+ <#{h2_html}> in
225
+ <#{section_html}>
226
+ EOM
227
+ exception = Test::Unit::AssertionFailedError.new(message)
228
+ assert_raise(exception) do
229
+ assert_not_find(section, "h2")
230
+ end
231
+ end
232
+ end
233
+ end
234
+
235
+ class InspectorTest < self
236
+ setup do
237
+ @html = <<-HTML
238
+ <html>
239
+ <body>
240
+ <h1>Hello</h1>
241
+ <h2>Yay!</h2>
242
+ <div class="section">
243
+ <h2>World</h2>
244
+ </div>
245
+ </body>
246
+ </html>
247
+ HTML
248
+ Capybara.app = lambda do |environment|
249
+ [
250
+ 200,
251
+ {"Content-Type" => "text/html"},
252
+ [@html],
253
+ ]
254
+ end
255
+ end
256
+
257
+ def test_pretty_print
258
+ visit("/")
259
+ section_html = @html.scan(/<div class="section">.*?<\/div>/m)[0]
260
+ message = <<-EOM.strip
261
+ <"XXX"> expected but was
262
+ <\#<Capybara::Element tag="div" path="/html/body/div"
263
+ #{section_html}>>.
264
+
265
+ diff:
266
+ - "XXX"
267
+ + \#<Capybara::Element tag="div" path="/html/body/div"
268
+ + <div class="section">
269
+ + <h2>World</h2>
270
+ + </div>>
271
+ EOM
272
+ exception = Test::Unit::AssertionFailedError.new(message)
273
+ assert_raise(exception) do
274
+ assert_equal("XXX", find("div.section"))
275
+ end
276
+ end
277
+ end
278
+ end
@@ -0,0 +1,28 @@
1
+ # -*- ruby -*-
2
+ #
3
+ # Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ require "bundler/setup"
20
+
21
+ require "test/unit"
22
+ require "test/unit/notify"
23
+ require "test/unit/capybara"
24
+
25
+ require "pp"
26
+
27
+ tmp_path = File.expand_path(File.join("tmp", "capybara"), __FILE__)
28
+ Capybara.save_and_open_page_path = tmp_path
@@ -0,0 +1,96 @@
1
+ # -*- ruby -*-
2
+ #
3
+ # Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com>
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ require "test-unit-capybara-test-utils"
20
+
21
+ class WrapperTest < Test::Unit::TestCase
22
+ class WrappedTest < Test::Unit::TestCase
23
+ include Capybara::DSL
24
+
25
+ @@testing = false
26
+
27
+ class << self
28
+ def testing=(testing)
29
+ @@testing = testing
30
+ end
31
+ end
32
+
33
+ def valid?
34
+ @@testing
35
+ end
36
+
37
+ setup do
38
+ @html = <<-HTML
39
+ <html>
40
+ <body>
41
+ <h1>Hello</h1>
42
+ <h2>Yay!</h2>
43
+ <div class="section">
44
+ <h2>World</h2>
45
+ </div>
46
+ </body>
47
+ </html>
48
+ HTML
49
+ Capybara.app = lambda do |environment|
50
+ [
51
+ 200,
52
+ {"Content-Type" => "text/html"},
53
+ [@html],
54
+ ]
55
+ end
56
+ end
57
+
58
+ def test_find_fail
59
+ visit("/")
60
+ within("div.section") do
61
+ find("h3")
62
+ end
63
+ end
64
+ end
65
+
66
+ def setup
67
+ WrappedTest.testing = true
68
+ end
69
+
70
+ def teardown
71
+ WrappedTest.testing = false
72
+ end
73
+
74
+ def test_find
75
+ result = _run_test
76
+ message = <<-EOM.chomp
77
+ <"h3">(:css) expected to find a element in
78
+ <<div class="section">
79
+ <h2>World</h2>
80
+ </div>>
81
+ EOM
82
+ assert_equal([message],
83
+ result.failures.collect {|failure| failure.message})
84
+ assert_equal("1 tests, 1 assertions, 1 failures, 0 errors, 0 pendings, " \
85
+ "0 omissions, 0 notifications", result.to_s)
86
+ end
87
+
88
+ private
89
+ def _run_test
90
+ result = Test::Unit::TestResult.new
91
+ test = WrappedTest.suite
92
+ yield(test) if block_given?
93
+ test.run(result) {}
94
+ result
95
+ end
96
+ end