Search code examples
grailsspockgeb

How to pass Grails test with Geb and Spock? (using grails-functional-test-development)


Please help.

I have page login/auth.gsp

with the following code inside the body

<div id="page-wrap">
  <div class="login-block">
    <g:if test="${flash.message}">
      <h3  class='login_message'>${flash.message}</h3>
    </g:if>
    <g:else>
      <h3>IMMS Login</h3>
    </g:else>
    <form action='${postUrl}' method='POST' id='loginForm' class='cssform' autocomplete='off'>
      <p>
        <label for='username'><g:message code="auth.username.label" default="User Name"/></label>
        <input type='text' class='text_' name='j_username' id='username'/>
      </p>
      <p>
        <label for='password'><g:message code="auth.password.label" default="Password"/></label>
        <input type='password' class='text_' name='j_password' id='password'/>
      </p>
      <p class="submit-wrap">
        <input type='submit' value="${message(code: 'default.button.Login.label', default: 'Login')}"/>
      </p>
    </form>
  </div>
  <div class="image-block"></div>
</div>

Under test/functional/pages dir, I have LoginPage

package pages

import geb.Page

class LoginPage extends Page {
  static url = "login/auth/"
  static at = {
    userName.text() == ""
  }
  static content = {
    userName { $("input", name : "j_username") }
    password { $("input", name : "j_password") }
    loginButton(to: IndexPage){ $(".submit-wrap").find("input").first() }
  }
}

And here is my testing code

import geb.spock.GebReportingSpec
import pages.*
import spock.lang.Stepwise
@Stepwise
class BaseSpec extends GebReportingSpec {
  String getBaseUrl() {
    return "http://localhost:8080/IMMS/"
  }

  def "open URL"() {
    when:
    to LoginPage
    then:
    at LoginPage
  }

}

I run the test and failed. This is the report

<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="0" failures="1" hostname="SGCDMSSVR" name="BaseSpec" tests="1" time="24.715" timestamp="2011-07-12T07:00:04">
  <properties />
  <testcase classname="BaseSpec" name="open URL" time="24.693">
    <failure message="Condition not satisfied:

at LoginPage
|
false
" type="junit.framework.AssertionFailedError">junit.framework.AssertionFailedError: Condition not satisfied:

at LoginPage
|
false

    at BaseSpec.open URL(BaseSpec.groovy:18)
</failure>
  </testcase>
  <system-out><![CDATA[--Output from open URL--
]]></system-out>
  <system-err><![CDATA[--Output from open URL--
]]></system-err>
</testsuite>

Any idea to help me? am I missing some configuration or my jQuery-like navigation is incorrect?

For the testing, I'm using "Functional Test Development" plugin.


Update: Initially I used GebConfig exactly from The sample. I just noticed that the default driver is HTMLUnit.

When I run the functional test using Functional Test Development feature from command console.

grails develop-functional-tests

I select option to run all functional test. The console shown failed tests.

When I change the default driver to Firefox. It still failed but I can see it automatically open Firefox browser and open the URL:

http://localhost:8080/IMMS/login/auth.gsp

and it failed to open the URL 404. I think this is the reason why the test failed.

I tried to run the following from IDE.

grails test-app -functional

It works and firefox browser opened and it did what's written test script and the test pass.

So, I revise the title here. The focus now is on the grails' functional test dev plugin. Perhaps any of you ever tried this plugin and has answer? Thanks.

PS: Am I allow to revise the question? or should I create new question in stackoverflow?


Solution

  • I've asked the creator of the grails plugin here. So basically, it's not about the test code problem. I just need to use different port for testing.