1
- require 'active_support/deprecation'
2
1
require_relative 'selector_assertions/count_describable'
3
2
require_relative 'selector_assertions/html_selector'
4
3
5
4
module Rails
6
5
module Dom
7
6
module Testing
8
7
module Assertions
9
- # Adds the +assert_select + method for use in Rails functional
8
+ # Adds the +assert_dom + method for use in Rails functional
10
9
# test cases, which can be used to make assertions on the response HTML of a controller
11
- # action. You can also call +assert_select + within another +assert_select + to
10
+ # action. You can also call +assert_dom + within another +assert_dom + to
12
11
# make assertions on elements selected by the enclosing assertion.
13
12
#
14
13
# Use +css_select+ to select elements without making an assertions, either
15
14
# from the response HTML or elements selected by the enclosing assertion.
16
15
#
17
16
# In addition to HTML responses, you can make the following assertions:
18
17
#
19
- # * +assert_select_encoded + - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
20
- # * +assert_select_email + - Assertions on the HTML body of an e-mail.
18
+ # * +assert_dom_encoded + - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
19
+ # * +assert_dom_email + - Assertions on the HTML body of an e-mail.
21
20
module SelectorAssertions
22
21
23
22
# Select and return all matching elements.
@@ -70,41 +69,41 @@ def css_select(*args)
70
69
# starting from (and including) that element and all its children in
71
70
# depth-first order.
72
71
#
73
- # If no element is specified +assert_select + selects from
72
+ # If no element is specified +assert_dom + selects from
74
73
# the element returned in +document_root_element+
75
- # unless +assert_select + is called from within an +assert_select + block.
76
- # Override +document_root_element+ to tell +assert_select + what to select from.
74
+ # unless +assert_dom + is called from within an +assert_dom + block.
75
+ # Override +document_root_element+ to tell +assert_dom + what to select from.
77
76
# The default implementation raises an exception explaining this.
78
77
#
79
- # When called with a block +assert_select + passes an array of selected elements
80
- # to the block. Calling +assert_select + from the block, with no element specified,
78
+ # When called with a block +assert_dom + passes an array of selected elements
79
+ # to the block. Calling +assert_dom + from the block, with no element specified,
81
80
# runs the assertion on the complete set of elements selected by the enclosing assertion.
82
- # Alternatively the array may be iterated through so that +assert_select + can be called
81
+ # Alternatively the array may be iterated through so that +assert_dom + can be called
83
82
# separately for each element.
84
83
#
85
84
#
86
85
# ==== Example
87
86
# If the response contains two ordered lists, each with four list elements then:
88
- # assert_select "ol" do |elements|
87
+ # assert_dom "ol" do |elements|
89
88
# elements.each do |element|
90
- # assert_select element, "li", 4
89
+ # assert_dom element, "li", 4
91
90
# end
92
91
# end
93
92
#
94
93
# will pass, as will:
95
- # assert_select "ol" do
96
- # assert_select "li", 8
94
+ # assert_dom "ol" do
95
+ # assert_dom "li", 8
97
96
# end
98
97
#
99
98
# The selector may be a CSS selector expression (String, Symbol, or Numeric) or an expression
100
99
# with substitution values (Array).
101
100
# Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution.
102
- # assert_select returns nil if called with an invalid css selector.
101
+ # assert_dom returns nil if called with an invalid css selector.
103
102
#
104
- # assert_select "div:match('id', ?)", "id_string"
105
- # assert_select "div:match('id', ?)", :id_string
106
- # assert_select "div:match('id', ?)", 1
107
- # assert_select "div:match('id', ?)", /\d+/
103
+ # assert_dom "div:match('id', ?)", "id_string"
104
+ # assert_dom "div:match('id', ?)", :id_string
105
+ # assert_dom "div:match('id', ?)", 1
106
+ # assert_dom "div:match('id', ?)", /\d+/
108
107
#
109
108
# === Equality Tests
110
109
#
@@ -136,32 +135,32 @@ def css_select(*args)
136
135
# evaluated the block is called with an array of all matched elements.
137
136
#
138
137
# # At least one form element
139
- # assert_select "form"
138
+ # assert_dom "form"
140
139
#
141
140
# # Form element includes four input fields
142
- # assert_select "form input", 4
141
+ # assert_dom "form input", 4
143
142
#
144
143
# # Page title is "Welcome"
145
- # assert_select "title", "Welcome"
144
+ # assert_dom "title", "Welcome"
146
145
#
147
146
# # Page title is "Welcome" and there is only one title element
148
- # assert_select "title", {count: 1, text: "Welcome"},
147
+ # assert_dom "title", {count: 1, text: "Welcome"},
149
148
# "Wrong title or more than one title element"
150
149
#
151
150
# # Page contains no forms
152
- # assert_select "form", false, "This page must contain no forms"
151
+ # assert_dom "form", false, "This page must contain no forms"
153
152
#
154
153
# # Test the content and style
155
- # assert_select "body div.header ul.menu"
154
+ # assert_dom "body div.header ul.menu"
156
155
#
157
156
# # Use substitution values
158
- # assert_select "ol>li:match('id', ?)", /item-\d+/
157
+ # assert_dom "ol>li:match('id', ?)", /item-\d+/
159
158
#
160
159
# # All input fields in the form have a name
161
- # assert_select "form input" do
162
- # assert_select ":match('name', ?)", /.+/ # Not empty
160
+ # assert_dom "form input" do
161
+ # assert_dom ":match('name', ?)", /.+/ # Not empty
163
162
# end
164
- def assert_select ( *args , &block )
163
+ def assert_dom ( *args , &block )
165
164
@selected ||= nil
166
165
167
166
selector = HTMLSelector . new ( args , @selected ) { nodeset document_root_element }
@@ -179,7 +178,6 @@ def assert_select(*args, &block)
179
178
end
180
179
end
181
180
alias_method :assert_dom , :assert_select
182
- deprecate assert_select : "assert_select will be renamed to assert_dom in an upcoming release"
183
181
184
182
# Extracts the content of an element, treats it as encoded HTML and runs
185
183
# nested assertion on it.
@@ -192,30 +190,30 @@ def assert_select(*args, &block)
192
190
# element +encoded+. It then calls the block with all un-encoded elements.
193
191
#
194
192
# # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
195
- # assert_select "feed[xmlns='http://www.w3.org/2005/Atom']" do
193
+ # assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do
196
194
# # Select each entry item and then the title item
197
- # assert_select "entry>title" do
195
+ # assert_dom "entry>title" do
198
196
# # Run assertions on the encoded title elements
199
- # assert_select_encoded do
200
- # assert_select "b"
197
+ # assert_dom_encoded do
198
+ # assert_dom "b"
201
199
# end
202
200
# end
203
201
# end
204
202
#
205
203
#
206
204
# # Selects all paragraph tags from within the description of an RSS feed
207
- # assert_select "rss[version=2.0]" do
205
+ # assert_dom "rss[version=2.0]" do
208
206
# # Select description element of each feed item.
209
- # assert_select "channel>item>description" do
207
+ # assert_dom "channel>item>description" do
210
208
# # Run assertions on the encoded elements.
211
- # assert_select_encoded do
212
- # assert_select "p"
209
+ # assert_dom_encoded do
210
+ # assert_dom "p"
213
211
# end
214
212
# end
215
213
# end
216
- def assert_select_encoded ( element = nil , &block )
214
+ def assert_dom_encoded ( element = nil , &block )
217
215
if !element && !@selected
218
- raise ArgumentError , "Element is required when called from a nonnested assert_select "
216
+ raise ArgumentError , "Element is required when called from a nonnested assert_dom "
219
217
end
220
218
221
219
content = nodeset ( element || @selected ) . map do |elem |
@@ -229,50 +227,48 @@ def assert_select_encoded(element = nil, &block)
229
227
if content . empty?
230
228
yield selected
231
229
else
232
- assert_select ":root" , &block
230
+ assert_dom ":root" , &block
233
231
end
234
232
end
235
233
end
236
234
alias_method :assert_dom_encoded , :assert_select_encoded
237
- deprecate assert_select_encoded : "assert_select_encoded will be renamed to assert_dom_encoded in an upcoming release"
238
235
239
236
# Extracts the body of an email and runs nested assertions on it.
240
237
#
241
238
# You must enable deliveries for this assertion to work, use:
242
239
# ActionMailer::Base.perform_deliveries = true
243
240
#
244
- # assert_select_email do
245
- # assert_select "h1", "Email alert"
241
+ # assert_dom_email do
242
+ # assert_dom "h1", "Email alert"
246
243
# end
247
244
#
248
- # assert_select_email do
249
- # items = assert_select "ol>li"
245
+ # assert_dom_email do
246
+ # items = assert_dom "ol>li"
250
247
# items.each do
251
248
# # Work with items here...
252
249
# end
253
250
# end
254
- def assert_select_email ( &block )
251
+ def assert_dom_email ( &block )
255
252
deliveries = ActionMailer ::Base . deliveries
256
253
assert !deliveries . empty? , "No e-mail in delivery list"
257
254
258
255
deliveries . each do |delivery |
259
256
( delivery . parts . empty? ? [ delivery ] : delivery . parts ) . each do |part |
260
257
if part [ "Content-Type" ] . to_s =~ /^text\/ html\W /
261
258
root = Nokogiri ::HTML ::DocumentFragment . parse ( part . body . to_s )
262
- assert_select root , ":root" , &block
259
+ assert_dom root , ":root" , &block
263
260
end
264
261
end
265
262
end
266
263
end
267
264
alias_method :assert_dom_email , :assert_select_email
268
- deprecate assert_select_email : "assert_select_email will be renamed to assert_dom_email in an upcoming release"
269
265
270
266
private
271
267
include CountDescribable
272
268
273
269
def document_root_element
274
270
raise NotImplementedError , 'Implementing document_root_element makes ' \
275
- 'assert_select work without needing to specify an element to select from.'
271
+ 'assert_dom work without needing to specify an element to select from.'
276
272
end
277
273
278
274
# +equals+ must contain :minimum, :maximum and :count keys
@@ -289,7 +285,7 @@ def assert_size_match!(size, equals, css_selector, message = nil)
289
285
end
290
286
291
287
def nest_selection ( selection )
292
- # Set @selected to allow nested assert_select .
288
+ # Set @selected to allow nested assert_dom .
293
289
# Can be nested several levels deep.
294
290
old_selected , @selected = @selected , selection
295
291
yield @selected
0 commit comments