This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Using CssSelector Component

Discuss Symfony 2 bundles here

Using CssSelector Component

by beltouche » Sat Nov 01, 2014 1:55 am

I've installed the CssSelector component with Composer, but either it doesn't work reliably at all, or I'm missing something. (My guess is the latter.)
Here's a pseudo-code stub of what I have:
Code: Select all
namespace me\MyBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\CssSelector\CssSelector;
use Symfony\Component\DomCrawler\Crawler;

class SomeControllerTest extends WebTestCase
   public function testCreateActionReturnsProperForm(){
        $client = static::createClient();

        $crawler = $client->request('GET', 'foo/bar');
        $this->assertCount(some_number, $crawler->filter(some_css_selector));

This what kinds of things I'm getting with different selectors & the counts I expect/know I have:
  • if the selector is h1 or form, the assertion passes. I have one of each.
  • if the selector is span or div, the assertions also make sense in that I know I have a lot of them, and the counts seem reasonable.
  • I know there are 19 labels, but the assertion fails, claiming there's only one.
  • for selectors using attributes, e.g., div.some_class, or #some_id, the filter count is zero, when I know these exist
  • for some regular (new HTML5) tags, e.g. header & section, the counts are also zero, despite the tags existing

I'd appreciate some help in understanding how to use this component.
I've tried lots of variants, including using CssSelector::cssToXPath('div.some_class') and/or $crawler->filterXPath(), but the results are similar, or error out.

Junior Member
Posts: 1
Joined: Sat Nov 01, 2014 1:31 am