Skip to content

How to check if an element is unique by xpath #6546

Closed

Description

@pamoller

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

Activity

  1. Naktibalda commented on Aug 13, 2022

    @Naktibalda
    Member

    new methods were released as codeception/module-rest 3.2.0

  2. changed the title [-]How to check if an elemet is unique by xpath[/-] [+]How to check if an element is unique by xpath[/+] on Aug 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions