Skip to content

[FrameworkBundle] Test assertAnySelectorText* functions #48213

@SVillette

Description

@SVillette

Description

In a functional test, it is useful to check that a specific text is present on an HTML element. For example, at the end of a controller action test, an assertion could be made to check that an entity property has been modified.
The current assertSelectorTextContains and assertSelectorTextSame only check the first occurence of a given selector. It is a problem when the element is in an unknown position.

I would like to know if others already had the same issue, and if this 2 functions would be useful for the community (assertAnySelectorTextContains and assertAnySelectorTextSame).

Example

A simple example with ul li selector

<ul>
    <li>test1</li>
    <li>test2</li>
    <li>test3</li>
</ul>
// If the node is at first position
public function testIndexAction(): void
{
    $client = self::createClient();
    $client->request('GET', '/');

    self::assertResponseIsSuccessful();
    self::assertSelectorTextContains('ul li', 'test1');
}

// If the node position is known
public function testIndexAction2(): void
{
    $client = self::createClient();
    $client->request('GET', '/');

    self::assertResponseIsSuccessful();
    self::assertSelectorTextContains('ul li:nth-child(2)', 'test2');
}

// If the node position is unknown
public function testIndexAction3(): void
{
    $client = self::createClient();
    $crawler = $client->request('GET', '/');

    $this->assertResponseIsSuccessful();
    $nodes = $crawler->filter('ul li')->each(fn(Crawler $node) => $node->text());

    self::assertResponseIsSuccessful();
    self::assertContains('test3', $nodes);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrameworkBundleRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions