Skip to content

Commit 03eca7e

Browse files
committed
Document the assertion system
1 parent e7f9322 commit 03eca7e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

guides/assertion-system.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Assertion system
2+
================
3+
4+
The ``\Behat\Mink\WebAssert`` class provides a set of assertions. There are assertions
5+
about the address of the page, the cookies, the status code, the response headers,
6+
the content of the page, the page elements...
7+
8+
API with exceptions
9+
-------------------
10+
11+
An assertion can improve code readability and avoid having a fatal error in method chaining.
12+
13+
For example, instead of:
14+
15+
.. code-block:: php
16+
17+
$registerForm = $page->find('css', 'form.register');
18+
19+
if (null === $registerForm) {
20+
throw new \Exception('The element is not found');
21+
}
22+
23+
$field = $registerForm->findField('Email');
24+
25+
if (null === $field) {
26+
throw new \Exception('The element is not found');
27+
}
28+
29+
$field->setValue('foo@example.com');
30+
31+
you can do:
32+
33+
.. code-block:: php
34+
35+
// Throw exception when not found instead of returning null.
36+
$registerForm = $mink->assertSession()->elementExists('css', 'form.register');
37+
38+
$field = $mink->assertSession()->fieldExists('Email', $registerForm);
39+
40+
$field->setValue('foo@example.com');
41+
42+
43+
Webassert and multisessions
44+
---------------------------
45+
46+
You could use an another session by calling ``assertSession`` with a session name
47+
or an instance of ``\Behat\Mink\Session``.
48+
49+
.. code-block:: php
50+
51+
$mink->assertSession()->elementExists('css', 'form.register');
52+
$mink->assertSession('goutte')->elementExists('css', 'form.register');
53+
$mink->assertSession($mink->getSession('goutte'))->elementExists('css', 'form.register');

index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Learn Mink with the topical guides:
7474
guides/interacting-with-pages
7575
guides/drivers
7676
guides/managing-sessions
77+
guides/assertion-system
7778
contributing
7879

7980
Testing Tools Integration

0 commit comments

Comments
 (0)