-
Notifications
You must be signed in to change notification settings - Fork 17
/
assertions.ex
167 lines (129 loc) · 3.94 KB
/
assertions.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
defmodule PhoenixTest.Assertions do
@moduledoc false
import ExUnit.Assertions
alias ExUnit.AssertionError
alias PhoenixTest.Html
alias PhoenixTest.Query
@doc """
Asserts that the rendered HTML content within the given session contains an
element matching the specified selector and (optional) text.
## Parameters
- `session`: The test session.
- `selector`: The CSS selector to search for.
- `text` (optional): The expected text content of the element.
## Raises
Raises `AssertionError` if no element is found with the given selector and text.
"""
def assert_has(session, "title") do
title = PhoenixTest.Driver.render_page_title(session)
if is_nil(title) || title == "" do
raise AssertionError,
message: """
Expected title to be present but could not find it.
"""
else
assert true
end
session
end
def assert_has(session, selector) do
session
|> PhoenixTest.Driver.render_html()
|> Query.find(selector)
|> case do
{:found, _found} ->
assert true
{:found_many, _found} ->
assert true
:not_found ->
raise AssertionError,
message: """
Could not find any elements with selector #{inspect(selector)}.
"""
end
session
end
def assert_has(session, "title", text) do
title = PhoenixTest.Driver.render_page_title(session)
if title == text do
assert true
else
raise AssertionError,
message: """
Expected title to be #{inspect(text)} but got #{inspect(title)}
"""
end
session
end
def assert_has(session, selector, text) do
session
|> PhoenixTest.Driver.render_html()
|> Query.find(selector, text)
|> case do
{:found, _found} ->
assert true
{:found_many, _found} ->
assert true
{:not_found, []} ->
raise AssertionError,
message: """
Could not find any elements with selector #{inspect(selector)}.
"""
{:not_found, elements_matched_selector} ->
raise AssertionError,
message: """
Could not find element with text #{inspect(text)}.
Found other elements matching the selector #{inspect(selector)}:
#{format_found_elements(elements_matched_selector)}
"""
end
session
end
@doc """
Asserts that the rendered HTML within the given session does not contain an element matching the specified selector and text.
## Parameters
- `session`: The test session.
- `selector`: The CSS selector for the element.
- `text`: The text that the element is expected not to contain.
## Raises
Raises `AssertionError` if an element matching the selector and text is found.
"""
def refute_has(session, "title", text) do
title = PhoenixTest.Driver.render_page_title(session)
if title == text do
raise AssertionError,
message: """
Expected title not to be #{inspect(text)}
"""
else
refute false
end
end
def refute_has(session, selector, text) do
session
|> PhoenixTest.Driver.render_html()
|> Query.find(selector, text)
|> case do
{:not_found, _} ->
refute false
{:found, element} ->
raise AssertionError,
message: """
Expected not to find an element.
But found an element with selector #{inspect(selector)} and text #{inspect(text)}:
#{format_found_elements(element)}
"""
{:found_many, elements} ->
raise AssertionError,
message: """
Expected not to find an element.
But found #{Enum.count(elements)} elements with selector #{inspect(selector)} and text #{inspect(text)}:
"""
end
session
end
defp format_found_elements(elements) when is_list(elements) do
Enum.map_join(elements, "\n", &Html.raw/1)
end
defp format_found_elements(element), do: format_found_elements([element])
end