Skip to content

[BrowserKit] selectButton method does not look for all legitimate elements when providing a input value #61061

Closed
@bytes-commerce

Description

@bytes-commerce

Symfony version(s) affected

7.3

Description

This method accepts a string $button and then passes it to the crawler:

public function submitForm(string $button, array $fieldValues = [], string $method = 'POST', array $serverParameters = []): Crawler
    {
        $crawler = $this->crawler ?? throw new BadMethodCallException(\sprintf('The "request()" method must be called before "%s()".', __METHOD__));
        $buttonNode = $crawler->selectButton($button);

        if (0 === $buttonNode->count()) {
            throw new InvalidArgumentException(\sprintf('There is no button with "%s" as its content, id, value or name.', $button));
        }

        $form = $buttonNode->form($fieldValues, $method);

        return $this->submit($form, [], $serverParameters);
    }

Now, the crawler has this XPath setup:

    public function selectButton(string $value): static
    {
        return $this->filterRelativeXPath(
            \sprintf('descendant-or-self::input[((contains(%1$s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)) or (contains(%1$s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)) or @id=%3$s or @name=%3$s] | descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s) or @id=%3$s or @name=%3$s]', 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")', static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value))
        );
    }

Here, only input fields will be searched for when looking for "value" values, however, not only inputs can have a value, also buttons do.

This leads to a situation where the logic will not find any button with given value. This must be adapted in order to correctly identify respective DOM elements.

How to reproduce

Have this HTML:

<div class="page-actions">                <button class="action-saveAndAddAnother btn btn-secondary action-save" type="submit" name="ea[newForm][btn]" value="saveAndAddAnother" data-action-name="saveAndAddAnother" form="new-Task-form">
        <span class="btn-label"><span class="action-label">Erstellen und weiteres Element hinzufügen</span></span>
    </button>

                <button class="action-saveAndReturn btn btn-primary action-save" type="submit" name="ea[newForm][btn]" value="saveAndReturn" data-action-name="saveAndReturn" form="new-Task-form">
        <span class="btn-label"><span class="action-label">Erstellen</span></span>
    </button>

    </div>

Now, check if you can fetch the correct button by passing Erstellen -> crawler will select the first button and not the one with the literal value (probably intended).

The first button here is "Create and create another", the second one is "create only".

Now, if I pass the buttons VALUE to the method: saveAndReturn

It will not find anything.

Possible Solution

Just adapt the XPath accordingly:

public function selectButton(string $value): static
    {
        return $this->filterRelativeXPath(
            \sprintf(
                'descendant-or-self::input[
            (
                (contains(%1$s, "submit") or contains(%1$s, "button"))
                and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)
            )
            or (
                contains(%1$s, "image")
                and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)
            )
            or @id=%3$s
            or @name=%3$s
        ]
        |
        descendant-or-self::button[
            contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s)
            or contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)
            or @id=%3$s
            or @name=%3$s
        ]',
                'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")',
                static::xpathLiteral(' '.$value.' '),
                static::xpathLiteral($value)
            )
        );
    }

This succesfully lets the crawler identify the required button correctly.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy