@@ -387,21 +387,17 @@ returns a ``Crawler`` instance.
387
387
Use the crawler to find DOM elements in the response. These elements can then
388
388
be used to click on links and submit forms::
389
389
390
- $link = $crawler->selectLink('Go elsewhere...')->link();
391
- $crawler = $client->click($link);
390
+ $crawler = $client->clickLink('Go elsewhere...');
392
391
393
- $form = $crawler->selectButton('validate')->form();
394
- $crawler = $client->submit($form, array('name' => 'Fabien'));
392
+ $crawler = $client->submitForm('validate', array('name' => 'Fabien'));
395
393
396
- The ``click () `` and ``submit () `` methods both return a ``Crawler `` object.
394
+ The ``clickLink () `` and ``submitForm () `` methods both return a ``Crawler `` object.
397
395
These methods are the best way to browse your application as it takes care
398
396
of a lot of things for you, like detecting the HTTP method from a form and
399
397
giving you a nice API for uploading files.
400
398
401
- .. tip ::
402
-
403
- You will learn more about the ``Link `` and ``Form `` objects in the
404
- :ref: `Crawler <testing-crawler >` section below.
399
+ .. versionadded :: 4.2
400
+ The ``clickLink() `` and ``submitForm() `` methods were introduced in Symfony 4.2.
405
401
406
402
The ``request() `` method can also be used to simulate form submissions directly
407
403
or perform more complex requests. Some useful examples::
@@ -667,65 +663,68 @@ The Crawler can extract information from the nodes::
667
663
Links
668
664
~~~~~
669
665
670
- To select links, you can use the traversing methods above or the convenient
671
- `` selectLink() `` shortcut ::
666
+ Use the `` clickLink() `` method to click on the first link that contains the
667
+ given text (or the first clickable image with that `` alt `` attribute) ::
672
668
673
- $crawler->selectLink('Click here');
669
+ $client = static::createClient();
670
+ $client->request('GET', '/post/hello-world');
674
671
675
- This selects all links that contain the given text, or clickable images for
676
- which the ``alt `` attribute contains the given text. Like the other filtering
677
- methods, this returns another ``Crawler `` object.
672
+ $client->clickLink('Click here');
678
673
679
- Once you've selected a link, you have access to a special ``Link `` object,
680
- which has helpful methods specific to links (such as ``getMethod() `` and
681
- ``getUri() ``). To click on the link, use the Client's ``click() `` method
682
- and pass it a ``Link `` object::
674
+ If you need access to the :class: `Symfony\\ Component\\ DomCrawler\\ Link ` object
675
+ that provides helpful methods specific to links (such as ``getMethod() `` and
676
+ ``getUri() ``), use the ``selectLink() `` method instead:
683
677
684
- $link = $crawler->selectLink('Click here')->link();
678
+ $client = static::createClient();
679
+ $crawler = $client->request('GET', '/post/hello-world');
685
680
681
+ $link = $crawler->selectLink('Click here')->link();
686
682
$client->click($link);
687
683
688
684
Forms
689
685
~~~~~
690
686
691
- Forms can be selected using their buttons, which can be selected with the
692
- ``selectButton() `` method, just like links::
687
+ Use the ``submitForm() `` method to submit the form that contains the given button::
693
688
694
- $buttonCrawlerNode = $crawler->selectButton('submit');
689
+ $client = static::createClient();
690
+ $client->request('GET', '/post/hello-world');
691
+
692
+ $crawler = $client->submitForm('Add comment', array(
693
+ 'comment_form[content]' => '...',
694
+ ));
695
+
696
+ The first argument of ``submitForm() `` is the text content, ``id ``, ``value `` or
697
+ ``name `` of any ``<button> `` or ``<input type="submit"> `` included in the form.
698
+ The second optional argument is used to override the default form field values.
695
699
696
700
.. note ::
697
701
698
702
Notice that you select form buttons and not forms as a form can have several
699
703
buttons; if you use the traversing API, keep in mind that you must look for a
700
704
button.
701
705
702
- The ``selectButton() `` method can select ``button `` tags and submit ``input ``
703
- tags. It uses several parts of the buttons to find them:
706
+ If you need access to the :class: `Symfony\\ Component\\ DomCrawler\\ Form ` object
707
+ that provides helpful methods specific to forms (such as ``getUri() ``,
708
+ ``getValues() `` and ``getFields() ``) use the ``selectButton() `` method instead::
704
709
705
- * The ``value `` attribute value;
706
- * The ``id `` or ``alt `` attribute value for images;
707
- * The ``id `` or ``name `` attribute value for ``button `` tags.
710
+ $client = static::createClient();
711
+ $crawler = $client->request('GET', '/post/hello-world');
708
712
709
- Once you have a Crawler representing a button, call the ``form() `` method
710
- to get a ``Form `` instance for the form wrapping the button node::
713
+ $buttonCrawlerNode = $crawler->selectButton('submit');
711
714
715
+ // select the form that contains this button
712
716
$form = $buttonCrawlerNode->form();
713
717
714
- When calling the ``form() `` method, you can also pass an array of field values
715
- that overrides the default ones::
716
-
718
+ // you can also pass an array of field values that overrides the default ones
717
719
$form = $buttonCrawlerNode->form(array(
718
720
'my_form[name]' => 'Fabien',
719
721
'my_form[subject]' => 'Symfony rocks!',
720
722
));
721
723
722
- And if you want to simulate a specific HTTP method for the form, pass it as a
723
- second argument::
724
-
724
+ // you can pass a second argument to override the form HTTP method
725
725
$form = $buttonCrawlerNode->form(array(), 'DELETE');
726
726
727
- The Client can submit ``Form `` instances::
728
-
727
+ // submit the Form object
729
728
$client->submit($form);
730
729
731
730
The field values can also be passed as a second argument of the ``submit() ``
@@ -771,10 +770,11 @@ their type::
771
770
772
771
.. tip ::
773
772
774
- The ``submit() `` method defines a third optional argument to add custom
775
- HTTP headers when submitting the form::
773
+ The ``submit() `` and `` submitForm() `` methods define optional arguments to
774
+ add custom server parameters and HTTP headers when submitting the form::
776
775
777
776
$client->submit($form, array(), array('HTTP_ACCEPT_LANGUAGE' => 'es'));
777
+ $client->submitForm($button, array(), 'POST', array('HTTP_ACCEPT_LANGUAGE' => 'es'));
778
778
779
779
.. versionadded :: 4.1
780
780
The feature to add custom HTTP headers was introduced in Symfony 4.1.
0 commit comments