hamcrest / JavaHamcrest

Java (and original) version of Hamcrest
http://hamcrest.org/
BSD 3-Clause "New" or "Revised" License
2.11k stars 376 forks source link

Matchers.hasXPath should handle valueMatcher against multiple elements #323

Open fabian-froehlich opened 3 years ago

fabian-froehlich commented 3 years ago

It seems convinient to have a XPath-Matcher that is not limited to only look into the first matched element for the XPath

Example-XML:

<books>
    <book>
        <isbn>A-ISBN</isbn>
        <name>A-Book</name>
    </book>
    <book>
        <isbn>B-ISBN</isbn>
        <name>B-Book</name>
    </book>
</books>

will not be accpeted by the assert assertThat(xml, hasXPath("/books/book/isbn", is(equalTo("B-ISBN"))));

Expected: an XML document with XPath /books/book/isbn is "B-ISBN"
     but: mismatches were: [an XML document with XPath /books/book/isbn is "B-ISBN" was "A-ISBN"

But this should match because the book with B-ISBN exists.

A workaround would be to leave out the valueMatcher and put the value into the XPath assertThat(actualDocument, hasXPath("/books/book/isbn[text()='B-ISBN']")); which will correctly match the existence but at costs of a poorly explaining error message. This makes it harder identify whether your test is broken or your system-under-test.

night-gale commented 3 years ago

Hi, I am currently working on this issue @nhojpatrick , and had a PR