Mai 16 2011

How to create a Grails Web Service with a Selenium Test

Published by at 8:28 PM under Grails,intellij idea

Table of contents


For a Customer Project i had to write a SOAP Web Service (for Interaction with an iPhone App) and for Test Driven Development i needed an easy way to create some Tests. First, i wrote a JUnit Test. Ok it worked, but the problem is that it is not usable with Continuous Integration, because it can only be tested against a running Grails-App. So i decided to use the good old Selenium-Framework.

The Selenium-Framework starts the Grails-App (with the Server) and runs the Tests against it! Now i want to show you the (simple!) solution.

I am using Grails 1.3.7 .

Create the Application

First, you have to add the Plugins “cxf” and “selenium-rc” to your project (in this example the project is named as “GrailsWebServiceWithSeleniumTest“. Here is my configuration-file:


Add this line to your “BuildConfig.groovy” to resolve the correct Maven dependency for the “Apache Httpclient commons“.



Create the Service “” and insert this code:



import javax.jws.WebParam
import javax.jws.WebResult
import javax.jws.WebService
import javax.jws.WebMethod

 * Creates a Web Service with JAX-WS Annotations
 * URL: http://localhost:8080/GrailsWebServiceWithSeleniumTest/services/myTest?wsdl
 * @author Siegfried Bolz
@WebService(name = "MyTestWebService", targetNamespace = "", serviceName = "MyTestWebServiceName")
class MyTestService {

  // Using Plugin cxf
  static expose = ['cxfjax']

   * Web Service Operation
   * @param firstName String
   * @param lastName String
   * @return message String
  @WebResult(name = "loginResult")
  @WebMethod(operationName = "login")
  String login(@WebParam(name = "firstname") String firstName, @WebParam(name = "lastname") String lastName) {
    return "Hello " + firstName + " " + lastName

This is the Web Service, using JAX-WS-Annotations for the Contract Last approach. As you can see, i declare everything with the Annotations.

Now start the Grails Application, you can browse to it at:


Created WSDL

Created WSDL

You can use Tools like “soapui” to create sample Requests for testing.

Create the Selenium Test

The next step is to create the Selenium Test. In Grails there is now the new Directory “/test/selenium“. Create the file “” and insert this:



import grails.plugins.selenium.SeleniumAware
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.methods.StringRequestEntity

 * Selenium-Test for the Web Service, uses Selenium for starting the Grails Server.
 * @author Siegfried Bolz
class MyTestServiceSeleniumTests extends GroovyTestCase {

  void testLoginMethod() { "http://localhost:8080/GrailsWebServiceWithSeleniumTest/services/myTest?wsdl"

    def url = "http://localhost:8080/GrailsWebServiceWithSeleniumTest/services/myTest?wsdl"

    def payload = """
            <soapenv:Envelope xmlns:soapenv="" xmlns:eu="">

    def method = new PostMethod(url)
    def client = new HttpClient()

    payload = payload.trim()
    method.addRequestHeader("Content-Type", "text/xml")
    method.addRequestHeader("Accept", "text/xml,application/xml;q=0.9")
    method.setRequestEntity(new StringRequestEntity(payload))
    def statusCode = client.executeMethod(method)
    println "STATUS CODE : ${statusCode}"

    assertEquals "Status Code 200 expected", 200, statusCode

    def resultsString = method.getResponseBodyAsString()
    println resultsString

    assertTrue "Result did not contain the expected result message", resultsString.contains("<loginResult>Hello Siegfried Bolz</loginResult>")



To start the test, execute this command:

test-app :selenium -echoOut

As VM parameters i recommend:

-Xmx1024m -Xms1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:MaxPermSize=256m

Selenium Test in IDEA

Selenium Test in IDEA


A working example for Grails 1.3.7 and IntelliJ IDEA 10 is available here (click)


As you can see, it is very simple to create a Web Service with a Selenium Test. If you have any comments, drop a line below.

Technorati Tags: , , , , , , , , , , , , ,

No responses yet

Comments are closed.