【发布时间】:2017-07-16 10:33:17
【问题描述】:
如何在 Geb 中设置 cookie?我在给定的示例中遇到了以下错误:
org.openqa.selenium.InvalidCookieDomainException: {"errorMessage":"Can only set Cookies for the current domain" ....
.. 我还尝试使用 Cookie Builder 显式设置 cookie 多米诺骨牌,但这只会导致另一个异常:org.openqa.selenium.UnableToSetCookieException: {"errorMessage":"Unable to set Cookie"}
请注意,我曾经在 GebConfig.groovy 文件中有一个 baseURL .. 但我也将其删除了.. 除了 PhantomJS 驱动程序配置之外,配置文件中没有任何设置。
我在 OSX 上使用 PhantomJS 最新版本(1.3.0 jar 和 2.1.1 驱动程序 OSX)。
请注意,出于某种原因,该示例确实可以使用 Chrome Webdriver。
import geb.spock.GebSpec
import org.openqa.selenium.Cookie
class SetCookieIT extends GebSpec {
def "Cookie example"() {
given:
def options = driver.manage()
when:
go "https://www.wikipedia.org/"
then:
!options.getCookieNamed("my-geb-cookie")
when:
options.addCookie(new Cookie("my-geb-cookie", "foobar"))
go "https://www.wikipedia.org/"
then:
title == "Wikipedia"
options.getCookieNamed("my-geb-cookie").value == "foobar"
}
}
【问题讨论】:
-
@Mouneer,我看过那个问题,这不是重复的......我认为这更多是 Geb(Groovy)的问题,然后是 Selenium 甚至 Python问题。
标签: selenium groovy phantomjs geb