-
Notifications
You must be signed in to change notification settings - Fork 1
/
monocle.ex
365 lines (284 loc) · 11.5 KB
/
monocle.ex
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
defmodule Monocle do
@moduledoc """
Test the presence of elements in a rendered component using their data-test attributes,
data-test attribute values or a combination of both. This module uses Floki.
"""
@doc """
Checks if an element with a given data-test attribute is present in the rendered component.
## Example
Let's say we have the following html code, and we want to check if the confirmation button
is present in the rendered component:
```html
<div data-test-confirmation-button>Confirm</div>
```
We can do so by calling the `we_see_attribute/2` function on the html of the rendered component:
```elixir
assert we_see_attribute(html, "confirmation button")
```
## Arguments
html - the html of the rendered component
attribute - the data-test attribute of the element we want to check for
"""
@spec we_see_attribute(String.t(), String.t()) :: boolean()
def we_see_attribute(html, attribute) do
element_by_attribute(html, attribute)
|> length() > 0
end
@doc """
Similar to `we_see_attribute/2`, but also checks if the element has the given content.
## Example
Let's say we have the following html code, and we want to check if the confirmation button
is present in the rendered component:
```html
<div data-test-confirmation-button>Confirm</div>
```
We can do so by calling the `we_see_attribute/3` function on the html of the rendered component:
```elixir
assert we_see_attribute(html, "confirmation button", content: "Confirm")
```
## Arguments
html - the html of the rendered component
attribute - the data-test attribute of the element we want to check for
content - the content of the element we want to check for
"""
@spec we_see_attribute(String.t(), String.t(), keyword()) :: boolean()
def we_see_attribute(html, attribute, content: content) do
element_by_attribute(html, attribute)
|> number_of_elements_with_content(content) > 0
end
@doc """
Checks if an element with a given data-test attribute and value is present in the rendered component.
## Example
Let's say we have the following html code, and we want to check if the confirmation button
is present in the rendered component:
```html
<div data-test-confirmation-button="confirm">Confirm</div>
```
We can do so by calling the `we_see_attribute_and_value/3` function on the html of the rendered component:
```elixir
assert we_see_attribute_and_value(html, "confirmation button", "confirm")
```
## Arguments
html - the html of the rendered component
attribute - the data-test attribute of the element we want to check for
attribute_value - the data-test attribute value of the element we want to check for
"""
@spec we_see_attribute_and_value(String.t(), String.t(), String.t()) :: boolean()
def we_see_attribute_and_value(html, attribute, attribute_value) do
element_by_attribute_and_value(html, attribute, attribute_value)
|> length() > 0
end
@doc """
Similar to `we_see_attribute_and_value/3`, but also checks if the element has the given content.
## Example
Let's say we have the following html code, and we want to check if the confirmation button
is present in the rendered component:
```html
<div data-test-confirmation-button="confirm">Confirm</div>
```
We can do so by calling the `we_see_attribute_and_value/4` function on the html of the rendered component:
```elixir
assert we_see_attribute_and_value(html, "confirmation button", "confirm", content: "Confirm")
```
## Arguments
html - the html of the rendered component
attribute - the data-test attribute of the element we want to check for
attribute_value - the data-test attribute value of the element we want to check for
content - the content of the element we want to check for
"""
@spec we_see_attribute_and_value(String.t(), String.t(), String.t(), keyword()) :: boolean()
def we_see_attribute_and_value(html, attribute, attribute_value, content: content) do
element_by_attribute_and_value(html, attribute, attribute_value)
|> number_of_elements_with_content(content) > 0
end
@doc """
Checks if an element with a given data-test attribute value is present in the rendered component.
## Example
Let's say we have the following html code, and we want to check if the title is present in the rendered component:
```html
<div data-test="title">Title</div>
```
We can do so by calling the `we_see_attribute_value/2` function on the html of the rendered component:
```elixir
assert we_see_attribute_value(html, "title")
```
## Arguments
html - the html of the rendered component
attribute_value - the data-test attribute value of the element we want to check for
"""
@spec we_see_attribute_value(String.t(), String.t()) :: boolean()
def we_see_attribute_value(html, attribute_value) do
element_by_attribute_value(html, attribute_value)
|> length() > 0
end
@doc """
Similar to `we_see_attribute_value/2`, but also checks if the element has the given content.
## Example
Let's say we have the following html code, and we want to check if the title is present in the rendered component:
```html
<div data-test="title">Title</div>
```
We can do so by calling the `we_see_attribute_value/3` function on the html of the rendered component:
```elixir
assert we_see_attribute_value(html, "title", content: "Title")
```
## Arguments
html - the html of the rendered component
attribute_value - the data-test attribute value of the element we want to check for
content - the content of the element we want to check for
"""
@spec we_see_attribute_value(String.t(), String.t(), keyword()) :: boolean()
def we_see_attribute_value(html, attribute_value, content: content) do
element_by_attribute_value(html, attribute_value)
|> number_of_elements_with_content(content) > 0
end
@doc """
Checks if an element is present in the rendered component, n number of times
## Example
Let's say we have the following html code, and we want to check if buttons are present
in the rendered component:
```html
<div data-test="button">Button</div>
<div data-test="button">Button</div>
```
We can do so by calling the `we_see_exactly/3` function on the html of the rendered component:
```elixir
assert we_see_exactly(html, 2, attribute: "button")
```
## Arguments
html - the html of the rendered component
number - the number of times the element should be present
## Optional arguments
attribute - the data-test attribute of the element we want to check for
content - the content of the element we want to check for
attribute_value - the data-test attribute value of the element we want to check for
We can have any combination of the above optional arguments, but we must have at least
one attribute or attribute_value argument. For instance, lets look at the following
html code:
```html
<div data-test="button">Button</div>
<div data-test="button">Second Button</div>
<div data-test-cancellation>Third Button</div>
<div data-test-cancellation>Fourth Button</div>
<div data-test-optional="option">Fifth Button</div>
<div data-test-optional="option">Sixth Button</div>
```
We can check the number of buttons by calling the `we_see_exactly/2` function with
multiple optional arguments:
With attribute:
```elixir
assert we_see_exactly(html, 2, attribute: "cancellation")
```
With attribute_value:
```elixir
assert we_see_exactly(html, 2, attribute_value: "button")
```
With attribute and attribute_value:
```elixir
assert we_see_exactly(html, 2, attribute: "optional", attribute_value: "option")
```
With attribute and content:
```elixir
assert we_see_exactly(html, 1, attribute: "cancellation", content: "Third Button")
```
With attribute_value and content:
```elixir
assert we_see_exactly(html, 1, attribute_value: "button", content: "Second Button")
```
With attribute, attribute_value and content:
```elixir
assert we_see_exactly(html, 1, attribute: "optional", attribute_value: "option", content: "Fifth Button")
```
"""
@spec we_see_exactly(String.t(), number, keyword()) :: boolean()
def we_see_exactly(html, number, attribute: attribute) do
element_by_attribute(html, attribute)
|> length() == number
end
def we_see_exactly(html, number, attribute: attribute, content: content) do
element_by_attribute(html, attribute)
|> number_of_elements_with_content(content) == number
end
def we_see_exactly(html, number, attribute: attribute, attribute_value: attribute_value) do
element_by_attribute_and_value(html, attribute, attribute_value)
|> length() == number
end
def we_see_exactly(html, number,
attribute: attribute,
attribute_value: attribute_value,
content: content
) do
element_by_attribute_and_value(html, attribute, attribute_value)
|> number_of_elements_with_content(content) == number
end
def we_see_exactly(html, number, attribute_value: attribute_value) do
element_by_attribute_value(html, attribute_value)
|> length() == number
end
def we_see_exactly(html, number, attribute_value: attribute_value, content: content) do
element_by_attribute_value(html, attribute_value)
|> number_of_elements_with_content(content) == number
end
@doc """
Checks if an element is present in the rendered component, at least n number of times
This function is similar to `we_see_exactly/3`, but it checks if the element is present.
Please see the documentation for `we_see_exactly/3` for more information.
"""
@spec we_see_at_least(String.t(), number, keyword()) :: boolean()
def we_see_at_least(html, number, attribute: attribute) do
element_by_attribute(html, attribute)
|> length() >= number
end
def we_see_at_least(html, number, attribute: attribute, content: content) do
element_by_attribute(html, attribute)
|> number_of_elements_with_content(content) >= number
end
def we_see_at_least(html, number, attribute: attribute, attribute_value: attribute_value) do
element_by_attribute_and_value(html, attribute, attribute_value)
|> length() >= number
end
def we_see_at_least(html, number,
attribute: attribute,
attribute_value: attribute_value,
content: content
) do
element_by_attribute_and_value(html, attribute, attribute_value)
|> number_of_elements_with_content(content) >= number
end
def we_see_at_least(html, number, attribute_value: attribute_value) do
element_by_attribute_value(html, attribute_value)
|> length() >= number
end
def we_see_at_least(html, number, attribute_value: attribute_value, content: content) do
element_by_attribute_value(html, attribute_value)
|> number_of_elements_with_content(content) >= number
end
# helper functions
defp number_of_elements_with_content(list, content) do
Enum.reduce(list, 0, fn element, acc ->
if Floki.text(element) |> String.trim() == content do
acc + 1
else
acc
end
end)
end
defp element_by_attribute(html, attribute) do
html
|> Floki.parse_document!()
|> Floki.find("[data-test-#{format_attribute(attribute)}]")
end
defp element_by_attribute_and_value(html, attribute, attribute_value) do
html
|> Floki.parse_document!()
|> Floki.find("[data-test-#{format_attribute(attribute)}=\"#{attribute_value}\"]")
end
defp element_by_attribute_value(html, attribute_value) do
html
|> Floki.parse_document!()
|> Floki.find("[data-test=\"#{attribute_value}\"]")
end
defp format_attribute(attribute) do
String.replace(attribute, " ", "-")
end
end