Class: Sandals::TestSession
- Inherits:
-
Object
- Object
- Sandals::TestSession
show all
- Defined in:
- lib/sandals/test_session.rb
Defined Under Namespace
Classes: AssertionError, FieldLocator
Constant Summary
collapse
- FIELD_TYPES =
[
TextField,
NumberField,
TextArea,
Checkbox,
Select,
Radio,
FileField
].freeze
- FILLABLE_TYPES =
[TextField, NumberField, TextArea].freeze
Instance Method Summary
collapse
Constructor Details
#initialize(application) ⇒ TestSession
Returns a new instance of TestSession.
59
60
61
62
|
# File 'lib/sandals/test_session.rb', line 59
def initialize(application)
@application = application
use_snapshot(@application.__send__(:publish_view))
end
|
Instance Method Details
#assert_error(expected) ⇒ Object
185
186
187
|
# File 'lib/sandals/test_session.rb', line 185
def assert_error(expected)
assert_message(Error, "error", expected)
end
|
#assert_field_error(label_or_locator, expected) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/sandals/test_session.rb', line 203
def assert_field_error(label_or_locator, expected)
actual = field_error(label_or_locator)
return self if actual == expected.to_s
label =
if label_or_locator.is_a?(FieldLocator)
label_or_locator.label
else
label_or_locator
end
raise AssertionError,
"expected field \"#{label}\" to have error \"#{expected}\", " \
"but its error was #{actual.inspect}"
end
|
#assert_notice(expected) ⇒ Object
177
178
179
|
# File 'lib/sandals/test_session.rb', line 177
def assert_notice(expected)
assert_message(Notice, "notice", expected)
end
|
#assert_text(expected) ⇒ Object
151
152
153
154
155
156
|
# File 'lib/sandals/test_session.rb', line 151
def assert_text(expected)
return self if text?(expected)
raise AssertionError,
%(expected the view to include "#{expected}", but it rendered:\n#{visible_text})
end
|
#attach(label, file:) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/sandals/test_session.rb', line 103
def attach(label, file:)
control = find_field(label)
unless control.is_a?(FileField)
raise AssertionError, %(field "#{label}" is not a file field)
end
uploaded_file =
if file.is_a?(UploadedFile)
file
else
path = File.expand_path(file.to_s)
UploadedFile.new(
name: File.basename(path),
content_type: content_type_for(path),
content: File.binread(path)
)
end
dispatch(control, "change", "value" => uploaded_file)
self
end
|
#check(label) ⇒ Object
125
126
127
|
# File 'lib/sandals/test_session.rb', line 125
def check(label)
change_checkbox(label, true)
end
|
#choose(option, from:) ⇒ Object
142
143
144
145
146
147
148
149
|
# File 'lib/sandals/test_session.rb', line 142
def choose(option, from:)
control = find_field(from)
unless control.is_a?(Radio)
raise AssertionError, %(field "#{from}" is not a radio)
end
change_option(control, option)
end
|
#click(content) ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/sandals/test_session.rb', line 92
def click(content)
button = find_one(
controls.select do |control|
control.is_a?(Button) && control.content == content.to_s
end,
%(button named "#{content}")
)
dispatch(button, button.is_a?(Submit) ? "submit" : "click")
self
end
|
#error?(expected) ⇒ Boolean
173
174
175
|
# File 'lib/sandals/test_session.rb', line 173
def error?(expected)
messages(Error).include?(expected.to_s)
end
|
#field(label) ⇒ Object
64
65
66
67
|
# File 'lib/sandals/test_session.rb', line 64
def field(label)
find_field(label)
FieldLocator.new(self, label)
end
|
#field_error(label_or_locator) ⇒ Object
193
194
195
196
197
198
199
200
201
|
# File 'lib/sandals/test_session.rb', line 193
def field_error(label_or_locator)
label =
if label_or_locator.is_a?(FieldLocator)
label_or_locator.label
else
label_or_locator
end
find_field(label).error
end
|
#field_value(label_or_locator) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/sandals/test_session.rb', line 79
def field_value(label_or_locator)
label =
if label_or_locator.is_a?(FieldLocator)
label_or_locator.label
else
label_or_locator
end
control = find_field(label)
return control.checked? if control.is_a?(Checkbox)
control.value
end
|
#fill(label, with:) ⇒ Object
69
70
71
72
73
74
75
76
77
|
# File 'lib/sandals/test_session.rb', line 69
def fill(label, with:)
control = find_field(label)
unless FILLABLE_TYPES.any? { |type| control.is_a?(type) }
raise AssertionError, %(field "#{label}" cannot be filled)
end
dispatch(control, "change", "value" => serialize(with))
self
end
|
#notice?(expected) ⇒ Boolean
169
170
171
|
# File 'lib/sandals/test_session.rb', line 169
def notice?(expected)
messages(Notice).include?(expected.to_s)
end
|
#refute_error(unexpected) ⇒ Object
189
190
191
|
# File 'lib/sandals/test_session.rb', line 189
def refute_error(unexpected)
refute_message(Error, "error", unexpected)
end
|
#refute_notice(unexpected) ⇒ Object
181
182
183
|
# File 'lib/sandals/test_session.rb', line 181
def refute_notice(unexpected)
refute_message(Notice, "notice", unexpected)
end
|
#refute_text(unexpected) ⇒ Object
158
159
160
161
162
163
|
# File 'lib/sandals/test_session.rb', line 158
def refute_text(unexpected)
return self unless text?(unexpected)
raise AssertionError,
%(expected the view not to include "#{unexpected}", but it rendered:\n#{visible_text})
end
|
#select(option, from:) ⇒ Object
133
134
135
136
137
138
139
140
|
# File 'lib/sandals/test_session.rb', line 133
def select(option, from:)
control = find_field(from)
unless control.instance_of?(Select)
raise AssertionError, %(field "#{from}" is not a select)
end
change_option(control, option)
end
|
#text?(expected) ⇒ Boolean
165
166
167
|
# File 'lib/sandals/test_session.rb', line 165
def text?(expected)
visible_text.include?(expected.to_s)
end
|
#uncheck(label) ⇒ Object
129
130
131
|
# File 'lib/sandals/test_session.rb', line 129
def uncheck(label)
change_checkbox(label, false)
end
|