admin管理员组

文章数量:1415491

i use this script groovy in my soap test , i want to ignore the result of this attribut in my reponse

ns4:Period ns4:firstDate2020-09-22T13:12:23</ns4:firstDate> </ns4:Period>``

**Script Assertion : **

import groovy.xml.XmlUtil
import groovy.xml.XmlParser
import .xmlunit.builder.DiffBuilder
import .xmlunit.builder.Input
import .xmlunit.diff.DefaultNodeMatcher
import .xmlunit.diff.Diff
import .xmlunit.diff.ElementSelectors
import .xmlunit.diff.MultiLevelByNameAndTextSelector

def anonymize(String current, List<String> fieldsToAnonymize) {
    Node node = new XmlParser().parseText(current)
    anonymizeField(node, fieldsToAnonymize, null)
    return XmlUtil.serialize(node)
}

def anonymizeField(Node node, List<String> fieldsToAnonymize, String previousLevel) {
    String currentLevel = previousLevel == null ? node.name() : previousLevel + "/" + node.name()
    NodeList children = (NodeList) node.value()
    for (def child : children) {
        if (child instanceof Node) {
            anonymizeField((Node) child, fieldsToAnonymize, currentLevel)
        } else if (fieldsToAnonymize.contains(currentLevel)) {
            log.info("contentAnonymize for: " + currentLevel)
            node.setValue("")
        }
    }
}

def compare(String current, String expected) {
    Diff myDiff = DiffBuilderpare(Input.fromString(current))
            .withTest(Input.fromString(expected))
            .checkForSimilar()
            .normalizeWhitespace()
            .withNodeMatcher(new DefaultNodeMatcher(new MultiLevelByNameAndTextSelector(2), ElementSelectors.byNameAndAllAttributes))
            .build()

    if (myDiff.hasDifferences()) {
        log.info(myDiff.toString())
    }

    assert !myDiff.hasDifferences()
}

String expected = context.expand('${#TestCase#Request-2')
String fieldsToAnonymize = context.expand('${#TestCase#Request-2_fieldsToAnonymize}')
String current = messageExchange.getResponseContentAsXml()
String currentAnonymize = anonymize(current, Arrays.asList(fieldsToAnonymize.split(",")))
String expectedAnonymize = anonymize(expected, Arrays.asList(fieldsToAnonymize.split(",")))
log.info("actual  : " + currentAnonymize)
log.info("expected: " + expectedAnonymize)
compare(currentAnonymize, expectedAnonymize)

I have test to add this code in my script to ignore the attribut result , but does not work:

import com.eviware.soapui.support.XmlHolder

xmlHolder["//ns4:startDateTime"] = ""

when i execute the test i want to ignore the result of this attribut :


<ns4:Period>
   <ns4:firstDate>2020-09-22T13:12:23</ns4:firstDate>
</ns4:Period>``

any idea !!

本文标签: javaSOAPUITNRIgnore the result of an attribut in my response soapStack Overflow