.. index:: single: BrowserKit single: Components; BrowserKit The BrowserKit Component ======================== The BrowserKit component simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically. .. note:: In Symfony versions prior to 4.3, the BrowserKit component could only make internal requests to your application. Starting from Symfony 4.3, this component can also :ref:`make HTTP requests to any public site ` when using it in combination with the :doc:`HttpClient component `. Installation ------------ .. code-block:: terminal $ composer require symfony/browser-kit .. include:: /components/require_autoload.rst.inc Basic Usage ----------- .. seealso:: This article explains how to use the BrowserKit features as an independent component in any PHP application. Read the :ref:`Symfony Functional Tests ` article to learn about how to use it in Symfony applications. Creating a Client ~~~~~~~~~~~~~~~~~ The component only provides an abstract client and does not provide any backend ready to use for the HTTP layer. To create your own client, you must extend the abstract ``Client`` class and implement the :method:`Symfony\\Component\\BrowserKit\\Client::doRequest` method. This method accepts a request and should return a response:: namespace Acme; use Symfony\Component\BrowserKit\Client as BaseClient; use Symfony\Component\BrowserKit\Response; class Client extends BaseClient { protected function doRequest($request) { // ... convert request into a response return new Response($content, $status, $headers); } } For a simple implementation of a browser based on the HTTP layer, have a look at the :class:`Symfony\\Component\\BrowserKit\\HttpBrowser` provided by :ref:`this component `. For an implementation based on ``HttpKernelInterface``, have a look at the :class:`Symfony\\Component\\HttpKernel\\Client` provided by the :doc:`HttpKernel component `. Making Requests ~~~~~~~~~~~~~~~ Use the :method:`Symfony\\Component\\BrowserKit\\Client::request` method to make HTTP requests. The first two arguments are the HTTP method and the requested URL:: use Acme\Client; $client = new Client(); $crawler = $client->request('GET', '/'); The value returned by the ``request()`` method is an instance of the :class:`Symfony\\Component\\DomCrawler\\Crawler` class, provided by the :doc:`DomCrawler component `, which allows accessing and traversing HTML elements programmatically. The :method:`Symfony\\Component\\BrowserKit\\Client::xmlHttpRequest` method, which defines the same arguments as the ``request()`` method, is a shortcut to make AJAX requests:: use Acme\Client; $client = new Client(); // the required HTTP_X_REQUESTED_WITH header is added automatically $crawler = $client->xmlHttpRequest('GET', '/'); Clicking Links ~~~~~~~~~~~~~~ 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; $client = new Client(); $client->request('GET', '/product/123'); $crawler = $client->clickLink('Go elsewhere...'); 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 ``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; $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