Groovy Geb功能性web测试&x2B;饼干

Groovy Geb功能性web测试&x2B;饼干,groovy,selenium,htmlunit,webtest,Groovy,Selenium,Htmlunit,Webtest,我一直在测试Grails应用程序的身份验证时遇到问题。浏览器似乎不接受cookies,因此我创建了一个简单的grails应用程序作为测试 <html> <head> <title>Welcome to Grails</title> </head> <body> <g:each in="${request.cookies}"> <h1>${it.name} = <sp

我一直在测试Grails应用程序的身份验证时遇到问题。浏览器似乎不接受cookies,因此我创建了一个简单的grails应用程序作为测试

<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>
当我通过浏览器查看时,会打印两个cookie值。当通过我的Geb测试访问它时,会选择
test测试
HTML,因为请求中没有要迭代的cookie

我已经做了一些关于如何使用Geb+cookies的搜索,但是由于它是相对较新的软件,似乎没有太多的信息(尽管它的手册很棒)

但是,会为每个测试方法创建一个新的浏览器实例,因为默认行为是跨浏览器实例重新使用默认驱动程序。驱动程序的cookie将在Spock cleanup()方法中清除。但是,如果您的规范是逐步的(即用@spock.lang.stepwise注释-有关详细信息,请参阅spock docs),Cookie不会在cleanup()中清除,而是在cleanupSpec()中清除,这意味着浏览器状态不会在测试方法之间重置(这对于逐步规范是有意义的)


而且,我只执行一个测试方法,但没有发送cookie。有什么想法吗?

因为这是您的第一个请求,所以浏览器不会有任何cookie,因为服务器没有发送任何cookie


如果您执行后续请求,您应该会看到cookies。

因为这是您的第一个请求,浏览器将不会有任何cookies,因为服务器没有发送任何cookies

如果您执行后续请求,您应该会看到cookies

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() }
 }
}