Skip to content

css-selector is an optional dependency of the browser-kit #8982

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

Merged
merged 1 commit into from
Jan 5, 2018
Merged
Changes from all commits
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
30 changes: 23 additions & 7 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,9 @@ As an example, a test could look like this::
{
$client = static::createClient();

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

$this->assertGreaterThan(
0,
$crawler->filter('html:contains("Hello World")')->count()
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}

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

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

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

Click on a link by first selecting it with the crawler using either an XPath
expression or a CSS selector, then use the client to click on it. For example::
The crawler integrates with the ``symfony/css-selector`` component to give you the
power of CSS selectors to find content in a page. To install the CSS selector
component, run:

.. code-block:: terminal

$ composer require --dev css-selector

Now you can use CSS selectors with the crawler. To assert that the phrase
"Hello World" is on the page at least once, you can use this assertion::

$this->assertGreaterThan(
0,
$crawler->filter('html:contains("Hello World")')->count()
);

The crawler can also be used to interact with the page. Click on a link by first
selecting it with the crawler using either an XPath expression or a CSS selector,
then use the client to click on it::

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