Extracting Optional elements from response

LegacyForumLegacyForum Posts: 1,669 ✭✭
edited December 2016 in SOAtest
Hi,
I am testing a service which returns some optional elements in response traffic. I want to extract them in a method and kick off other tests based on the extracted values.

Any suggestions on how can I achieve that?
Tagged:

Comments

  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    Hi ash1,

    Does the scenario require that it be done with a Method Tool? Remember that you can use an XML Data Bank to write to a Writable Data Source. By default, if the element doesn't exist (is optional and not specified) then nothing will be sent to the Writable DS. When this is all done, then you can parametrize another test against the Writable DS.
  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    Hi Brian,
    Thanks for your response.

    Actually, I want to extract that optional element and set test variable to true/false. Then there is another test that I want to mark to run it conditionally depending on the variable value. Could you please suggest how do I achieve this using writable DS or a Method tool?

    Ash

    Hi ash1,

    Does the scenario require that it be done with a Method Tool? Remember that you can use an XML Data Bank to write to a Writable Data Source. By default, if the element doesn't exist (is optional and not specified) then nothing will be sent to the Writable DS. When this is all done, then you can parametrize another test against the Writable DS.

  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    Hi Ash,

    You could attach an XML Transformer to the SOAP Client, which extracts the optional element. Then, chain the Method Tool to the XML Transformer with a method like:
    CODE
    from com.parasoft.api import *
    from java.lang import *

    def myFunc(input, context):
        runTest = context.getVariable("runTest")
        value = String(str(input))
        if (not value.startsWith("XML input did not have XPath item")):
            runTest.setValue("True")
        return 1

    The runTest variable here is the boolean Test Suite Variable that you want to conditionally set. Note that this function always returns 1 - you will want to enable the "Return Value indicates success" checkbox, so that the XML Transformer doesn't cause the test to fail when the optional element is not found.
  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    If you want to extract "true" or "false" based on whether an element exists in XML, you can use in the XML Data Bank an XPath that checks if the element exists without using any scripting:

    count(/path/to/element/of/interest) > 0

    This stores in your extracted variable the value "true" if /path/to/element/of/interest returns one or more nodes; otherwise the extracted variable is set to "false". If you want to check that exactly one matching element exists, then you can change "> 0" to "= 0".

    Alternatively if you want to return values other than "true" and "false" based on the existence of an element, then you can use the "if" statement now that SOAtest supports XPath 2.0:

    if (count(/path/to/element/of/interest) > 0) then "valueIfExists" else "valueIfDoesNotExist"
Sign In or Register to comment.