55The BrowserKit Component
66========================
77
8- The BrowserKit component simulates the behavior of a web browser.
9-
10- The BrowserKit component allows you to make web requests, click on links and submit forms.
8+ The BrowserKit component simulates the behavior of a web browser, allowing
9+ you to make requests, click on links and submit forms programmatically.
1110
1211Installation
1312------------
1413
1514You can install the component in two different ways:
1615
17- * :doc: `Install it via Composer </components/using_components >` (``symfony/browser-kit `` on `Packagist `_);
16+ * :doc: `Install it via Composer </components/using_components >`
17+ (``symfony/browser-kit `` on `Packagist `_);
1818* Use the official Git repository (https://github.com/symfony/BrowserKit).
1919
2020Basic Usage
2121-----------
2222
23- .. note ::
24-
25- The component only provides an abstract client and does not provide any "default" backend for the HTTP layer.
26-
2723Creating a Client
28- -----------------
24+ ~~~~~~~~~~~~~~~~~
2925
30- To create your own client you must extend the abstract client class and implement the doRequest method.
31- This method accepts a requests and should return a response .
26+ The component only provides an abstract client and does not provide any backend
27+ ready to use for the HTTP layer .
3228
33- .. code-block :: php
29+ To create your own client you must extend the abstract client class and
30+ implement the :method: `Symfony\\ Component\\ BrowserKit\\ Client::doRequest ` method.
31+ This method accepts a request and should return a response::
3432
35- namespace ACME ;
33+ namespace Acme ;
3634
3735 use Symfony\Component\BrowserKit\Client as BaseClient;
3836 use Symfony\Component\BrowserKit\Response;
3937
40- class Client extends BaseClient
38+ class Client extends BaseClient
4139 {
42- protected function doRequest($request)
40+ protected function doRequest($request)
4341 {
4442 // convert request into a response
4543 // ...
44+
4645 return new Response($content, $status, $headers);
4746 }
4847 }
4948
50- For a simple implementation of a browser based on an HTTP layer, have a look at Goutte _.
51-
52- For an implementation based on ``HttpKernelInterface ``, have a look at the Client provided by the :doc: `/components/http_kernel/introduction `.
53-
49+ For a simple implementation of a browser based on an HTTP layer, have a look
50+ at `Goutte `_. For an implementation based on ``HttpKernelInterface ``, have a
51+ look at the Client provided by the :doc: `/components/http_kernel/introduction `.
5452
5553Making Requests
5654~~~~~~~~~~~~~~~
5755
58- To make a request you use the client's request _ method.
59- The first two arguments are for the HTTP method and the request URL.
60- The request method will return a crawler object.
56+ Use the :method: ` Symfony \\ Component \\ BrowserKit \\ Client:: request` method to make
57+ any HTTP request. The first two arguments are for the HTTP method and the
58+ requested URL::
6159
62- .. code-block :: php
63-
64- use ACME\Client;
60+ use Acme\Client;
6561
6662 $client = new Client();
6763 $crawler = $client->request('GET', 'http://symfony.com');
6864
65+ The value returned by the ``request() `` method is an instance of the
66+ :class: `Symfony\\ Component\\ DomCrawler\\ Crawler ` class, which allows accessing
67+ and traversing HTML elements programmatically.
68+
6969Clicking Links
7070~~~~~~~~~~~~~~
7171
72- Select a link with the crawler and pass it to the click _ method to click on the link.
72+ The ``Crawler `` object is capable of simulating link clicks. First, pass the
73+ text content of the link to the ``selectLink() `` method, which returns you a
74+ ``Link `` object. Then, pass this object to the ``click() `` method, which makes
75+ the needed HTTP GET request to simulate the link click::
7376
74- .. code-block :: php
75-
76- use ACME\Client;
77+ use Acme\Client;
7778
7879 $client = new Client();
7980 $crawler = $client->request('GET', 'http://symfony.com');
8081 $link = $crawler->selectLink('Go elsewhere...')->link();
8182 $client->click($link);
8283
83- Submiting Forms
84- ~~~~~~~~~~~~~~~
84+ Submitting Forms
85+ ~~~~~~~~~~~~~~~~
8586
86- You can submit forms with the submit method which takes a form object.
87- You can get the form object by using the crawler to select the button and running the form method.
87+ The ``Crawler `` object is also capable of simulating form submissions. First,
88+ select the form via any of its buttons (thanks to the ``selectButton() `` and
89+ ``form() `` methods). Then, fill in the form data to send and use the ``submit() ``
90+ method to make the needed HTTP POST request to submit the form::
8891
89- .. code-block :: php
90-
91- use ACME\Client;
92+ use Acme\Client;
9293
9394 // make a real request to an external site
9495 $client = new Client();
@@ -105,14 +106,14 @@ You can get the form object by using the crawler to select the button and runnin
105106Cookies
106107-------
107108
108- Retreiving Cookies
109- ~~~~~~~~~~~~~~~~~~
110-
111- The Crawler has a cookieJar which is a container for storing and recieving cookies.
109+ Retrieving Cookies
110+ ~~~~~~~~~~~~~~~~~~
112111
113- .. code-block :: php
112+ The ``Crawler `` object exposes cookies (if any) through a
113+ :class: `Symfony\C omponent\B rowserKit\C ookieJar `, which allows you to store and
114+ retrieve any cookie while making requests with the client::
114115
115- use ACME \Client;
116+ use Acme \Client;
116117
117118 // Make a request
118119 $client = new Client();
@@ -122,25 +123,28 @@ The Crawler has a cookieJar which is a container for storing and recieving cooki
122123 $cookieJar = $crawler->getCookieJar();
123124
124125 // Get a cookie by name
125- $flavor = $cookieJar->get('flavor ');
126+ $cookie = $cookieJar->get('name_of_the_cookie ');
126127
127128 // Get cookie data
128- $name = $flavor->getName();
129- $value = $flavor->getValue();
130- $raw = $flavor->getRawValue();
131- $secure = $flavor->isSecure();
132- $isHttpOnly = $flavor->isHttpOnly();
133- $isExpired = $flavor->isExpired();
134- $expires = $flavor->getExpiresTime();
135- $path = $flavor->getPath();
136- $domain = $flavor->getDomain();
129+ $name = $cookie->getName();
130+ $value = $cookie->getValue();
131+ $raw = $cookie->getRawValue();
132+ $secure = $cookie->isSecure();
133+ $isHttpOnly = $cookie->isHttpOnly();
134+ $isExpired = $cookie->isExpired();
135+ $expires = $cookie->getExpiresTime();
136+ $path = $cookie->getPath();
137+ $domain = $cookie->getDomain();
138+
139+ .. note ::
140+ These methods only return cookies that have not expired.
137141
138142Looping Through Cookies
139143~~~~~~~~~~~~~~~~~~~~~~~
140144
141145.. code-block :: php
142146
143- use ACME \Client;
147+ use Acme \Client;
144148
145149 // Make a request
146150 $client = new Client();
@@ -151,7 +155,7 @@ Looping Through Cookies
151155
152156 // Get array with all cookies
153157 $cookies = $cookieJar->all();
154- foreach($cookies as $cookie)
158+ foreach($cookies as $cookie)
155159 {
156160 // ...
157161 }
@@ -170,17 +174,13 @@ Looping Through Cookies
170174 // ...
171175 }
172176
173- .. note ::
174- These cookie jar methods only return cookies that have not expired.
175-
176177 Setting Cookies
177178~~~~~~~~~~~~~~~
178179
179- You can define create cookies and add them to a cookie jar that can be injected it into the client constructor.
180+ You can also create cookies and add them to a cookie jar that can be injected
181+ into the client constructor::
180182
181- .. code-block :: php
182-
183- use ACME\Client;
183+ use Acme\Client;
184184
185185 // create cookies and add to cookie jar
186186 $expires = new \DateTime();
@@ -198,11 +198,10 @@ You can define create cookies and add them to a cookie jar that can be injected
198198History
199199-------
200200
201- The client stores all your requests allowing you to go back and forward in your history.
202-
203- .. code-block :: php
201+ The client stores all your requests allowing you to go back and forward in your
202+ history::
204203
205- use ACME \Client;
204+ use Acme \Client;
206205
207206 // make a real request to an external site
208207 $client = new Client();
@@ -218,21 +217,17 @@ The client stores all your requests allowing you to go back and forward in your
218217 // go forward to documentation page
219218 $doc_crawler = $client->forward();
220219
221- You can restart the client's history with the restart method. This will also clear out the CookieJar.
220+ You can delete the client's history with the ``restart() `` method. This will
221+ also delete all the cookies::
222222
223- .. code-block :: php
224-
225- use ACME\Client;
223+ use Acme\Client;
226224
227225 // make a real request to an external site
228226 $client = new Client();
229227 $home_crawler = $client->request('GET', 'http://symfony.com');
230228
231- // restart history
229+ // delete history
232230 $client->restart();
233231
234-
235232.. _Packagist : https://packagist.org/packages/symfony/browser-kit
236233.. _Goutte : https://github.com/fabpot/Goutte
237- .. _request : http://api.symfony.com/2.3/Symfony/Component/BrowserKit/Client.html#method_request
238- .. _click : http://api.symfony.com/2.3/Symfony/Component/BrowserKit/Client.html#method_click
0 commit comments