Issue 1953 in selenium: Webdriver.manage().addCookie throws undocumented exception if browser is not in the exact domain of the cookie set

1,687 views
Skip to first unread message

sele...@googlecode.com

unread,
Jun 29, 2011, 12:42:27 PM6/29/11
to selenium-deve...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 1953 by zorze...@google.com: Webdriver.manage().addCookie throws
undocumented exception if browser is not in the exact domain of the cookie
set
http://code.google.com/p/selenium/issues/detail?id=1953

I have the need to set a cookie before I "get" my initial page. Something
like this:

public void testCookie() {
WebDriver driver = new FirefoxDriver();
driver.manage().addCookie(new Cookie("foo", "bar", "localhost", "/",
null));
// The rest of my test
}

But this throws an NS_ERROR_FAILURE (see full stack trace at the bottom).
The doc for addCookie:

/**
* Add a specific cookie. If the cookie's domain name is left blank, it
* is assumed that the cookie is meant for the domain of the current
* document.
*
* @param cookie The cookie to add.
*/

Though not properly stated, it seems to imply that, if the cookie's domain
name is "blank" (I think "{@code null}" is what it means), this method
would have trouble executing. But it also certainly seems to imply that I
could simply provide a domain name and things would work, which, as I said,
is not the case.

Furthermore, this fails in a different way if the browser is visiting a
domain other than the one set in the Cookie. E.g. the code below blows
with "org.openqa.selenium.InvalidCookieDomainException: You may only set
cookies for the current domain":

WebDriver driver = new FirefoxDriver();
driver.get("http://amazon.com");
driver.manage().addCookie(new
Cookie("foo", "bar", "www.google.com", "/", null));

The ability to set Cookies on arbitrary domains (whether or not the browser
is currently at any particular domain) is useful to me, so I would hate if
the "fix" this bug was to just document the actual behavior.

Thanks,

Z

*********************************************************

Full stack trace:

org.openqa.selenium.WebDriverException: Component returned failure code:
0x80004005 (NS_ERROR_FAILURE) [nsIDOMLocation.host]
Build info: version: '2.0rc3', revision: '12536', time: '2011-06-20
18:19:52'
System info: os.name: 'Linux', os.arch: 'i386',
os.version: '2.6.32-gg508-generic', java.version: '1.6.0_01'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at
org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
at
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:402)
at
org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions.addCookie(RemoteWebDriver.java:431)
at
junit3_tdtc.tutorial_1_server.Example4InjectionControllerTest.testCookie(Example4InjectionControllerTest.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at
com.google.common.testing.junit3.TearDownTestCase.runBare(TearDownTestCase.java:106)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException:
Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)
[nsIDOMLocation.host]
Build info: version: '2.0rc3', revision: '12536', time: '2011-06-20
18:19:52'
System info: os.name: 'Linux', os.arch: 'i386',
os.version: '2.6.32-gg508-generic', java.version: '1.6.0_01'
Driver info: driver.version: unknown


sele...@googlecode.com

unread,
Jun 30, 2011, 9:05:22 PM6/30/11
to selenium-deve...@googlegroups.com

Comment #1 on issue 1953 by genit...@gmail.com:
Webdriver.manage().addCookie throws undocumented exception if browser is
not in the exact domain of the cookie set
http://code.google.com/p/selenium/issues/detail?id=1953

I had the same problem. Remove this at line 612 of
/javascript/firefox-driver/extension/components/firefoxDriver.js

if (currDomain.indexOf(cookie.domain) == -1) { // Not quite right, but
close enough
throw new WebDriverError(ErrorCode.INVALID_COOKIE_DOMAIN,
"You may only set cookies for the current domain");
}


sele...@googlecode.com

unread,
Jun 30, 2011, 9:30:23 PM6/30/11
to selenium-deve...@googlegroups.com

Comment #2 on issue 1953 by genit...@gmail.com:
Webdriver.manage().addCookie throws undocumented exception if browser is
not in the exact domain of the cookie set
http://code.google.com/p/selenium/issues/detail?id=1953

I had the same problem. Remove the following at line 612 of
/javascript/firefox-driver/extension/components/firefoxDriver.js

if (currDomain.indexOf(cookie.domain) == -1) { // Not quite right, but
close enough
throw new WebDriverError(ErrorCode.INVALID_COOKIE_DOMAIN,
"You may only set cookies for the current domain");
}


Attachments:
patch.txt 804 bytes

sele...@googlecode.com

unread,
Jun 30, 2011, 10:05:34 PM6/30/11
to selenium-deve...@googlegroups.com

Comment #3 on issue 1953 by genit...@gmail.com:
Webdriver.manage().addCookie throws undocumented exception if browser is
not in the exact domain of the cookie set
http://code.google.com/p/selenium/issues/detail?id=1953

More changes needed if you want to add cookies while on about:blank, useful
for loading session info before you visit the target. I'm not sure why it
is rejecting adding cookies unless you are on an HTML page, adding cookies
to the browser session shouldn't hurt anything no matter what kind of page
you are on.

Attachments:
patch_addcookies.txt 1.3 KB

sele...@googlecode.com

unread,
Jul 2, 2011, 10:04:37 AM7/2/11
to selenium-deve...@googlegroups.com
Updates:
Status: WontFix

Comment #4 on issue 1953 by simon.m.stewart: Webdriver.manage().addCookie

throws undocumented exception if browser is not in the exact domain of the
cookie set
http://code.google.com/p/selenium/issues/detail?id=1953

The cookies methods only act on cookies that would be visible as this is
the only thing that can be made to work consistently across all browsers.
The behaviour that you see is the expected behaviour.

sele...@googlecode.com

unread,
Sep 21, 2011, 9:59:02 AM9/21/11
to selenium-deve...@googlegroups.com

Comment #5 on issue 1953 by glaurung...@gmail.com:
Webdriver.manage().addCookie throws undocumented exception if browser is
not in the exact domain of the cookie set
http://code.google.com/p/selenium/issues/detail?id=1953

I have the same issue: I would like to add a cookie before the first "get"
but it doesn't work and the error is cryptic (HtmlUnit driver):

driver = new HtmlUnitDriver();
driver.manage().addCookie(new Cookie(xxxxxx));

org.openqa.selenium.InvalidCookieDomainException: You may only add cookies
that would be visible to the current domain: .myserver.mycompany.com => .

Could it be possible to provide a way to have default cookies before the
first "get" call. For example to emulate cookies that are still prevent
even after restarting the browser.

Thanks

Reply all
Reply to author
Forward
0 new messages