Forum Discussion

workpeter's avatar
workpeter
Occasional Contributor
6 years ago

How to: Transfer parameter from SOAPUI to Selenium/Java (Continuous integration solution)

 

 

HI All, 

 

This is my solution for integrating SOAPUI with Selenium. I gave myself a challenge to call the PAYPAL REST service, grab a response parameter and feed it into a Selenium script as a dynamic value to confirm the payment. 

 

The below code I have made a custom method called runAPI. In my particular case it calls a SOAPUI project, which within it contains a few groovy scripts that output to the log using the log.info method.  One of those values I outputted was needed for my Selenium script to pick up.

 

runAPI executes my TestCase, reads the output stream, and then returns a List of strings which were captured with log.info. If your API fails, then it will output the exception/failure message and fail your test/build (as expected).

 

In the output code at the bottom of this post, you can see the output log for SOAPUI generated by SoapUITestCaseRunner. And at the very bottom you can the two string values return by runAPI.

From this i can grab the String below which I can then later dynamically go to using Selenium. Selenium uses this URL to confirm paypal payment.

 

Hope this is useful to anyone, and please let me know if you have your own solution. 

 

https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-62A21529UW0412133

 

 

package testsuites;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.smartbear.ready.cmd.runner.SoapUITestCaseRunner;

public class runme {

	@Test
	public void Test() throws Exception {


		String dir = "C:\\Users\\workpeter\\Documents\\Eclipse-workspace\\SOAPUI\\src\\test\\resources\\";

		//Call the SOAPUI project using my runAPI method, and it returns all the logs to Java
		List<String> logs = runAPI(dir + "PAYPAL_API.xml","PAYPAL_PAYMENT_TESTCASE","setPayment");

		//Show logs to console. If 
		for (String log: logs){
			System.out.println(log);
		}


	}

	
	//=====================================================
	/* 
	  Method will invoke a SOAPUI project and return the log to calling method
	 
	  Example use: 
	  Within SOAPUI you have a groovy step which saves a particular value using log.info
	  That value will be grabbed by this method and outputted. You may wish to then use that 
	  value in a selenium script. Thus this method allows for continous integration between
	  SOAPUI and Selenium/Java. 
	  
	 */ 
	//=====================================================
	
	public List<String> runAPI(String projectXML, 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) =====");

			SoapUITestCaseRunner runner = new SoapUITestCaseRunner(); 
			runner.setProjectFile(projectXML);
			runner.setPrintReport(true);

			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;

	}

}

 

OUTPUT

 

 

 

