2

I am trying to create a cookie with domain, not host, or entire website.

I have this code now

driver.manage.add_cookie(:name => 'test', :value => 'testvalue', :path => '/', :secure => false)

I want something like this

name=test
value=testvalue
domain=.site.com
path=/

I am getting such result in a firefox cookie dialog

enter image description here

while I want something like this

enter image description here

You can see Host: is empty in my case and in another case it is replaced with Domain: and this is what I want to achieve, to set a cookie domain to .mydomain.com

I want to achieve this for JavaScript to be able to read domain-specific cookies as it can not read what's outside of current domain scope.

Alexander T.
  • 1,401
  • 2
  • 9
  • 11

2 Answers2

5

Try following:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :firefox
driver.get('http://eu.httpbin.org') # <-- required.
driver.manage.add_cookie(name: 'test', value: 'testvalue', path: '/', domain: '.httpbin.org')
driver.get('http://eu.httpbin.org/cookies') # eu.httpbin.org
puts driver.page_source
# => ...
# {
#   "cookies": {
#     "test": "testvalue"
#   }
# }
# ...
driver.get('http://httpbin.org/cookies') # httpbin.org
puts driver.page_source
# => ...
# {
#   "cookies": {
#     "test": "testvalue"
#   }
# }
# ...

NOTE: You have to go to the same domain page (html page) before adding cookie.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Thanks a lot. It's impossible for a cookie to become visible from the start without additional `driver.get` requests right? – Alexander T. Sep 09 '13 at 07:30
  • @SatestoEqaunti, Yes, It's a mandatory step. – falsetru Sep 09 '13 at 07:32
  • @falsetru One update - *driver.get('http://httpbin.org/cookies')* not needed,see my answer. You can use this `driver.manage.cookie_named('test')`. – Arup Rakshit Sep 09 '13 at 07:46
  • @Babai, Thank you for comment. The first `driver.get('http://httpbin.org/')` was typo of `driver.get('http://eu.httpbin.org/')`. It was used to show that cookie was passed to the server properly (with different hostnames). – falsetru Sep 09 '13 at 08:18
  • 1
    @SatestoEqaunti, Only the very first `driver.get` is required. Sorry if it was not clear. – falsetru Sep 09 '13 at 08:18
  • @falsetru *+1* to you,to show that `#add_cookie` takes this `domain: '.httpbin.org'` as an argument. As it is missed in the documentation,except this option all they mentioned... :) – Arup Rakshit Sep 09 '13 at 08:21
3

You can do as below using JavaScript :

require "selenium-webdriver"
require "awesome_print"

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://example.com"

COOKIE_DOMAIN = <<-eotl
var cookieName = arguments[0];
var cookieValue = arguments[1];
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + encodeURIComponent(cookieValue)
                  + ";expires=" + myDate 
                  + ";domain=.example.com;path=/";
eotl

driver.execute_script(COOKIE_DOMAIN,'test','testvalue')
ap driver.manage.cookie_named('test')

output

{
       :name => "test",
      :value => "testvalue",
       :path => "/",
     :domain => ".example.com",
    :expires => #<DateTime: 2014-09-09T07:43:12+00:00 ((2456910j,27792s,999999924n),+0s,2299161j)>,
     :secure => false
}
falsetru
  • 357,413
  • 63
  • 732
  • 636
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • thanks, but it's only added after page has been processed right? I need to `navigate.to` again for cookie to appear and be visible for javascript. I guess it's not possible to achieve what I want. – Alexander T. Sep 09 '13 at 07:28
  • 1
    @Babai, +1, Escape cookie value using [`encodeURIComponent`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent) to ensure that the string does not contain any disallowed characters. – falsetru Sep 09 '13 at 08:23
  • @falsetru You want me to pass this string `cookieName +"=" + cookieValue + ";expires=" + myDate + ";domain=.example.com;path=/";` through the function `encodeURIComponent` ? – Arup Rakshit Sep 09 '13 at 08:27
  • 1
    @Babai, I updated the code to use `encodeURIComponent`. – falsetru Sep 09 '13 at 08:30
  • @falsetru You are one of my helpful friend in SO as always... :) – Arup Rakshit Sep 09 '13 at 08:36