Search code examples
phpunitfunctional-testinggoutte

Selecting an node by attribute with Goutte / Symfony CssSelector component


I'm using Goutte (which is a standalone package of the Symfony functional testing components) to test a Wordpress theme:

public function testDocumentHasBasicNodes(){
    $this->assertEquals(1, $this->crawler->filter('title')->count(),
            'document shall have a TITLE node');

    $this->assertEquals(1, $this->crawler->filter('meta')->count(),
            'document shall have a meta[charset="utf-8"] node');
}

The first test passes but:

indexTest::testDocumentHasBasicNodes
document shall have a meta[charset="utf-8"] node
Failed asserting that 0 matches expected 1.

So to sum up my question:
How do I get a node by attribute with Goutte / Symfony CSS Selector component?


Solution

  • You may filter by attribute like this.

    $this->assertEquals(1, $this->crawler->filter('meta[charset="utf-8"]')->count(),
        'document shall have a meta[charset="utf-8"] node');