10000 Added docs for clickLink() and submitForm() by javiereguiluz · Pull Request #10071 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Added docs for clickLink() and submitForm() #10071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Explain the shortcuts first
  • Loading branch information
javiereguiluz committed Jul 18, 2018
commit 59abd1574036c0ac8f3bf59d8d1ba7d18eb0b50d
89 changes: 53 additions & 36 deletions components/browser_kit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,67 +97,84 @@ make AJAX requests::
Clicking Links
~~~~~~~~~~~~~~

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

use Acme\Client;
use Acme\Client;

$client = new Client();
$crawler = $client->request('GET', '/product/123');
$link = $crawler->selectLink('Go elsewhere...')->link();
$client->click($link);
$client->request('GET', '/product/123');

If you don't need the low level ``Link`` object, you can use the ``clickLink()``
shortcut to click links more easily::

// ...
$client->clickLink('Go elsewhere...');
$crawler = $client->clickLink('Go elsewhere...');

.. versionadded:: 4.2
The ``clickLink()`` method was introduced in Symfony 4.2.

If you need the :class:`Symfony\\Component\\DomCrawler\\Link` object that
provides access to the link properties (e.g. ``$link->getMethod()``,
``$link->getUri()``), use this other method:

// ...
$crawler = $client->request('GET', '/product/123');
$link = $crawler->selectLink('Go elsewhere...')->link();
$client->click($link);

Submitting Forms
~~~~~~~~~~~~~~~~

The ``Crawler`` object is also capable of selecting forms. First, select any of
the form's buttons with the ``selectButton EED1 ()`` method. Then, use the ``form()``
method to select the form which the button belongs to.

After selecting the form, fill in its data and send it using the ``submit()``
method (which makes the needed HTTP POST request to submit the form contents)::
The ``Client`` object is also capable of submitting forms. First, select the
form using any of its buttons and then override any of its properties (method,
field values, etc.) before submitting it:

use Acme\Client;

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

// find the form with the 'Log in' button and submit it
// 'Log in' can be the text content, id, value or name of a <button> or <input type="submit">
$client->submitForm('Log in');

// same as above, but override the default form field values
$client->submitForm('Log in', array(
'login' => 'my_user',
'password' => 'my_pass',
// to upload a file, the value must be the absolute file path
'file' => __FILE__,
));

// same as above, but also override the default form HTTP method
$client->submitForm('Log in', array(
'login' => 'my_user',
'password' => 'my_pass',
), 'PUT');

// same as above, but also override some $_SERVER parameters (e.g. HTTP headers)
$client->submitForm('Log in', array(
'login' => 'my_user',
'password' => 'my_pass',
), 'PUT', array(
'HTTP_ACCEPT_LANGUAGE' => 'es',
));

.. versionadded:: 4.2
The ``submitForm()`` method was introduced in Symfony 4.2.

If you need the :class:`Symfony\\Component\\DomCrawler\\Form` object that
provides access to the form properties (e.g. ``$form->getUri()``,
``$form->getValues()``, ``$form->getFields()``), use this other method:

// ...

// select the form and fill in some values
$form = $crawler->selectButton('Log in')->form();
$form['login'] = 'symfonyfan';
$form['password'] = 'anypass';

// To upload a file, the value should be the absolute file path
$form['file'] = __FILE__;

// submit that form
$crawler = $client->submit($form);

If you don't need the low level ``Form`` object, you can use the
``submitForm()`` shortcut to submit the form more easily::

// ...
$client->submitForm('Log in', [
'login' => 'symfonyfan',
'password' => 'anypass',
'file' => __FILE__,
]);

.. versionadded:: 4.2
The ``submitForm()`` method was introduced in Symfony 4.2.

Cookies
-------

Expand Down
0