@@ -5,12 +5,325 @@ The ``\Behat\Mink\WebAssert`` class provides a set of assertions. There are asse
5
5
about the address of the page, the cookies, the status code, the response headers,
6
6
the content of the page, the page elements...
7
7
8
- API with exceptions
8
+ .. note ::
9
+
10
+ If an assertion evaluates to false, an exception ``Behat\Mink\Exception\ExpectationException ``
11
+ is thrown.
12
+
13
+ Mink initialisation
9
14
-------------------
10
15
11
- An assertion can improve code readability and avoid having a fatal error in method chaining.
16
+ .. code-block :: php
17
+
18
+ // Choose a Mink driver.
19
+ $driver = new \Behat\Mink\Driver\GoutteDriver();
20
+ $session = new \Behat\Mink\Session($driver);
21
+ $mink = new \Behat\Mink\Mink(array('goutte' => $session));
22
+ $mink->setDefaultSessionName('goutte');
23
+
24
+ Checking address
25
+ ----------------
26
+
27
+ ``WebAssert::addressEquals ``
28
+ Checks that current session address is equals to provided one.
29
+
30
+ ``WebAssert::addressNotEquals ``
31
+ Checks that current session address is not equals to provided one.
32
+
33
+ .. code-block :: php
34
+
35
+ $mink->getSession()->visit('http://exemple.com');
36
+
37
+ $mink->assertSession()->addressEquals('http://example.com/');
38
+ $mink->assertSession()->addressEquals('/');
39
+
40
+ $mink->assertSession()->addressNotEquals('http://example.com/not_found');
41
+ $mink->assertSession()->addressNotEquals('/not_found');
42
+
43
+ ``WebAssert::addressMatches ``
44
+ Checks that current session address matches regex.
45
+
46
+ .. code-block :: php
47
+
48
+ $mink->getSession()->visit('http://example.com/script.php/sub/url/foo/bar');
49
+
50
+ $mink->assertSession()->addressMatches('/sub.*bar/');
51
+
52
+ .. caution ::
53
+ Webassert compare only the path and the fragment (after the hashmark #). The scheme,
54
+ the host, the query (after the question mark ?) are not taken into account.
55
+
56
+ These examples didn't throw exception:
57
+
58
+ .. code-block :: php
59
+
60
+ $mink->getSession()->visit('http://example.com');
61
+ // different scheme
62
+ $mink->assertSession()->addressEquals('https://example.com');
63
+ // different host
64
+ $mink->assertSession()->addressEquals('https://another.com');
65
+
66
+
67
+ $mink->getSession()->visit('http://example.com/script.php/sub/url?param=true#webapp/nav');
68
+ // Without query string
69
+ $mink->assertSession()->addressEquals('http://examyyyyyple.com/sub/url#webapp/nav');
70
+
71
+ Checking cookie
72
+ ---------------
73
+
74
+ ``WebAssert::cookieExists ``
75
+ Checks that specified cookie exists.
76
+
77
+ ``WebAssert::cookieEquals ``
78
+ Checks that specified cookie exists and its value.
79
+
80
+ .. code-block :: php
81
+
82
+ $mink->assertSession()->cookieExists('cookie_name');
83
+ $mink->assertSession()->cookieEquals('cookie_name', 'foo_value');
84
+
85
+ Checking status code
86
+ --------------------
87
+
88
+ ``WebAssert::statusCodeEquals ``
89
+ Checks that current response code equals to provided one.
90
+
91
+ ``WebAssert::statusCodeNotEquals ``
92
+ Checks that current response code not equals to provided one.
93
+
94
+ .. code-block :: php
95
+
96
+ $mink->assertSession()->statusCodeEquals(200);
97
+ $mink->assertSession()->statusCodeNotEquals(500);
98
+
99
+ .. note ::
100
+
101
+ See the :ref: `driver-feature-support ` to see which driver supports this feature.
102
+
103
+ Checking response headers
104
+ -------------------------
105
+
106
+ ``WebAssert::responseHeaderEquals ``
107
+ Checks that current response header equals value.
108
+
109
+ ``WebAssert::responseHeaderNotEquals ``
110
+ Checks that current response header does not equal value.
111
+
112
+ ``WebAssert::responseHeaderContains ``
113
+ Checks that current response header contains value.
114
+
115
+ ``WebAssert::responseHeaderNotContains ``
116
+ Checks that current response header does not contain value.
117
+
118
+ .. code-block :: php
119
+
120
+ $mink->assertSession()->responseHeaderEquals('Content-Type', 'text/html;charset=utf-8');
121
+ $mink->assertSession()->responseHeaderNotEquals('Content-Type', 'application/json');
122
+ $mink->assertSession()->responseHeaderContains('Content-Type', 'charset=utf-8');
123
+ $mink->assertSession()->responseHeaderNotContains('Content-Type', 'application/json');
124
+
125
+ ``WebAssert::responseHeaderMatches ``
126
+ Checks that current response header matches regex.
127
+
128
+ ``WebAssert::responseHeaderNotMatches ``
129
+ Checks that current response header does not match regex.
130
+
131
+ .. code-block :: php
132
+
133
+ $mink->assertSession()->responseHeaderMatches('Content-Type', '/text.*charset.*/');
134
+ $mink->assertSession()->responseHeaderNotMatches('Content-Type', '/application.*charset.*/');
135
+
136
+ .. note ::
137
+
138
+ See the :ref: `driver-feature-support ` to see which driver supports this feature.
139
+
140
+ Checking response text content
141
+ ------------------------------
142
+
143
+ WebAssert can checks the text content of current page. The comparison is case-insensitive.
144
+
145
+ ``WebAssert::pageTextContains ``
146
+ Checks that current page contains text.
147
+
148
+ ``WebAssert::pageTextNotContains ``
149
+ Checks that current page does not contains text.
150
+
151
+ .. code-block :: php
152
+
153
+ $mink->assertSession()->pageTextContains('Example');
154
+ $mink->assertSession()->pageTextNotContains('Examplefoobar');
155
+
156
+ ``WebAssert::pageTextMatches ``
157
+ Checks that current page text matches regex.
158
+
159
+ ``WebAssert::pageTextNotMatches ``
160
+ Checks that current page text does not matches regex.
161
+
162
+ .. code-block :: php
163
+
164
+ $mink->assertSession()->pageTextMatches('/Example/');
165
+ $mink->assertSession()->pageTextNotMatches('/Examplefoobar/');
166
+
167
+ Checking response HTML content
168
+ ------------------------------
169
+
170
+ WebAssert can checks the HTML content of current page. The comparison is case-insensitive.
171
+
172
+ ``WebAssert::responseContains ``
173
+ Checks that page HTML (response content) contains text.
174
+
175
+ ``WebAssert::responseNotContains ``
176
+ Checks that page HTML (response content) does not contains text.
177
+
178
+ .. code-block :: php
179
+
180
+ $mink->assertSession()->responseContains('<h1 >Example Domain</h1 >');
181
+ $mink->assertSession()->responseNotContains('<h2 >Example Domain </h2 >');
182
+
183
+ ``WebAssert::responseMatches ``
184
+ Checks that page HTML (response content) matches regex.
185
+
186
+ ``WebAssert::responseNotMatches ``
187
+ Checks that page HTML (response content) does not matches regex.
188
+
189
+ .. code-block :: php
190
+
191
+ $mink->assertSession()->responseMatches('/<h1 >Example.*<\/h1>/');
192
+ $mink->assertSession()->responseNotMatches('/<h1 >ExampleFooBar.*<\/h1>/');
193
+
194
+ Checking elements
195
+ -----------------
196
+
197
+ ``WebAssert::element* `` supports same :ref: `selectors <selectors >` that the
198
+ ``ElementInterface::find `` and ``ElementInterface::findAll `` methods.
199
+
200
+ ``WebAssert::elementsCount ``
201
+ Checks that there is specified number of specific elements on the page.
202
+
203
+ .. code-block :: php
204
+
205
+ $mink->assertSession()->elementsCount('css', 'h1', 1);
206
+
207
+ ``WebAssert::elementExists ``
208
+ Checks that specific element exists on the current page.
209
+
210
+ .. code-block :: php
211
+
212
+ $titleNodeElement = $mink->assertSession()->elementExists('css', 'h1');
213
+
214
+ ``WebAssert::elementNotExists ``
215
+ Checks that specific element does not exists on the current page.
216
+
217
+ .. code-block :: php
218
+
219
+ $mink->assertSession()->elementNotExists('css', 'h5');
220
+
221
+ ``WebAssert::elementTextContains ``
222
+ Checks that specific element contains text. The comparison is case-insensitive.
223
+
224
+ .. code-block :: php
225
+
226
+ $mink->assertSession()->elementTextContains('css', 'h1', 'Example Domain');
227
+
228
+ ``WebAssert::elementTextNotContains ``
229
+ Checks that specific element does not contains text. The comparison is case-insensitive.
230
+
231
+ .. code-block :: php
232
+
233
+ $mink->assertSession()->elementTextNotContains('css', 'h1', 'ExampleFooBar');
234
+
235
+ ``WebAssert::elementContains ``
236
+ Checks that specific element contains HTML. The comparison is case-insensitive.
237
+
238
+ .. code-block :: php
239
+
240
+ $mink->assertSession()->elementContains('css', 'div', '<h1 >Example Domain</h1 >');
241
+
242
+ ``WebAssert::elementNotContains ``
243
+ Checks that specific element does not contains HTML. The comparison is case-insensitive.
244
+
245
+ .. code-block :: php
246
+
247
+ $mink->assertSession()->elementNotContains('css', 'div', '<h1 >ExampleFooBar</h1 >');
248
+
249
+ ``Webassert::elementAttributeExists ``
250
+ Checks that an attribute exists in an element.
251
+
252
+ .. code-block :: php
253
+
254
+ $mink->assertSession()->elementAttributeExists('css', 'a', 'href');
255
+
256
+ ``Webassert::elementAttributeContains ``
257
+ Checks that an attribute of a specific elements contains text. The comparison is case-insensitive.
258
+
259
+ .. code-block :: php
260
+
261
+ $mink->assertSession()->elementAttributeContains('css', 'a', 'href', 'http://');
262
+
263
+ ``Webassert::elementAttributeNotContains ``
264
+ Checks that an attribute of a specific elements does not contain text.
265
+
266
+ .. code-block :: php
267
+
268
+ $mink->assertSession()->elementAttributeNotContains('css', 'a', 'href', 'https://');
269
+
270
+ All ``Webassert::field* `` use the :ref: `field named selector <named-selector >`.
271
+
272
+ ``Webassert::fieldExists ``
273
+ Checks that specific field exists on the current page.
274
+
275
+ .. code-block :: php
276
+
277
+ $mink->assertSession()->fieldExists('username');
278
+
279
+ ``Webassert::fieldNotExists ``
280
+ Checks that specific field does not exists on the current page.
281
+
282
+ .. code-block :: php
283
+
284
+ $mink->assertSession()->fieldNotExists('not_exists');
285
+
286
+ ``Webassert::fieldValueEquals ``
287
+ Checks that specific field have provided value. The comparison is case-insensitive.
288
+
289
+ .. code-block :: php
290
+
291
+ $mink->assertSession()->fieldValueEquals('username', 'foo');
292
+
293
+ ``Webassert::fieldValueNotEquals ``
294
+ Checks that specific field have provided value. The comparison is case-insensitive.
295
+
296
+ .. code-block :: php
297
+
298
+ $mink->assertSession()->fieldValueEquals('username', 'foo');
299
+
300
+ All ``Webassert::checkbox* `` use the :ref: `checkbox named selector <named-selector >`.
301
+
302
+ ``Webassert::checkboxChecked ``
303
+ Checks that specific checkbox is checked.
304
+
305
+ .. code-block :: php
306
+
307
+ $mink->assertSession()->checkboxChecked('remember_me');
308
+
309
+ ``Webassert::checkboxNotChecked ``
310
+ Checks that specific checkbox is unchecked.
311
+
312
+ .. code-block :: php
313
+
314
+ $mink->assertSession()->checkboxNotChecked('remember_me');
315
+
316
+ Nested Traversing
317
+ -----------------
318
+
319
+ Every ``WebAssert::*Exists``method return a ``Behat\Mink\Element\NodeElement ``.
320
+ (Except of course ``WebAssert::*NotExists``methods.).
321
+
322
+ ``WebAssert::elementsCount ``, ``WebAssert::elementExists ``, ``WebAssert::elementNotExists ``,
323
+ ``Webassert::field* ``, ``Webassert::checkbox*``methods support an ``ElementInterface ``
324
+ argument in order to not search on all the page but only in an element of the page.
12
325
13
- For example , instead of:
326
+ So , instead of that :
14
327
15
328
.. code-block :: php
16
329
@@ -39,6 +352,7 @@ you can do:
39
352
40
353
$field->setValue('foo@example.com');
41
354
355
+ This can improve code readability or avoid having a fatal error in method chaining.
42
356
43
357
Webassert and multisessions
44
358
---------------------------
0 commit comments