Codeception / Codeception/lib-xml

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

Đang mở
#6 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
PHP
Star
17
Fork
1
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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:
- v4 used to catch parser exceptions, and then continues to try an xpath: https://github.com/Codeception/Codeception/blob/4.2.2/src/Codeception/Util/XmlStructure.php#L39-L46
- v5 does not: https://github.com/Codeception/lib-xml/blob/main/src/Util/XmlStructure.php#L40

## Reproduce

- Create a unit test and pass an xpath, for example through Codeceptions SOAP module `grabTextContentFrom()` (https://codeception.com/docs/modules/SOAP#grabTextContentFrom):
```
$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 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:
```php
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);
}
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.