Forum Discussion

Sebastian_Vilst's avatar
Sebastian_Vilst
Contributor
16 years ago

How to assert "this" OR "that"

I'd like to assert that a webservice return one of two valid responses.

Response one is a valid "Custemer created" and the other is and error "Customer already exists".
If it's either of those two, then i'd like to have a "green light"

How do i do that?

6 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    you should be able to use the OR operator (|) in the condition clause of your xpath-expression!? Can you show the response message so I can try to give you an example?

    regards,

    /Ole
    eviware.com
  • Xpath is a possibility, but it gets rather complicated since it's two different possible response structures I'm checking in.

    Also, I'd like to teach a solution to our testers and they are not developers and cannot be expected to fully understand xpath.

    Example of a succesful response where i want to check for "Kunden eksisterer allerede":

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Body>
          <s:Fault>
            <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
            <faultstring xml:lang="da-DK">Kunden eksisterer allerede</faultstring>
            <detail xmlns="http://www.w3.org/2003/05/soap-envelope" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
                <CustomerExists xmlns="http://ementor.dk/tas/faults/customer/2008/02/22" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                  <CorrelationId i:nil="true" xmlns="http://ementor.dk/tas/faults/core/2007/10/29"/>
                  <ErrorId xmlns="http://ementor.dk/tas/faults/core/2007/10/29">-1</ErrorId>
                  <ErrorMessage xmlns="http://ementor.dk/tas/faults/core/2007/10/29">Kunden eksisterer allerede</ErrorMessage>
                </CustomerExists>
            </detail>
          </s:Fault>
      </s:Body>
    </s:Envelope>


    and the alternative error response where I want to check for a certain number in the customerPrimaryNumberField:


    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Body>
          <CustomerCreateResponse xmlns="http://ementor.dk/tas/esb/customer/2007/11/06">
            <CustomerCreateResult xmlns:a="http://schemas.datacontract.org/2004/07/Ementor.TAS.DataContracts.ESBCustomerServiceDataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <a:customerKeyStructureField>
                  <a:customerPrimaryNumberField>10820873</a:customerPrimaryNumberField>
                  <a:customerPrimaryTypeNameField>companyname</a:customerPrimaryTypeNameField>
                  <a:customerSecondaryNumberField i:nil="true"/>
                  <a:customerSecondaryTypeNameField>none</a:customerSecondaryTypeNameField>
                </a:customerKeyStructureField>
                <a:lastUpdatedTimeStampField>2009-02-24T10:17:48.365</a:lastUpdatedTimeStampField>
            </CustomerCreateResult>
          </CustomerCreateResponse>
      </s:Body>
    </s:Envelope>


    The problem is that both of these responses are fine, even though one of them is an error. It's simply telling me that the customer already exists which is okay for me as I need to be able to run any test multiple times, without getting "red lights"

    Hmm, maybe this is really a feature request then...


    I was thinking of something like two simple CONTAINS assertions, but with and OR between them.
    Or expanding the CONTAINS assertion by allowing me to put several words in it with some.
    or maybe makeing a new type of assertion: CONTAINS_WITH_OR that has multiple fields in the popup.
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    You can do this already with the Contains assertion, if you use a regular expression. Simply check the box "Use token as Regular Expression" and modify the string to be a regular expression which can contain OR, or many other advanced constructs. For example, you could write something along the lines of:

    (?s).*(Kunden eksisterer allerede|<a:customerPrimaryNumberField>[1-9][0-9]*</a:customerPrimaryNumberField>).*

    For more information on the supported syntax of Regular Expressions: http://www.jdocs.com/javase/7.b12/java/util/regex/Pattern.html.

    /Dain
    eviware support
  • Great!
    Thanks I should have noticed the regular expression checkbox myself, but sometimes it's hard to see the forest for all the trees...
  • I got it to work, thanks to your help

    My final regex was


    (?s).*(Kunden eksisterer allerede|CustomerGetResponse).*


    The (?s) bit was required, but i can't figure out why. What does it do? Why do I need it?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    The "(?s)" puts the Java regular expression engine into DOTALL mode, where a dot (".") can match any character, including newline, which it otherwise does not match against, causing your regex to fail.

    Regards,
    Dain
    eviware support