5
5
The BrowserKit Component
6
6
========================
7
7
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.
11
10
12
11
Installation
13
12
------------
14
13
15
14
You can install the component in two different ways:
16
15
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 `_);
18
18
* Use the official Git repository (https://github.com/symfony/BrowserKit).
19
19
20
20
Basic Usage
21
21
-----------
22
22
23
- .. note ::
24
-
25
- The component only provides an abstract client and does not provide any "default" backend for the HTTP layer.
26
-
27
23
Creating a Client
28
- -----------------
24
+ ~~~~~~~~~~~~~~~~~
29
25
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 .
32
28
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::
34
32
35
- namespace ACME ;
33
+ namespace Acme ;
36
34
37
35
use Symfony\Component\BrowserKit\Client as BaseClient;
38
36
use Symfony\Component\BrowserKit\Response;
39
37
40
- class Client extends BaseClient
38
+ class Client extends BaseClient
41
39
{
42
- protected function doRequest($request)
40
+ protected function doRequest($request)
43
41
{
44
42
// convert request into a response
45
43
// ...
44
+
46
45
return new Response($content, $status, $headers);
47
46
}
48
47
}
49
48
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 `.
54
52
55
53
Making Requests
56
54
~~~~~~~~~~~~~~~
57
55
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::
61
59
62
- .. code-block :: php
63
-
64
- use ACME\Client;
60
+ use Acme\Client;
65
61
66
62
$client = new Client();
67
63
$crawler = $client->request('GET', 'http://symfony.com');
68
64
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
+
69
69
Clicking Links
70
70
~~~~~~~~~~~~~~
71
71
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::
73
76
74
- .. code-block :: php
75
-
76
- use ACME\Client;
77
+ use Acme\Client;
77
78
78
79
$client = new Client();
79
80
$crawler = $client->request('GET', 'http://symfony.com');
80
81
$link = $crawler->selectLink('Go elsewhere...')->link();
81
82
$client->click($link);
82
83
83
- Submiting Forms
84
- ~~~~~~~~~~~~~~~
84
+ Submitting Forms
85
+ ~~~~~~~~~~~~~~~~
85
86
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::
88
91
89
- .. code-block :: php
90
-
91
- use ACME\Client;
92
+ use Acme\Client;
92
93
93
94
// make a real request to an external site
94
95
$client = new Client();
@@ -105,14 +106,14 @@ You can get the form object by using the crawler to select the button and runnin
105
106
Cookies
106
107
-------
107
108
108
- Retreiving Cookies
109
- ~~~~~~~~~~~~~~~~~~
110
-
111
- The Crawler has a cookieJar which is a container for storing and recieving cookies.
109
+ Retrieving Cookies
110
+ ~~~~~~~~~~~~~~~~~~
112
111
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::
114
115
115
- use ACME \Client;
116
+ use Acme \Client;
116
117
117
118
// Make a request
118
119
$client = new Client();
@@ -122,25 +123,28 @@ The Crawler has a cookieJar which is a container for storing and recieving cooki
122
123
$cookieJar = $crawler->getCookieJar();
123
124
124
125
// Get a cookie by name
125
- $flavor = $cookieJar->get('flavor ');
126
+ $cookie = $cookieJar->get('name_of_the_cookie ');
126
127
127
128
// 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.
137
141
138
142
Looping Through Cookies
139
143
~~~~~~~~~~~~~~~~~~~~~~~
140
144
141
145
.. code-block :: php
142
146
143
- use ACME \Client;
147
+ use Acme \Client;
144
148
145
149
// Make a request
146
150
$client = new Client();
@@ -151,7 +155,7 @@ Looping Through Cookies
151
155
152
156
// Get array with all cookies
153
157
$cookies = $cookieJar->all();
154
- foreach($cookies as $cookie)
158
+ foreach($cookies as $cookie)
155
159
{
156
160
// ...
157
161
}
@@ -170,17 +174,13 @@ Looping Through Cookies
170
174
// ...
171
175
}
172
176
173
- .. note ::
174
- These cookie jar methods only return cookies that have not expired.
175
-
176
177
Setting Cookies
177
178
~~~~~~~~~~~~~~~
178
179
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::
180
182
181
- .. code-block :: php
182
-
183
- use ACME\Client;
183
+ use Acme\Client;
184
184
185
185
// create cookies and add to cookie jar
186
186
$expires = new \DateTime();
@@ -198,11 +198,10 @@ You can define create cookies and add them to a cookie jar that can be injected
198
198
History
199
199
-------
200
200
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::
204
203
205
- use ACME \Client;
204
+ use Acme \Client;
206
205
207
206
// make a real request to an external site
208
207
$client = new Client();
@@ -218,21 +217,17 @@ The client stores all your requests allowing you to go back and forward in your
218
217
// go forward to documentation page
219
218
$doc_crawler = $client->forward();
220
219
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::
222
222
223
- .. code-block :: php
224
-
225
- use ACME\Client;
223
+ use Acme\Client;
226
224
227
225
// make a real request to an external site
228
226
$client = new Client();
229
227
$home_crawler = $client->request('GET', 'http://symfony.com');
230
228
231
- // restart history
229
+ // delete history
232
230
$client->restart();
233
231
234
-
235
232
.. _Packagist : https://packagist.org/packages/symfony/browser-kit
236
233
.. _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