cancel
Showing results for 
Search instead for 
Did you mean: 

How to create REST project using Groovy script?

Highlighted
Occasional Contributor

How to create REST project using Groovy script?

My requirement:  Get the URI/WADL as input from user, create the new REST project and generates the TestSuite and Test Case for each resource.

1. is that possible to do using groovy scripting/soapUI APIs ?

2.if yes kindly tell me the way how i can achive this?

 

 

10 REPLIES 10
Highlighted
Valued Contributor

Re: How to create REST project using Groovy script?

Hi,

I would expect that to create a REST project, TestSuite and TestCase from a WADL is certainly possible using Groovy Script, but before going any further, given that you say this is based on a URI/WADL that the user provides, may I ask:

a) Why you want to do this and why the usr cannot just use the menu driven functionality e.g. new REST project from WADL?

b) Is it that you want to generate the project on the fly and have many different WADLs to use? If you just want to vary the REST service endpoint / request values of the tests, then there are easier ways to achieve this.

Cheers,

Rupert

 

 

Author of SoapUI Cookbook
Highlighted
Occasional Contributor

Re: How to create REST project using Groovy script?

 

Thanks for the quick response

 

a) I will have GUI for user to provide the URI / WADL as input. based on that it generates TestSuites,Testcase automatically.

b) Generate the new  REST Project and override the endpoint/request params.

Highlighted
Valued Contributor

Re: How to create REST project using Groovy script?

Ok, no problem.

 

for a) I take it you know how to get the GUI to generate a REST project / TestSuite / TestCase based on the WADL/URI? e.g. File New REST Project > Import WSDL (or use URI) etc and generate TestSuite, TestCase and REST Test Request Test Step? Sorry if this is not what you meant.

 

for b) You would need to parameterise (using property expansions) the enpoint / request params for the REST Test Request TestStep - to do this, have you seen this other questsion & answer that came up today it covers this topic:

 

http://community.smartbear.com/t5/SoapUI-Open-Source/Can-resource-path-of-a-REST-API-be-changed-by-d...

 

Once parameterised, you can then provide the values for the endpoint/request from anywhere you like e.g. files, databases, other service responses, property files etc.

 

Is this the sort of thing you meant?

 

Cheers,

Rup

 

 

 

 

Author of SoapUI Cookbook
Highlighted
Occasional Contributor

Re: How to create REST project using Groovy script?

Hi Rup,

 

a) Yes i know this option avilable in soapUI GUI. But my question here is how i can do this using groovy script. (e.g ) just executing the .bat file will call the groovy script and create the project.xml in specified location. all the inputs (URI/WADL, project name, path) will be passed as parameters.

Highlighted
Valued Contributor

Re: How to create REST project using Groovy script?

Hi,

 

Ok, sorry, I did wonder if that was what you meant i.e. generate the whole project etc from a command line.

 

Its definately an interesting question, but unfortunately apart from feeling that it is very doable with Groovy (as I have done / seen bits od scripts like this before),  I don't have the script yet and it might take me a while - maybe someone else can jump in if they have such a script?

 

I did see this post where on page 2 the guy has a Groovy script to create the TestSteps dynamically (but not the project):

http://community.smartbear.com/t5/SoapUI-NG/Resolved-Problem-with-creating-REST-TestRequests-via-Gro...

 

I must admit I am quite curious to know why you need to generate the whole thing? As I have always been able to simply create the project / TestSuite / TestCase / Steps manually, parameterise all the aspects that need to be dynamic as property expansions and then pass the poperty vales when running from the command like script - do you need to generate everything on the fly as part of some kind of CI process?

 

Sorry not to have a quick answer,

Cheers,

Rup

 

Author of SoapUI Cookbook
Highlighted
Occasional Contributor

Re: How to create REST project using Groovy script?

It’s ok Rup. Yes I need to generate the whole project from command line and execute as part of the CI process.  If you can get me the code it will be very useful…

Highlighted
Valued Contributor

Re: How to create REST project using Groovy script?

Ok, no problem.

I will put this on my list of things to do. I did wonder about it some time back, but didn't know how useful it would be. When I crack it I'll share the script with you so you can test it, I might even do a quick blog on it.

Let me know if you crack it first please.

Cheers,

Rup

Author of SoapUI Cookbook
Highlighted
Occasional Contributor

Re: How to create REST project using Groovy script?

Sure Rup. Thank you so much

Highlighted
Valued Contributor

Re: How to create REST project using Groovy script?

Hi,

 

I managed to get a chance to work a little on what we discussed... here is my first quick shot and results, please let me know what you think:

 

Create a Groovy script with the following content (note if you do this in Eclipse, you'll need to reference all the SoapUI libs):

 

import com.eviware.soapui.impl.actions.RestServiceBuilder

import com.eviware.soapui.impl.rest.RestRequest

import com.eviware.soapui.impl.wsdl.WsdlProject

import com.eviware.soapui.impl.wsdl.WsdlSubmitContext

import com.eviware.soapui.model.iface.Response

import com.eviware.soapui.model.iface.Submit

 

def uri = "http://jsonplaceholder.typicode.com/posts/1"

 

WsdlProject project = new WsdlProject()

RestServiceBuilder serviceBuilder = new RestServiceBuilder()

serviceBuilder.createRestService(project, uri)

 

println "service name: "+project.getInterfaceList()[0].name

println "Resource: "+project.getInterfaceList()[0].getOperationList()[0]

 

RestRequest request = project.getInterfaceList()[0].getOperationList()[0].getRequestList()[0]

Submit submit = (Submit) request.submit( new WsdlSubmitContext(), false );

Response response = submit.getResponse();

String responseContent = response.getContentAsString();

println( responseContent );

 

Running this gives:

 

2015-05-15 14:01:13,769 [main] WARN  com.eviware.soapui.SoapUI - Could not find jfxrt.jar. Internal browser will be disabled.

14:01:14,606 WARN  [SoapUI] Missing folder [/Users/bearsoftware/Documents/workspace-sts-soapui-testarea/TestSoapUIScripts/ext] for external libraries

14:01:15,565 INFO  [DefaultSoapUICore] initialized soapui-settings from [/Users/bearsoftware/soapui-settings.xml]

14:01:15,755 INFO  [HttpClientSupport$Helper] Initializing KeyStore

service name: http://jsonplaceholder.typicode.com

Resource: RestResource: /posts/1

14:01:19,242 DEBUG [HttpClientSupport$SoapUIHttpClient] Attempt 1 to execute request

14:01:19,242 DEBUG [SoapUIMultiThreadedHttpConnectionManager$SoapUIDefaultClientConnection] Sending request: GET /posts/1 HTTP/1.1

14:01:19,635 DEBUG [SoapUIMultiThreadedHttpConnectionManager$SoapUIDefaultClientConnection] Receiving response: HTTP/1.1 200 OK

14:01:19,640 DEBUG [HttpClientSupport$SoapUIHttpClient] Connection can be kept alive indefinitely

{

  "userId": 1,

  "id": 1,

  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",

  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et **bleep**\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"

}

14:01:51,851 DEBUG [SoapUIMultiThreadedHttpConnectionManager$SoapUIDefaultClientConnection] Connection closed

 

The script:

  1. Creates a new REST Project based on the highlighted URI (a public test REST API)
  2. A GET request to the resource /posts/1 is automatically created.
  3. The request is dispatched and the response JSON content printed out.

When creating the project on-the-fly like this there is no need to persist anything or use a TestRunner script - is this allong the lines you need? 

 

Cheers,

Rup

Author of SoapUI Cookbook
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors