@@ -422,21 +422,17 @@ returns a ``Crawler`` instance.
422422Use the crawler to find DOM elements in the response. These elements can then
423423be 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.
432430These methods are the best way to browse your application as it takes care
433431of a lot of things for you, like detecting the HTTP method from a form and
434432giving 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
441437The ``request() `` method can also be used to simulate form submissions directly
442438or perform more complex requests. Some useful examples::
@@ -715,65 +711,68 @@ The Crawler can extract information from the nodes::
715711Links
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
736732Forms
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
779778The 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