Codeception icon indicating copy to clipboard operation
Codeception copied to clipboard

How to check if an elemet is unique by xpath

Open pamoller opened this issue 3 years ago • 0 comments

What are you trying to achieve?

I wanna check that a json element is unique

What do you get instead?

The xpath expression does not work

1) IdempotencyCest: Test
 Test  tests/api/setrex/v3/sync-data/IdempotencyCest.php:test
 Step  See response json matches xpath "count(//documentid[text() ='4821726c1947cdf3eebacade98173939'])=1"
 Fail  Received JSON did not match the XPath `count(//documentid[text() ='4821726c1947cdf3eebacade98173939'])=1`.
Json Response: 

Provide test source code if related

$I->seeResponseJsonMatchesXpath("count(//documentid[text() ='4821726c1947cdf3eebacade98173939'])=1");        

Details

 public function seeResponseJsonMatchesXpath(string $xPath): void
    {
        $response = $this->connectionModule->_getResponseContent();
        $this->assertGreaterThan(
            0,
            (new JsonArray($response))->filterByXPath($xPath)->length,
            "Received JSON did not match the XPath `{$xPath}`.\nJson Response: \n" . $response
        );
    }

Does not support any number of occurance, even use $path->evaluate to get a meaninful result for count(//..)=1

 /**
     * @return DOMNodeList|bool
     */
    public function filterByXPath(string $xPath)
    {
        $path = new DOMXPath($this->toXml());
        return $path->query($xPath);
    }

Thanks in advance

  • Codeception version: ^4.2
  • PHP Version: 7.4
  • Operating System: PHP Docker
  • Installation type: Phar || Composer
  • List of installed packages (composer show)
  • Suite configuration:
# paste suite config here

pamoller avatar Aug 09 '22 11:08 pamoller

new methods were released as codeception/module-rest 3.2.0

Naktibalda avatar Aug 13 '22 12:08 Naktibalda