Skip to content

Passing xpath to XmlStructure::matchElement() always throws Exception #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
marcovtwout opened this issue Nov 14, 2024 · 1 comment

Comments

@marcovtwout
Copy link

marcovtwout commented Nov 14, 2024

The signature allows param $cssOrXpath: https://github.com/Codeception/lib-xml/blob/main/src/Util/XmlStructure.php#L37
But when an xpath is passed, it will throw an exception.

This is broken since Codeception 5.0:

Reproduce

$xpath = '/example';
$I->grabTextContentFrom($xpath))
  • Run the test. Output:
1) ExampleCest: Call undefined operation
 Test  ExampleCest.php:example

  [Symfony\Component\CssSelector\Exception\SyntaxErrorException] Expected selector, but <delimiter "/" at 0> found.

Scenario Steps:

 4. $I->grabTextContentFrom("/example") at (..)

#1  (..)\vendor\symfony\css-selector\Exception\SyntaxErrorException.php:28
#2  (..)\vendor\symfony\css-selector\Parser\Parser.php:284
#3  (..)\vendor\symfony\css-selector\Parser\Parser.php:115
#4  (..)\vendor\symfony\css-selector\Parser\Parser.php:100
#5  (..)\vendor\symfony\css-selector\Parser\Parser.php:42
#6  (..)\vendor\symfony\css-selector\XPath\Translator.php:222
#7  (..)\vendor\symfony\css-selector\XPath\Translator.php:92
#8  (..)\vendor\symfony\css-selector\CssSelectorConverter.php:65
#9  Codeception\Module\SOAP->grabTextContentFrom

Possible fix

Re-implement the try-catch:

    public function matchElement(string $cssOrXPath): ?DOMNode
    {
        $domXpath = new DOMXpath($this->xml);
        try {
            $selector = (new CssSelectorConverter())->toXPath($cssOrXPath);
            $els = $domXpath->query($selector);
            if ($els !== false && count($els) > 0) {
                return $els->item(0);
            }
        } catch (\Symfony\Component\CssSelector\Exception\ParseException $e) {
        }
        $els = $domXpath->query($cssOrXPath);
        if ($els !== false && count($els) > 0) {
            return $els->item(0);
        }
        throw new ElementNotFound($cssOrXPath);
    }
@marcovtwout
Copy link
Author

@Naktibalda Could you explain if you did remove the try-catch on purpose, was this supposed to be caught some other way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant