Skip to content

[Testing] assertSelectorTextContains example is wrong #13385

@arneee

Description

@arneee

Hi,

affected page: https://symfony.com/doc/current/testing.html

Affected part of the page

$this->assertSelectorTextContains('html h1.title', 'Hello World')

This assertion will internally call $crawler->filter('html h1.title'), which allows you to use CSS selectors to filter any HTML element in the page and check for its existence, attributes, text, etc.

The assertSelectorTextContains method is not a native PHPUnit assertion and is available thanks to the WebTestCase class.

Using native PHPUnit methods, the same assertion would look like this:

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

This is wrong. assertSelectorTextContains() will check if the FIRST matching element contains the given text. The native phpunit example will check if there any more than 0 matching elements with that text.

For example, this test might fail since "Stuttgart" is not in the FIRST paragraph:
$this->assertSelectorTextContains('p','Stuttgart');

But the native phpunit test will succeed:

$this->assertGreaterThan(
        0,
        $crawler->filter('p:contains("Stuttgart")')->count()
);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions