1

I have the following sequence of Selenium code, written with Node.js:

   it('tests cookies', t => {
      driver.manage().getCookies().then(function (cookies) {
        console.log('all cookies => ', cookies);
      });
      driver.manage().addCookie({name:'foo', value: 'bar'});
      driver.manage().getCookies().then(function (cookies) {
        console.log('all cookies => ', cookies);
      });
      driver.manage().deleteCookie('foo');
      return driver.manage().getCookies().then(function (cookies) {
        console.log('all cookies => ', cookies);
      });
    });

and I get this output:

all cookies =>  []
all cookies =>  []
all cookies =>  []

anybody know why the addCookie functionality wouldn't work? I am not sure I understand why this wouldn't yield some cookies in the cookie jar.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

2

The problem is that cookie domain is not defined. You need to navigate to some URL before you can work with cookies. Try adding driver.get('<some_url>') before getting all the cookies and after setting a new cookie.

it('tests cookies', t => {
      driver.get('127.0.0.1'); // <-- This will set the domain
      driver.manage().getCookies().then(function (cookies) {
        console.log('all cookies => ', cookies);
      });
      driver.manage().addCookie({name:'foo', value: 'bar'});
      driver.get('127.0.0.1'); // <-- Navigate again after setting a new cookie
      driver.manage().getCookies().then(function (cookies) {
        console.log('all cookies => ', cookies);
      });
      driver.manage().deleteCookie('foo');
      return driver.manage().getCookies().then(function (cookies) {
        console.log('all cookies => ', cookies);
      });
    });

See this as well: Selenium JS add cookie to request

Ilia Frenkel
  • 1,969
  • 13
  • 19
  • thanks, I am looking here: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_Options.html – Alexander Mills May 30 '17 at 02:38
  • for some reason this call: driver.manage().addCookie({name: 'cec_user_id', value: val, secure: true, httpOnly: true}); yields: – Alexander Mills May 30 '17 at 02:38
  • all cookies => [ { path: '/', domain: 'cdt-server', name: 'cec_user_id', httpOnly: false, secure: false, hCode: -810793967, value: 'alexamil', class: 'org.openqa.selenium.Cookie' } ] – Alexander Mills May 30 '17 at 02:38
  • any idea why that cookie is neither secure nor httpOnly? – Alexander Mills May 30 '17 at 02:39
  • Secure cookies can only be set for HTTPS connections – Ilia Frenkel May 30 '17 at 02:48
  • As far as I know `httpOnly` means that the cookie was set by the server rather than the client side javascript. There was a known issue with Chromedriver always returning false for `httpOnly`. Check that you are using the latest version. – Ilia Frenkel May 30 '17 at 02:50
  • yeah so there's a problem with this - what if I cannot get the domain, until I have a cookie in hand? – Alexander Mills May 30 '17 at 06:17