|
| 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'); |
0 commit comments