Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Marcin Grzejszczak
    @marcingrzejszczak
    BUT
    what you can do is to return the xpath() and using that you can get an array using XPath and assert the size
    private ResultSequence xPathExpression(String xPathString) {
            try {
                XPath2Expression expr = new Engine().parseExpression(xPathString, cachedObjects.xpathBuilder);
                return expr.evaluate(new DynamicContextBuilder(cachedObjects.xpathBuilder),
                        new Object[] { cachedObjects.document });
            } catch (Exception e) {
                throw new XmlAsserterXpathException(xPath(), cachedObjects.xmlAsString, e);
            }
        }
    that's how I do it using the xpathstring
    that's how I read it
    @Override
        public String read() {
            String xpath = xPath();
            ResultSequence expr = xPathExpression(xpath);
            if (expr.empty()) {
                throw new XmlAsserterXpathException(xPath(), cachedObjects.xmlAsString);
            }
            if (expr instanceof ElementType) {
               return ((ElementType) expr).getStringValue();
            }
            throw new UnsupportedOperationException("Can't return values of complex types");
        }
    and of course you can do sth else with the ElementType
    most likely it will have the size too
    @xtwmx
    xtwmx
    @xtwmx
    ok, I can create a ticket as it would be useful. I am still novice java tester and can't do the dev myself.
    Marcin Grzejszczak
    @marcingrzejszczak
    :+1:
    xtwmx
    @xtwmx
    I just remembered that xpath has built in method count too, it's probably all I need
    Marcin Grzejszczak
    @marcingrzejszczak
    oh yeah that's true
    but we could reuse it
    in the implementation to just provide the count method
    xtwmx
    @xtwmx
    yeah, that would be elegant :)
    Marcin Grzejszczak
    @marcingrzejszczak
    sweet - if you manage to make it work just add that to the issue ok ?
    like the example of an XPath that can make a count is ...
    xtwmx
    @xtwmx
    sure,
    Marcin Grzejszczak
    @marcingrzejszczak
    thanks!
    glad that you like it BTW
    I've created that for Spring Cloud Contract but didn't yet implement a feature there to use this library
    xtwmx
    @xtwmx
    I realised that when I perform assertThat(xml1).node("some").isEqualTo("0") it will try to find a suitable node "some", but for asserting I require that only 1 is present. So without a hasCount method, I need to always do an extra element count for all nodes I expect to be have only 1 instance. This makes code quite messy.
    Marcin Grzejszczak
    @marcingrzejszczak
    yeah definitely
    xtwmx
    @xtwmx
    I didn't realise this before as JSON does not allow duplicate keys unlike xml
    Marcin Grzejszczak
    @marcingrzejszczak
    indeed
    xtwmx
    @xtwmx
    the beautiful way would be have assertThat(xml1).node("some").hasCount(1).isEqualTo("0") or smth like that
    Marcin Grzejszczak
    @marcingrzejszczak
    sure - can you update the issue with a proposal of API ?
    xtwmx
    @xtwmx
    will do
    xtwmx
    @xtwmx
    Another thing I noticed is that xpath comparison mismatch throws a java.lang.IllegalStateException, which is a test error and not a failure by junit rules. Is this intended?
    com.toomuchcoding.xmlassert.XmlAsserter.check(XmlAsserter.java:197)
    Marcin Grzejszczak
    @marcingrzejszczak
    yeah that is intedened
    cause this library can be used in production code (and actually is used in production code)
    xtwmx
    @xtwmx
    Hi, thanks for count support.
    I have now noticed that exception message is shown twice before stacktrace
    at com.toomuchcoding.xmlassert.XmlAsserter.check(XmlAsserter.java:201)
    at com.toomuchcoding.xmlassert.XmlAsserter.checkBufferedXPathString(XmlAsserter.java:220)
    at com.toomuchcoding.xmlassert.XmlAsserter.isEqualTo(XmlAsserter.java:98)
    java.lang.IllegalStateException: Parsed XML [] doesn't match the XPath <>
    java.lang.IllegalStateException:
    Parsed XML [] doesn't match the XPath <>
    at com.toomuchcoding.xmlassert.XmlAsserter.check(XmlAsserter.java:201)
    at com.toomuchcoding.xmlassert.XmlAsserter.checkBufferedXPathString(XmlAsserter.java:220)
    at com.toomuchcoding.xmlassert.XmlAsserter.isEqualTo(XmlAsserter.java:98)
    Not a big problem, but a hindrance if you want to quickly locate the problem in xml
    xtwmx
    @xtwmx
    interesting, it might be a problem with maven build logs instead - it is doesn't always duplicate
    Marcin Grzejszczak
    @marcingrzejszczak
    interesting
    so is there a bug or no :P ?
    xtwmx
    @xtwmx
    I need to run whole build locally to be sure it is not a problem only in teamcity as it currently seems to be.
    I will let you know tommorrow
    Marcin Grzejszczak
    @marcingrzejszczak
    ok!
    xtwmx
    @xtwmx
    I confirmed that the problem is only with my integration server (teamcity) build logs. Does not happen locally, probably some component is old there.
    Even the Allure there I use for reporting, shows correct exception. Might be internal teamcity problem.
    Marcin Grzejszczak
    @marcingrzejszczak
    interesting, thanks for the update!
    MeSaNei
    @MeSaNei
    Hello! I just started to use XML Assert and have a problem with node that contains ':'(colon). My xpath is /xml-fragment/diffgr:diffgram/BankPapers/BP[PCode='41100354B']. I got the exception Unknown prefix: diffgr. Should I register the namespace somehow? Or is it any other way to resolve this? Thank you.
    Marcin Grzejszczak
    @marcingrzejszczak
    Hi! Can you file an issue about this with an example?
    MeSaNei
    @MeSaNei
    sure
    MeSaNei
    @MeSaNei
    Done
    Marcin Grzejszczak
    @marcingrzejszczak
    Thanks