Forum Discussion
Matt_Johnson
16 years agoNew Contributor
Thank you for your response. Through lots of trial and error (8 hours worth) I was able to figure out how to clone an existing operation. Which is what I really needed anyways. Now I am having trouble adding an assertion. I don't get to copy this one. I am pasting all my code for any others down the road that stumble upon this thread.
import java.io.IOException;
import org.apache.xmlbeans.XmlException;
import com.eviware.soapui.impl.wsdl.WsdlProjectPro;
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapFaultAssertion;
import com.eviware.soapui.model.ModelItem;
import com.eviware.soapui.model.testsuite.TestCase;
import com.eviware.soapui.model.testsuite.TestProperty;
import com.eviware.soapui.model.testsuite.TestStep;
import com.eviware.soapui.model.testsuite.TestSuite;
import com.eviware.soapui.support.SoapUIException;
/**
* Opens an existing SoapUI project. Clones TestSteps which are marked '-correct' and creates various auto steps.
* @author mjohnson
*
*/
public class AutoProject {
private String projectFile;
private WsdlProjectPro project;
/**
* Constructs object to prepare for modifying the file.
* @param projectFile Location of the project file
*/
public AutoProject(String projectFile) {
this.projectFile = projectFile;
openProject();
}
/**
* Performs the actions against the file
*/
public void run() {
for (TestSuite testSuite : project.getTestSuiteList()) {
for (TestCase testCase : testSuite.getTestCaseList()) {
//remove any autos that have been added
cleanAutos((WsdlTestCase) testCase);
for (TestStep testStep : testCase.getTestStepList()) {
String name = testStep.getName();
//look for the test step to clone
if (name.endsWith("-correct")) {
generateAutos((WsdlTestStep)testStep);
}
}
}
}
}
/**
* Saves the changes made to the file
*/
public void commit() {
try {
project.save();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Opens project and handles errors
*/
private void openProject() {
try {
project = new WsdlProjectPro(projectFile);
} catch (XmlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SoapUIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Removes all existing test steps marked with '(auto)'
* @param testCase test case to parse through
*/
private void cleanAutos(WsdlTestCase testCase) {
for (TestStep testStep : testCase.getTestStepList()) {
WsdlTestStep wsdlTestStep = (WsdlTestStep) testStep;
String name = wsdlTestStep.getName();
if (name.endsWith("(auto)")) {
testCase.removeTestStep(wsdlTestStep);
}
}
}
/**
* Generates the "auto" steps
* @param testStep the test step found with the '-correct' label
*/
private void generateAutos(WsdlTestStep testStep) {
setupSQLInjectSqlString(testStep);
}
/**
* Creates a test step with sql in the username. This is designed to test against sql injection
* @param correctStep
*/
private void setupSQLInjectSqlString(WsdlTestStep correctStep) {
WsdlTestStep newStep = cloneStep(correctStep, "sql string");
//fix request
TestProperty request = newStep.getProperty("Request");
String xml = request.getValue();
xml = setUsername(xml, "select * from usertable");
xml = setPassword(xml, "1");
//fix assertions
for (ModelItem assertion : newStep.getChildren()) {
if (assertion.getClass().getName().equalsIgnoreCase("NotSoapFaultAssertion")) {
newStep.getChildren().remove(assertion);
}
}
//need to add a SoapFaultAssertion ??? not sure how from here
}
/**
* Clones a test step
* @param correctStep the step with the mark '-correct'
* @param name the new name of the cloned step. Must be unique to the test case
* @return the cloned test step. This is already attached to the test case
*/
private WsdlTestStep cloneStep(WsdlTestStep correctStep, String name) {
return correctStep.clone(correctStep.getTestCase(), "-" + name + "(auto)");
}
/**
* Uses regex to change the password field
* @param xml xml to modify
* @param newPassword new password
* @return
*/
private String setPassword(String xml, String newPassword) {
return xml.replaceAll("<stor:Password>.*</stor:Password>", "<stor:Password>"+newPassword+"</stor:Password>");
}
/**
* Uses regex to change the username field
* @param xml xml to modify
* @param newUsername new username
* @return
*/
private String setUsername(String xml, String newUsername) {
return xml.replaceAll("<stor:Username>.*</stor:Username>", "<stor:Username>"+newUsername+"</stor:Username>");
}
}