@@ -422,21 +422,17 @@ returns a ``Crawler`` instance.
422
422
Use the crawler to find DOM elements in the response. These elements can then
423
423
be used to click on links and submit forms::
424
424
425
- $link = $crawler->selectLink('Go elsewhere...')->link();
426
- $crawler = $client->click($link);
425
+ $crawler = $client->clickLink('Go elsewhere...');
427
426
428
- $form = $crawler->selectButton('validate')->form();
429
- $crawler = $client->submit($form, array('name' => 'Fabien'));
427
+ $crawler = $client->submitForm('validate', array('name' => 'Fabien'));
430
428
431
- The ``click () `` and ``submit () `` methods both return a ``Crawler `` object.
429
+ The ``clickLink () `` and ``submitForm () `` methods both return a ``Crawler `` object.
432
430
These methods are the best way to browse your application as it takes care
433
431
of a lot of things for you, like detecting the HTTP method from a form and
434
432
giving you a nice API for uploading files.
435
433
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.
440
436
441
437
The ``request() `` method can also be used to simulate form submissions directly
442
438
or perform more complex requests. Some useful examples::
@@ -715,65 +711,68 @@ The Crawler can extract information from the nodes::
715
711
Links
716
712
~~~~~
717
713
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) ::
720
716
721
- $crawler->selectLink('Click here');
717
+ $client = static::createClient();
718
+ $client->request('GET', '/post/hello-world');
722
719
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');
726
721
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:
731
725
732
- $link = $crawler->selectLink('Click here')->link();
726
+ $client = static::createClient();
727
+ $crawler = $client->request('GET', '/post/hello-world');
733
728
729
+ $link = $crawler->selectLink('Click here')->link();
734
730
$client->click($link);
735
731
736
732
Forms
737
733
~~~~~
738
734
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::
741
736
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.
743
747
744
748
.. note ::
745
749
746
750
Notice that you select form buttons and not forms as a form can have several
747
751
buttons; if you use the traversing API, keep in mind that you must look for a
748
752
button.
749
753
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::
752
757
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');
756
760
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');
759
762
763
+ // select the form that contains this button
760
764
$form = $buttonCrawlerNode->form();
761
765
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
765
767
$form = $buttonCrawlerNode->form(array(
766
768
'my_form[name]' => 'Fabien',
767
769
'my_form[subject]' => 'Symfony rocks!',
768
770
));
769
771
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
773
773
$form = $buttonCrawlerNode->form(array(), 'DELETE');
774
774
775
- The Client can submit ``Form `` instances::
776
-
775
+ // submit the Form object
777
776
$client->submit($form);
778
777
779
778
The field values can also be passed as a second argument of the ``submit() ``
@@ -819,10 +818,11 @@ their type::
819
818
820
819
.. tip ::
821
820
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::
824
823
825
824
$client->submit($form, array(), array('HTTP_ACCEPT_LANGUAGE' => 'es'));
825
+ $client->submitForm($button, array(), 'POST', array('HTTP_ACCEPT_LANGUAGE' => 'es'));
826
826
827
827
.. versionadded :: 4.1
828
828
The feature to add custom HTTP headers was introduced in Symfony 4.1.
0 commit comments