Skip to content

Commit ede300a

Browse files
javiereguiluzLiinkiing
authored andcommitted
Added docs for clickLink() and submitForm()
1 parent d3216ce commit ede300a

File tree

2 files changed

+89
-54
lines changed

2 files changed

+89
-54
lines changed

components/browser_kit.rst

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,77 @@ make AJAX requests::
9797
Clicking Links
9898
~~~~~~~~~~~~~~
9999

100-
The ``Crawler`` object is capable of simulating link clicks. First, pass the
101-
text content of the link to the ``selectLink()`` method, which returns a
102-
``Link`` object. Then, pass this object to the ``click()`` method, which
103-
performs the needed HTTP GET request to simulate the link click::
100+
The ``Client`` object is capable of simulating link clicks. Pass the text
101+
content of the link and the client will perform the needed HTTP GET request to
102+
simulate the link click::
104103

105104
use Acme\Client;
106105

107106
$client = new Client();
107+
$client->request('GET', '/product/123');
108+
109+
$crawler = $client->clickLink('Go elsewhere...');
110+
111+
.. versionadded:: 4.2
112+
The ``clickLink()`` method was introduced in Symfony 4.2.
113+
114+
If you need the :class:`Symfony\\Component\\DomCrawler\\Link` object that
115+
provides access to the link properties (e.g. ``$link->getMethod()``,
116+
``$link->getUri()``), use this other method:
117+
118+
// ...
108119
$crawler = $client->request('GET', '/product/123');
109120
$link = $crawler->selectLink('Go elsewhere...')->link();
110121
$client->click($link);
111122

112123
Submitting Forms
113124
~~~~~~~~~~~~~~~~
114125

115-
The ``Crawler`` object is also capable of selecting forms. First, select any of
116-
the form's buttons with the ``selectButton()`` method. Then, use the ``form()``
117-
method to select the form which the button belongs to.
118-
119-
After selecting the form, fill in its data and send it using the ``submit()``
120-
method (which makes the needed HTTP POST request to submit the form contents)::
126+
The ``Client`` object is also capable of submitting forms. First, select the
127+
form using any of its buttons and then override any of its properties (method,
128+
field values, etc.) before submitting it::
121129

122130
use Acme\Client;
123131

124-
// make a real request to an external site
125132
$client = new Client();
126133
$crawler = $client->request('GET', 'https://github.com/login');
127134

135+
// find the form with the 'Log in' button and submit it
136+
// 'Log in' can be the text content, id, value or name of a <button> or <input type="submit">
137+
$client->submitForm('Log in');
138+
139+
// the second optional argument lets you override the default form field values
140+
$client->submitForm('Log in', array(
141+
'login' => 'my_user',
142+
'password' => 'my_pass',
143+
// to upload a file, the value must be the absolute file path
144+
'file' => __FILE__,
145+
));
146+
147+
// you can override other form options too
148+
$client->submitForm(
149+
'Log in',
150+
array('login' => 'my_user', 'password' => 'my_pass'),
151+
// override the default form HTTP method
152+
'PUT',
153+
// override some $_SERVER parameters (e.g. HTTP headers)
154+
array('HTTP_ACCEPT_LANGUAGE' => 'es')
155+
);
156+
157+
.. versionadded:: 4.2
158+
The ``submitForm()`` method was introduced in Symfony 4.2.
159+
160+
If you need the :class:`Symfony\\Component\\DomCrawler\\Form` object that
161+
provides access to the form properties (e.g. ``$form->getUri()``,
162+
``$form->getValues()``, ``$form->getFields()``), use this other method::
163+
164+
// ...
165+
128166
// select the form and fill in some values
129167
$form = $crawler->selectButton('Log in')->form();
130168
$form['login'] = 'symfonyfan';
131169
$form['password'] = 'anypass';
132170

133-
// To upload a file, the value should be the absolute file path
134-
$form['file'] = __FILE__;
135-
136171
// submit that form
137172
$crawler = $client->submit($form);
138173

testing.rst

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -422,21 +422,17 @@ returns a ``Crawler`` instance.
422422
Use the crawler to find DOM elements in the response. These elements can then
423423
be used to click on links and submit forms::
424424

425-
$link = $crawler->selectLink('Go elsewhere...')->link();
426-
$crawler = $client->click($link);
425+
$crawler = $client->clickLink('Go elsewhere...');
427426

428-
$form = $crawler->selectButton('validate')->form();
429-
$crawler = $client->submit($form, array('name' => 'Fabien'));
427+
$crawler = $client->submitForm('validate', array('name' => 'Fabien'));
430428

431-
The ``click()`` and ``submit()`` methods both return a ``Crawler`` object.
429+
The ``clickLink()`` and ``submitForm()`` methods both return a ``Crawler`` object.
432430
These methods are the best way to browse your application as it takes care
433431
of a lot of things for you, like detecting the HTTP method from a form and
434432
giving you a nice API for uploading files.
435433

436-
.. tip::
437-
438-
You will learn more about the ``Link`` and ``Form`` objects in the
439-
:ref:`Crawler <testing-crawler>` section below.
434+
.. versionadded:: 4.2
435+
The ``clickLink()`` and ``submitForm()`` methods were introduced in Symfony 4.2.
440436

