views:

26

answers:

0

I've been having issues with testing my Grails application's authentication. It appears that the browser won't accept cookies, so I created a simple grails application as a test.

<html>
<head>
    <title>Welcome to Grails</title>
</head>
<body>
    <g:each in="${request.cookies}">
       <h1>${it.name} = <span class="value">${it.value}</span></h1>
    </g:each>

    <span class="value">test test</span>
</body>

and my Geb test:

import spock.lang.Stepwise;
import geb.Page;
import geb.spock.GebReportingSpec


@Stepwise
class LoginSmokeTests extends GebReportingSpec {
 String getBaseUrl() {
  return "http://localhost:8080/test123/"
 } 

 def "testing stuff"() {
  given:
   to HomePage
  when:
   println header

  then: 
   at HomePage
 }  
}



class HomePage extends Page {
 static at = { title == "Welcome to Grails" }

 static content = {
  header { $("span.value").first().text() }
 }
}

When I view this through the browser, 2 cookies' values are printed. When accessing it via my Geb test, the <span class="value">test test</span> HTML is picked up - as there are no cookies in the request to iterate over.

I've done some searching on how to use Geb + cookies, but as it's relatively new software, there doesn't seem to be too much information out there (although its manual is great).

A new browser instance is created for each test method However, since the default behaviour is to re-use the default driver across browser instances the driver's cookies are cleared in the Spock cleanup() method. However, if your spec is stepwise (i.e. is annotated with @spock.lang.Stepwise - see Spock docs for details) the cookies are NOT cleared in cleanup() but are cleared in cleanupSpec() which means browser state is not reset between test methods (which makes sense for a stepwise spec).

And, I'm only executing one test method - but no cookies are sent. Any ideas?