|
1 | 1 | Manipulating Pages |
2 | 2 | ================== |
3 | 3 |
|
4 | | -Ok, you've got an interesting page element. Now you'll want to do something |
5 | | -with it. ``Behat\Mink\Element\NodeElement`` provides bunch a of useful methods |
6 | | -for you: |
| 4 | +Once you :doc:`get a page element </guides/traversing-pages>`, you will want |
| 5 | +to manipulate it. You can also interact with the page, which is covered in |
| 6 | +the :doc:`next chapter </guides/interacting-with-pages>`. |
| 7 | + |
| 8 | +Getting the tag name |
| 9 | +-------------------- |
| 10 | + |
| 11 | +The ``NodeElement::getTagName`` method allows you to get the tag name of |
| 12 | +the element. This tag name is always returned lowercased. |
7 | 13 |
|
8 | 14 | .. code-block:: php |
9 | 15 |
|
10 | 16 | $el = $page->find('css', '.something'); |
11 | 17 |
|
12 | 18 | // get tag name: |
13 | | - echo $el->getTagName(); |
| 19 | + echo $el->getTagName(); // displays 'a' |
14 | 20 |
|
15 | | - // check that element has href attribute: |
16 | | - $el->hasAttribute('href'); |
| 21 | +Accessing HTML attributes |
| 22 | +------------------------- |
17 | 23 |
|
18 | | - // get element's href attribute: |
19 | | - echo $el->getAttribute('href'); |
20 | | -
|
21 | | -Element Content and Text |
22 | | -~~~~~~~~~~~~~~~~~~~~~~~~ |
23 | | - |
24 | | -To retrieve HTML content or plain text from out of the element, you can use: |
25 | | - |
26 | | -.. code-block:: php |
| 24 | +The ``NodeElement`` class gives you access to HTML attributes of the element. |
27 | 25 |
|
28 | | - $plainText = $el->getText(); |
29 | | - $html = $el->getHtml(); |
30 | | -
|
31 | | -.. note:: |
32 | | - |
33 | | - ``getText()`` will strip tags and unprinted characters out of the response, |
34 | | - including newlines. So it'll basically return the text, that user sees |
35 | | - on the page. |
| 26 | +``NodeElement::hasAttribute`` |
| 27 | + Checks whether the element has a given attribute. |
36 | 28 |
|
37 | | -Form Field Manipulations |
38 | | -~~~~~~~~~~~~~~~~~~~~~~~~ |
| 29 | +``NodeElement::getAttribute`` |
| 30 | + Gets the value of an attribute. |
39 | 31 |
|
40 | | -You can fill form fields/retrieve its values with form manipulation actions: |
| 32 | +``NodeElement::hasClass`` |
| 33 | + Checks whether the element has the given class (convenience wrapper around |
| 34 | + ``getAttribute('class')``). |
41 | 35 |
|
42 | 36 | .. code-block:: php |
43 | 37 |
|
44 | | - // check/uncheck checkbox: |
45 | | - if ($el->isChecked()) { |
46 | | - $el->uncheck(); |
47 | | - } |
48 | | - $el->check(); |
49 | | -
|
50 | | - // select option in select: |
51 | | - $el->selectOption('option value'); |
| 38 | + $el = $page->find('css', '.something'); |
52 | 39 |
|
53 | | - // attach file to file input: |
54 | | - $el->attachFile('/path/to/file'); |
| 40 | + if ($el->hasAttribute('href') { |
| 41 | + echo $el->getAttribute('href'); |
| 42 | + } else { |
| 43 | + echo 'This anchor is not a link. It does not have an href.'; |
| 44 | + } |
55 | 45 |
|
56 | | - // get input value: |
57 | | - echo $el->getValue(); |
| 46 | +Element Content and Text |
| 47 | +------------------------ |
58 | 48 |
|
59 | | - // set intput value: |
60 | | - $el->setValue('some val'); |
| 49 | +The ``Element`` class provides access to the content of elements. |
61 | 50 |
|
62 | | - // press the button: |
63 | | - $el->press(); |
| 51 | +``Element::getHtml`` |
| 52 | + Gets the inner HTML of the element, i.e. all children of the element. |
64 | 53 |
|
| 54 | +``Element::getOuterHtml`` |
| 55 | + Gets the outer HTML of the element, i.e. including the element itself. |
65 | 56 |
|
66 | | -Mouse Manipulations |
67 | | -~~~~~~~~~~~~~~~~~~~ |
| 57 | +``Element::getText`` |
| 58 | + Gets the text of the element. |
68 | 59 |
|
69 | | -You can perform mouse manipulations on an element: |
| 60 | +.. note:: |
70 | 61 |
|
71 | | -.. code-block:: php |
| 62 | + ``getText()`` will strip tags and unprinted characters out of the response, |
| 63 | + including newlines. So it'll basically return the text, that user sees |
| 64 | + on the page. |
72 | 65 |
|
73 | | - $el->click(); |
74 | | - $el->doubleClick(); |
75 | | - $el->rightClick(); |
76 | | - $el->mouseOver(); |
77 | | - $el->focus(); |
78 | | - $el->blur(); |
| 66 | +Checking Element Visibility |
| 67 | +--------------------------- |
79 | 68 |
|
80 | | -.. note:: |
| 69 | +The ``NodeElement::isVisible`` methods allows to checks whether the element |
| 70 | +is visible. |
81 | 71 |
|
82 | | - All methods except ``click()`` are not supported by ``Driver\GoutteDriver``, |
83 | | - because there is no way how it can perform them without actual browser |
84 | | - window. |
| 72 | +Accessing Form State |
| 73 | +-------------------- |
85 | 74 |
|
86 | | -Drag'n'Drop |
87 | | -~~~~~~~~~~~ |
| 75 | +The ``NodeElement`` class allows to access the state of form elements: |
88 | 76 |
|
89 | | -Mink even supports drag'n'drop of one field onto another: |
| 77 | +``NodeElement::getValue`` |
| 78 | + Gets the value of the element. See :ref:`interacting-with-forms`. |
90 | 79 |
|
91 | | -.. code-block:: php |
| 80 | +``NodeElement::isChecked`` |
| 81 | + Checks whether the checkbox or radio button is checked. |
92 | 82 |
|
93 | | - $el1 = $page->find(...); |
94 | | - $el2 = $page->find(...); |
| 83 | +``NodeElement::isSelected`` |
| 84 | + Checks whether the ``<option>`` element is selected. |
95 | 85 |
|
96 | | - $el1->dragTo($el2); |
| 86 | +Shortcut methods |
| 87 | +~~~~~~~~~~~~~~~~ |
97 | 88 |
|
98 | | -.. note:: |
| 89 | +The ``TraversableElement`` class provides a few shortcut methods allowing |
| 90 | +to find a child element in the page and checks the state of it immediately: |
99 | 91 |
|
100 | | - Drag'n'drop is not supported by ``Driver\GoutteDriver``, because there |
101 | | - is no way how it can perform this action without actual browser window. |
| 92 | +``TraversableElement::hasCheckedField`` |
| 93 | + Looks for a checkbox (see findField) and checks whether it is checked. |
102 | 94 |
|
| 95 | +``TraversableElement::hasUncheckedField`` |
| 96 | + Looks for a checkbox (see findField) and checks whether it is not checked. |
0 commit comments