Forum Discussion
I came across some interesting results when attempting to reverse the process and move parameters back into SOAPUI.
My setup requires me to call a SOAPUI TestCase containing REST calls, then Selenium, then finally a second TestCase to finalise the whole process.
Whilst I observed parameters/properties transferring between TestCase's within SOAPUI GUI (by setting my parameters at a project level (or suite level)) , the same wasn't true when executing the TestCase's separately from Java, i.e. making two runner.run(); calls. Its almost as if the values created from the first run are lost to memory, when you re-invoke runner.run();
Anyways, I managed to solve the issue, by pulling out the values from the first TestCase into java using my runAPI method and then inserting them back into the project using my setAPIproperties method.
I also observed recreating the SoapUITestCaseRunner object between calls is a big no no because you lose continuity.
Anyways here is the code below:
Pulling data out of SOAPUI into Java and then from Java back into SOAPUI.
Calling Test code
public void test() throws Throwable { //create the SoapUITestCaseRunner object once loadAPI(SOAPprojectDir + "PAYPAL_API.xml"); //Run against first TestCase List<String> logs = runAPI("PAYPAL_PAYMENT_TESTCASE","setPayment"); //Show the variables java grabbed from SOAPUI showRunAPIlog(logs); //save the strings from SOAPUI log accessToken = logs.get(0); confirmURL = logs.get(2); executeURL_API = logs.get(3); //put the paramters/variables back into SOAPUI for the second TestCase call setAPIproperties("access_token="+accessToken,"execute_url="+executeURL_API); //run second TestCase call runAPI("PAYPAL_PAYMENT_TESTCASE","executePayment"); }
Supporting methods
import org.testng.*; import com.smartbear.ready.cmd.runner.SoapUITestCaseRunner; import java.util.*; import java.util.regex.*; import java.io.*; public class config { SoapUITestCaseRunner runner; public void loadAPI(String projectXML){ runner = new SoapUITestCaseRunner(); runner.setProjectFile(projectXML); runner.setPrintReport(true); } public List<String> runAPI(String setTestSuite, String setTestCase) { ByteArrayOutputStream baos = null; PrintStream newPrintStream = null; PrintStream oldPrintStream = null; try{ //========================================================================== //redirect console output so it can be saved to a string and queried later. //========================================================================== // new stream to hold console output baos = new ByteArrayOutputStream(); newPrintStream = new PrintStream(baos); // Save the old stream oldPrintStream = System.out; // Set Java to use new stream System.setOut(newPrintStream); //========================================================================== //run SOAPUI test //========================================================================== System.out.println("===== SOAP UI LOG (START) ====="); runner.setTestSuite(setTestSuite); runner.setTestCase(setTestCase); runner.run(); }catch(Exception e){ //If test fails, fail build, and show SOAPUI exception. Assert.fail(e.getMessage()); //finally block required to redirect Java back to old console output }finally{ //========================================================================== //Save redirected output and put console output back to normal //========================================================================== System.out.flush(); System.setOut(oldPrintStream); //========================================================================== //Show and then return the console output to calling method //========================================================================== System.out.println(baos.toString()); } List<String> logs = new ArrayList<String>(); String StringStart = "[log] "; String StringEnd = "\r"; Pattern p = Pattern.compile(Pattern.quote(StringStart) + "(.*?)" + Pattern.quote(StringEnd)); Matcher m = p.matcher(baos.toString()); while (m.find()) { logs.add(m.group(1)); } //if method is succesful, then SOAPUI logs will be pulled and return to calling method. return logs; } //Use this to output to console all the logs returned by runAPI public void showRunAPIlog(List<String> logs) { System.out.println("#### showRunAPIlog results below ####"); for (String a: logs){ System.out.println(a); } } public void setAPIproperties(String...properties) { //Example use: //setAPIproperties("access_token="+accessToken,"execute_url="+executeURL_API); runner.setProjectProperties(properties); } }
Related Content
- 2 months ago
- 3 years ago