Forum Discussion
eeyoreeco
11 years agoOccasional Contributor
PaulM wrote: Sure thing, I will set up a quick example script that you should be able to use that will perform these actions.
//Setting up variables here.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
//Change "testName" to the name of the test step you are wanting to perform this on
def holder = groovyUtils.getXmlHolder("testName#Request");
def xml = holder.getPrettyXml();
//This regexp grabs all child nodes that have a variable. Note for some odd reason, it leaves the '?' in the request and it also will not make any changes to any null or empty child nodes. The idea here is to start with a valid request
def regexp = ("<([^<>]+)>(.*?)</\\1>");
def matcher = xml =~ regexp;
//Change testName here again. It might be a better idea to set this as a property or variable in the script.
def controlledStep = testRunner.testCase.getTestStepByName("testName");
//This section iterates through each of the regex matches
matcher.each {
//Here we are taking the match of the XML opening tag and the correct data and replacing it with the XML opening tag and the string "BadData". For example, if you use "<XML>hey!</XML>" it will replace it with "<XML>BadData</XML>". If you want specific data in each cell just use it[1] (The opening XML tag) to grab a property named with it.
def newXml = xml.replaceAll("<"+it[1]+">"+it[2],"<"+it[1]+">"+"BadData");
//Putting the bad data into the testStep's request
controlledStep.setPropertyValue("Request",newXml);
//Running the testStep. Again, replace testName.
def result = testRunner.runTestStepByName("testName");
//I am just logging out the assertion. You can do anything you want here.
log.info controlledStep.getAssertionStatus().toString();
}
//After it loops through every single child node, replacing only the values of that child node. I do not want to completely destroy the test step so I put the initial XML back into the test step. Always clean up your mess.
controlledStep.setPropertyValue("Request",xml);
If you need further assistance please ask. I commented it thoroughly so that you can hopefully see what is going on with the script. It's pretty simple overall.
Thanks Paul! I'll give a try for this one. Have a nice day!