Geb 功能网络测试 +曲奇饼

发布于 2024-09-24 23:10:49 字数 1475 浏览 4 评论 0原文

我在测试 Grails 应用程序的身份验证时遇到问题。浏览器似乎不接受 cookie,因此我创建了一个简单的 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>

和我的 Geb 测试:

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

当我通过浏览器查看此内容时,会打印 2 个 cookie 的值。当通过我的 Geb 测试访问它时,会拾取 test test HTML - 因为请求中没有要迭代的 cookie。

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2024-10-01 23:10:49

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

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

Because this is your first request, the browser won't have any cookies because the server hasn't sent any.

If you do a subsequent request you should see the cookies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文