===== SOAP UI LOG (START) =====
- System property 'soapui.home' is not set! Using this directory instead: .
- No base packages specified - no classpath scanning will be done
- Starting 'LifecycleManager@1317375498'
- Hibernate Validator 4.1.0.Final
- Adding module 'com.smartbear.ready.cmd.CommandLineModule
16:25:21,388 INFO  [license] ReadyAPI 2.2.0 started - System properties: {OS=Windows 10, User name=workpeter}
16:25:21,390 WARN  [ReadyApiPaths] System property 'soapui.home' is not set! Using this directory instead: .
16:25:21,393 WARN  [SoapUIExtensionClassLoader] Missing folder [C:\Users\workpeter\Documents\Eclipse-workspace\SOAPUI\ext] for external libraries
16:25:21,402 INFO  [DefaultSoapUICore] No JDBC drivers found in extensions directory
16:25:21,652 INFO  [DefaultSoapUICore] initialized soapui-settings from [C:\Users\workpeter\soapui-settings.xml]
16:25:21,873 WARN  [ReadyApiPaths] System property 'soapui.home' is not set! Using this directory instead: .
16:25:22,616 WARN  [ReadyApiPaths] System property 'soapui.home' is not set! Using this directory instead: .
16:25:24,591 INFO  [license] ReadyAPI 2.2.0 started - System properties: {OS=Windows 10, User name=workpeter}
16:25:24,591 WARN  [ReadyApiPaths] System property 'soapui.home' is not set! Using this directory instead: .
16:25:24,592 WARN  [SoapUIExtensionClassLoader] Missing folder [C:\Users\workpeter\Documents\Eclipse-workspace\SOAPUI\ext] for external libraries
16:25:24,594 INFO  [DefaultSoapUICore] No JDBC drivers found in extensions directory
16:25:24,598 INFO  [DefaultSoapUICore] initialized soapui-settings from [C:\Users\workpeter\soapui-settings.xml]
16:25:24,612 DEBUG [LicenseManagerServiceImpl] Trying to initialize license for SoapUI
16:25:24,613 DEBUG [LicenseManagerServiceImpl] No license found for SoapUI
16:25:24,619 DEBUG [LicenseManagerServiceImpl] Trying to initialize license for SoapUI
16:25:24,626 INFO  [LicenseManagerServiceImpl] Invalid license for SoapUI: [2018-01-31 00:00:00] soapUI trial - 660f5cac-e0bf-4fb8-a855-21617c8b2e62, issue date = Wed Jan 31 06:00:00 GMT 2018, expiry date = Thu Feb 15 06:00:00 GMT 2018
16:25:24,627 INFO  [LicenseManagerServiceImpl] License expired at Thu Feb 15 06:00:00 GMT 2018
16:25:24,627 INFO  [LicenseManagerServiceImpl] No reserve license found
16:25:24,641 DEBUG [LicenseManagerServiceImpl] Trying to initialize license for LoadUI
16:25:24,641 DEBUG [LicenseManagerServiceImpl] No license found for LoadUI
16:25:24,650 DEBUG [LicenseManagerServiceImpl] Trying to initialize license for Secure
16:25:24,650 DEBUG [LicenseManagerServiceImpl] No license found for Secure
16:25:24,655 DEBUG [LicenseManagerServiceImpl] Trying to initialize license for ServiceV
16:25:24,655 DEBUG [LicenseManagerServiceImpl] No license found for ServiceV
16:25:24,661 WARN  [ReadyApiPaths] System property 'soapui.home' is not set! Using this directory instead: .
16:25:24,663 DEBUG [LicenseManagerServiceImpl] License host for SoapUI: DESKTOP-KQS99BN
16:25:24,671 DEBUG [LicenseManagerServiceImpl] License host for Secure: DESKTOP-KQS99BN
16:25:24,671 DEBUG [LicenseManagerServiceImpl] License host for LoadUI: DESKTOP-KQS99BN
16:25:24,677 DEBUG [LicenseManagerServiceImpl] License host for ServiceV: DESKTOP-KQS99BN
16:25:24,663 DEBUG [LicenseManagerServiceImpl] License host for SoapUI: DESKTOP-KQS99BN
16:25:24,799 INFO  [WsdlProject] Loaded project from [file:/C:/Users/workpeter/Documents/Eclipse-workspace/SOAPUI/src/test/resources/PAYPAL_API.xml]
16:25:25,170 INFO  [PluginManager] Adding plugin from [C:\Users\workpeter\.soapui\plugins\ready-mqtt-plugin-dist.jar]
16:25:25,172 INFO  [PluginManager] Adding plugin from [C:\Users\workpeter\.soapui\plugins\ready-uxm-plugin-1.0.2.jar]
16:25:25,177 WARN  [ReadyApiPaths] System property 'soapui.home' is not set! Using this directory instead: .
16:25:25,178 INFO  [PluginManager] Adding plugin from [C:\Users\workpeter\.soapui\plugins\ready-api-git-plugin-1.1.0.jar]
16:25:25,537 WARN  [errorlog] The Groovy debugger is disabled.
16:25:25,581 WARN  [WsdlProject] Failed to find the "Default" environment. The default environment will be used.
16:25:25,606 INFO  [SoapUITestCaseRunner] Running tests in the project [REST Project - PayPal Payment]
16:25:25,615 INFO  [SoapUITestCaseRunner] Running TestCase [setPayment]
16:25:25,631 INFO  [SoapUITestCaseRunner] Running TestCase [setPayment]
16:25:25,632 INFO  [SoapUITestCaseRunner] running step [getTokenBearer]
16:25:25,834 INFO  [PluginManager] Adding plugin from [C:\Users\workpeter\.soapui\plugins\ready-jira-plugin-1.1-dist.jar]
16:25:25,842 INFO  [PluginManager] 4 plugins loaded in 766 ms
16:25:25,842 INFO  [DefaultSoapUICore] All plugins loaded
16:25:26,605 WARN  [ResponseProcessCookies] Invalid cookie header: "Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.API.1%26silo_version%3D1880%26app%3Dapiplatformproxyserv%26TIME%3D3814266203%26HTTP_X_PP_AZ_LOCATOR%3Dsandbox.slc; Expires=Mon, 04 Jun 2018 15:55:23 GMT; domain=.paypal.com; path=/; Secure; HttpOnly". Invalid 'expires' attribute: Mon, 04 Jun 2018 15:55:23 GMT
16:25:26,606 WARN  [ResponseProcessCookies] Invalid cookie header: "Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT". Invalid 'expires' attribute: Thu, 01 Jan 1970 00:00:01 GMT
16:25:27,034 INFO  [SoapUITestCaseRunner] Assertion [Valid HTTP Status Codes] has status VALID
16:25:27,034 INFO  [SoapUITestCaseRunner] Assertion [Script Assertion - Verify access token exists] has status VALID
16:25:27,035 INFO  [SoapUITestCaseRunner] running step [save TokenBearer into a testcase parameter]
16:25:27,175 INFO  [log] (access_token): A21AAETTgqwt9lQIwNSvGqi-UZeNipW6c9LoGisOSx96Qqx9rEIlE36pzb9-1mfTK-W20qV9llyqy-NSIxQqrYg3rwruVoEMg
16:25:27,184 INFO  [SoapUITestCaseRunner] running step [createPayement]
16:25:28,237 WARN  [ResponseProcessCookies] Invalid cookie header: "Set-Cookie: X-PP-SILOVER=name%3DSANDBOX3.API.1%26silo_version%3D1880%26app%3Dapiplatformproxyserv%26TIME%3D3847820635%26HTTP_X_PP_AZ_LOCATOR%3Dsandbox.slc; Expires=Mon, 04 Jun 2018 15:55:25 GMT; domain=.paypal.com; path=/; Secure; HttpOnly". Invalid 'expires' attribute: Mon, 04 Jun 2018 15:55:25 GMT
16:25:28,237 WARN  [ResponseProcessCookies] Invalid cookie header: "Set-Cookie: X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT". Invalid 'expires' attribute: Thu, 01 Jan 1970 00:00:01 GMT
16:25:28,279 INFO  [SoapUITestCaseRunner] Assertion [Script Assertion] has status VALID
16:25:28,279 INFO  [SoapUITestCaseRunner] running step [save payerConfirmURL into a testcase paramter]
16:25:28,330 INFO  [log] https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-62A21529UW0412133
16:25:28,345 INFO  [SoapUITestCaseRunner] Finished running TestCase [setPayment], time taken: 2002ms, status: FINISHED
16:25:28,348 INFO  [SoapUITestCaseRunner] TestCase [setPayment] finished with status [FINISHED] in 2002ms

ReadyAPI 2.2.0 TestCaseRunner Summary
-----------------------------
Time Taken: 2742ms
Total TestSuites: 0
Total TestCases: 1 (0 failed)
Total TestSteps: 4
Total Request Assertions: 3
Total Failed Assertions: 0
Total Exported Results: 0

 (access_token): A21AAETTgqwt9lQIwNSvGqi-UZeNipW6c9LoGisOSx96Qqx9rEIlE36pzb9-1mfTK-W20qV9llyqy-NSIxQqrYg3rwruVoEMg
 https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-62A21529UW0412133

 

 

1 Reply

  • workpeter's avatar
    workpeter
    Occasional Contributor

     

    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); 
    
    
    	}
    }