3

How do you set cookie in Geb ? I'm running into the following error with the given example:

org.openqa.selenium.InvalidCookieDomainException: {"errorMessage":"Can only set Cookies for the current domain" ....

.. Ive also tried explicitly setting the cookie domino using the Cookie Builder though that only cause another exception : org.openqa.selenium.UnableToSetCookieException: {"errorMessage":"Unable to set Cookie"}

Note that I used to have a baseURL in the GebConfig.groovy file .. but I have removed it as well .. Other then PhantomJS driver config, there are no settings in the config file.

I'm on OSX and using PhantomJS latest version (1.3.0 jar, and 2.1.1 driver OSX).

Note the example DOES work using the Chrome Webdriver for some reason.

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"
    }
}
vicsz
  • 9,552
  • 16
  • 69
  • 101
  • Possible duplicate of [How to set a cookie to a specific domain in selenium webdriver with python?](http://stackoverflow.com/questions/24919525/how-to-set-a-cookie-to-a-specific-domain-in-selenium-webdriver-with-python) – Mouneer Feb 25 '17 at 16:02
  • @Mouneer, I've seen that Question, and this is not a duplicate ... I think this is more of an issue with Geb (Groovy) then it is with Selenium or even Python for that matter. – vicsz Feb 26 '17 at 17:31

1 Answers1

8

Wikipedia is not spelt with an "ie" in the domain name and "org.com" also looks very strange. Maybe next time you want to provide an example which is actually executeable and does something meaningful. :-7

For me this works nicely:

package de.scrum_master.stackoverflow

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"
  }
}

If you have any further problems, please update your question and provide an SSCCE reproducing the actual problem.


Update after the question was modified: The problem with PhantomJS is that it refuses to create cookies if you do not explicitly specify the domain. This works:

options.addCookie(new Cookie("my-geb-cookie", "foobar", ".wikipedia.org", "/", null))
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Thanks! I've updated the example ... I typo'd wikipedia when replacing with our internal addresses. Nonetheless, using your example (not that I only changed GebReportingSpec to GebSpec) .. I am still getting the same error. – vicsz Feb 26 '17 at 17:29
  • Update ... this seems to have something to do with PhantomJS on OSX, the Chrome Driver works with the example, the PhantomJS one doesn't, Investigating. – vicsz Feb 26 '17 at 17:34
  • How interesting! I also tested on my Win10 box. It really seems to be PJS-related because it works on IE, Edge, chrome, Firefox, HtmlUnit. – kriegaex Feb 26 '17 at 17:41
  • Thanks @kriegaex .. After additional debugging, I've traced it down to PhantomJS, not sure what the correct Stackoverflow is for changing the question .. at this point it's : Why does the latest version of PhantomJS on OSX blow up when trying to set cookies. – vicsz Feb 26 '17 at 17:43
  • You were quicker in changing the question than I providing the answer. I have updated my answer with the solution for PhantomJS. If this glitch is to be attributed to PJS itself or to Ghostdriver, I do not know. And please also note: This is not MacOS-specific but PJS-specific, the behaviour is the same under Windows. – kriegaex Feb 26 '17 at 17:54
  • works ! thanks for quick solution ! Wouldn't have thought of that investigating the following error in the logs : phantomjs://platform/console++.js:263 in error ... Which I guess is unrelated. – vicsz Feb 26 '17 at 17:57