Skip to content

Commit 9072354

Browse files
committed
css-selector is an optional dependency of the browser-kit
restructured the testing introduction to not end up with an error in the first functional test example
1 parent 5d4254e commit 9072354

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

testing.rst

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,9 @@ As an example, a test could look like this::
151151
{
152152
$client = static::createClient();
153153

154-
$crawler = $client->request('GET', '/post/hello-world');
154+
$client->request('GET', '/post/hello-world');
155155

156-
$this->assertGreaterThan(
157-
0,
158-
$crawler->filter('html:contains("Hello World")')->count()
159-
);
156+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
160157
}
161158
}
162159

@@ -182,6 +179,8 @@ As an example, a test could look like this::
182179
``createKernel()`` or ``getKernelClass()`` methods of your functional test,
183180
which take precedence over the ``KERNEL_CLASS`` env var.
184181

182+
In the above example, you validated that the HTTP response was successful. The
183+
next step is to validate that the page actually contains the expected content.
185184
The ``createClient()`` method returns a client, which is like a browser that
186185
you'll use to crawl your site::
187186

@@ -197,8 +196,25 @@ be used to select elements in the response, click on links and submit forms.
197196
The ``Crawler`` only works when the response is an XML or an HTML document.
198197
To get the raw content response, call ``$client->getResponse()->getContent()``.
199198

200-
Click on a link by first selecting it with the crawler using either an XPath
201-
expression or a CSS selector, then use the client to click on it. For example::
199+
The crawler integrates with the ``symfony/css-selector`` component to give you the
200+
power of CSS selectors to find content in a page. To install the CSS selector
201+
component, run:
202+
203+
.. code-block:: terminal
204+
205+
$ composer require --dev css-selector
206+
207+
Now you can use CSS selectors with the crawler. To assert that the phrase
208+
"Hello World" is on the page at least once, you can use this assertion::
209+
210+
$this->assertGreaterThan(
211+
0,
212+
$crawler->filter('html:contains("Hello World")')->count()
213+
);
214+
215+
The crawler can also be used to interact with the page. Click on a link by first
216+
selecting it with the crawler using either an XPath expression or a CSS selector,
217+
then use the client to click on it::
202218

203219
$link = $crawler
204220
->filter('a:contains("Greet")') // find all links with the text "Greet"

0 commit comments

Comments
 (0)