somo debe
somo debe

Reputation: 1

How Can i pass result from a method in groovy to the body of a request in SOAPUi

Can i start by saying i am new to programming and SoapUI(more like a week old). i apologies in advance for asking the question(s) i am about to ask.

Problem

basically, i am trying to automate webservice. I need to create xml using Groovy in soapui and send it as part of a request body to a webservice(REST not SOAP) and do some assertion on the response received. i will be sending quite a few request, hence the reason to automate.

My proposed solution (which i am not sure about)

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.Method

class XmlGenerator {

  // I created a class i.e. called XmlGenerator with a
  // static method called GetXML() which looks like this:
  public static def GetXML() 
  {
    def  writer = new StringWriter()
    def  xml = new MarkupBuilder(writer)
    xml.GetDMessage() //creating the xml message
    {
      PrescribedItems 
      {
        PrescribedMethod(xml,'Some Value','Some Value','Some Value')
        PrescribedItem
        {
          PrescribedMethod(xml,'Some Value','Some Value','Some Value')
        }
      }
    }
    return writer
  }

  // This method creates the XML needed but then i need to
  // pass the xml generated to a request boy; so i
  //create another method within the same class:  
  static def postXML(String baseUrl, String path)
  {
    def RequestBody = XmlGenerator.GetXML()  // i am not sure if this will work
    try 
    {
      def ret = null
      def http = new HTTPBuilder(baseUrl)
      http.request(Method.POST, ContentType.XML) 
      {
        uri.path = path
        body = RequestBody
      }
    }
    catch (groovyx.net.http.HttpResponseException ex) 
    {
      ex.printStackTrace()
      return null
    }
    catch (java.net.ConnectException ex) 
    {
      ex.printStackTrace()
      return null
    }
  }
}

Summary

A class called XmlGenerator() with 2 methods; GetXML() (used to generate XML ) and postXML() (Used to send XML generated by GetXML() to the Webservice.)

Questions

  1. How can i make sure that this 2 methods ( GetXML() and postXML() ) are used by other request messages i.e. do i need to import the groovy scripte.g. do i do an import ... GroovyScriptName , if yes, please how?

  2. How can i create create the xml and run the request in subsequent request messages. For instance; do i do this; XmlGenerator() gen = new XmlGenerator(), then do gen.GetXML() and gen.postXML() to create and run the request. Also, what role can testRunner play in all this

  3. Running the code currently throws up HTTPBuilder, ContentType , Method can not be resolved even though i have imported them in the script (see above)

  4. Finally, what role can property play in building this framework? bearing in mind that the each request will be independent from the other request i.e. nothing is been passed around from one request to the other during test execution

Upvotes: 0

Views: 2937

Answers (1)

drond
drond

Reputation: 435

You can specify the REST request you are testing using standard soapui functionality, as outlined here:

http://www.soapui.org/REST-Testing/getting-started.html

Follow the steps in the above page to create a:

  1. New REST Service
  2. New REST Resource
  3. New REST Method

Then you can add a REST Test Step to your Test Case that invokes that request.

You can then insert a Groovy Test Step before the REST Test Step to build your xml body; here's a simple example of building a XML string:

import groovy.xml.MarkupBuilder

context.writer = new StringWriter()
def xml = new MarkupBuilder(context.writer)
xml.books() {
  book(name:'blue ocean') {
    format ('paperback')
    year ('2010')
  }
  book(name:'quicksilver') {
    format ('hardback')
    year ('2011')
  }
}

Note that the XML is assigned to a context variable (context.writer). This makes it available for use in subsequent steps within the scope of the Test Case for the duration of the test run.

Then, insert the following code into the body of your REST Test Step:

${=context.writer}

Finally, you have the option of adding Assertions within the REST Test Step; here's some information on that:

http://www.soapui.org/Functional-Testing/getting-started-with-assertions.html

Upvotes: 3

Related Questions