如何使用 PhantomJS 在 Geb / Selenium 中设置 cookie
How to set a cookie in Geb / Selenium with PhamtomJS
如何在 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"
}
}
维基百科的域名中没有拼写 "ie" 并且 "org.com" 看起来也很奇怪。也许下次你想提供一个实际可执行并做一些有意义的事情的例子。 :-7
对我来说这很好用:
package de.scrum_master.Whosebug
import geb.spock.GebReportingSpec
import org.openqa.selenium.Cookie
class SetCookieIT extends GebReportingSpec {
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"
}
}
如果您还有其他问题,请更新您的问题并提供 SSCCE 重现实际问题。
更新问题修改后:PhantomJS的问题是如果你没有明确指定域,它会拒绝创建cookie。这有效:
options.addCookie(new Cookie("my-geb-cookie", "foobar", ".wikipedia.org", "/", null))
如何在 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"
}
}
维基百科的域名中没有拼写 "ie" 并且 "org.com" 看起来也很奇怪。也许下次你想提供一个实际可执行并做一些有意义的事情的例子。 :-7
对我来说这很好用:
package de.scrum_master.Whosebug
import geb.spock.GebReportingSpec
import org.openqa.selenium.Cookie
class SetCookieIT extends GebReportingSpec {
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"
}
}
如果您还有其他问题,请更新您的问题并提供 SSCCE 重现实际问题。
更新问题修改后:PhantomJS的问题是如果你没有明确指定域,它会拒绝创建cookie。这有效:
options.addCookie(new Cookie("my-geb-cookie", "foobar", ".wikipedia.org", "/", null))