441437
The ``request()`` method can also be used to simulate form submissions directly
442438
or perform more complex requests. Some useful examples::
@@ -715,65 +711,68 @@ The Crawler can extract information from the nodes::
715711
Links
716712
~~~~~
717713

718-
To select links, you can use the traversing methods above or the convenient
719-
``selectLink()`` shortcut::
714+
Use the ``clickLink()`` method to click on the first link that contains the
715+
given text (or the first clickable image with that ``alt`` attribute)::
720716

721-
$crawler->selectLink('Click here');
717+
$client = static::createClient();
718+
$client->request('GET', '/post/hello-world');
722719

723-
This selects all links that contain the given text, or clickable images for
724-
which the ``alt`` attribute contains the given text. Like the other filtering
725-
methods, this returns another ``Crawler`` object.
720+
$client->clickLink('Click here');
726721

727-
Once you've selected a link, you have access to a special ``Link`` object,
728-
which has helpful methods specific to links (such as ``getMethod()`` and
729-
``getUri()``). To click on the link, use the Client's ``click()`` method
730-
and pass it a ``Link`` object::
722+
If you need access to the :class:`Symfony\\Component\\DomCrawler\\Link` object
723+
that provides helpful methods specific to links (such as ``getMethod()`` and
724+
``getUri()``), use the ``selectLink()`` method instead:
731725

732-
$link = $crawler->selectLink('Click here')->link();
726+
$client = static::createClient();
727+
$crawler = $client->request('GET', '/post/hello-world');
733728

729+
$link = $crawler->selectLink('Click here')->link();
734730
$client->click($link);
735731

736732
Forms
737733
~~~~~
738734

739-
Forms can be selected using their buttons, which can be selected with the
740-
``selectButton()`` method, just like links::
735+
Use the ``submitForm()`` method to submit the form that contains the given button::
741736

742-
$buttonCrawlerNode = $crawler->selectButton('submit');
737+
$client = static::createClient();
738+
$client->request('GET', '/post/hello-world');
739+
740+
$crawler = $client->submitForm('Add comment', array(
741+
'comment_form[content]' => '...',
742+
));
743+
744+
The first argument of ``submitForm()`` is the text content, ``id``, ``value`` or
745+
``name`` of any ``<button>`` or ``<input type="submit">`` included in the form.
746+
The second optional argument is used to override the default form field values.
743747

744748
.. note::
745749

746750
Notice that you select form buttons and not forms as a form can have several
747751
buttons; if you use the traversing API, keep in mind that you must look for a
748752
button.
749753

750-
The ``selectButton()`` method can select ``button`` tags and submit ``input``
751-
tags. It uses several parts of the buttons to find them:
754+
If you need access to the :class:`Symfony\\Component\\DomCrawler\\Form` object
755+
that provides helpful methods specific to forms (such as ``getUri()``,
756+
``getValues()`` and ``getFields()``) use the ``selectButton()`` method instead::
752757

753-
* The ``value`` attribute value;
754-
* The ``id`` or ``alt`` attribute value for images;
755-
* The ``id`` or ``name`` attribute value for ``button`` tags.
758+
$client = static::createClient();
759+
$crawler = $client->request('GET', '/post/hello-world');
756760

757-
Once you have a Crawler representing a button, call the ``form()`` method
758-
to get a ``Form`` instance for the form wrapping the button node::
761+
$buttonCrawlerNode = $crawler->selectButton('submit');
759762

763+
// select the form that contains this button
760764
$form = $buttonCrawlerNode->form();
761765

762-
When calling the ``form()`` method, you can also pass an array of field values
763-
that overrides the default ones::
764-
766+
// you can also pass an array of field values that overrides the default ones
765767
$form = $buttonCrawlerNode->form(array(
766768
'my_form[name]' => 'Fabien',
767769
'my_form[subject]' => 'Symfony rocks!',
768770
));
769771

770-
And if you want to simulate a specific HTTP method for the form, pass it as a
771-
second argument::
772-
772+
// you can pass a second argument to override the form HTTP method
773773
$form = $buttonCrawlerNode->form(array(), 'DELETE');
774774

775-
The Client can submit ``Form`` instances::
776-
775+
// submit the Form object
777776
$client->submit($form);
778777

779778
The field values can also be passed as a second argument of the ``submit()``
@@ -819,10 +818,11 @@ their type::
819818

820819
.. tip::
821820

822-
The ``submit()`` method defines a third optional argument to add custom
823-
HTTP headers when submitting the form::
821+
The ``submit()`` and ``submitForm()`` methods define optional arguments to
822+
add custom server parameters and HTTP headers when submitting the form::
824823

825824
$client->submit($form, array(), array('HTTP_ACCEPT_LANGUAGE' => 'es'));
825+
$client->submitForm($button, array(), 'POST', array('HTTP_ACCEPT_LANGUAGE' => 'es'));
826826

827827
.. versionadded:: 4.1
828828
The feature to add custom HTTP headers was introduced in Symfony 4.1.

0 commit comments

Comments
 (0)