Browser.manage().addCookie() returns error missing 'name'

Viewed 5038

When I am trying to set cookies in protractor, I am getting below error message:

Failed: invalid argument: missing 'name'
(Session info:chrome=61.0.3163.100)

enter image description here

I have used below different codes to set cookie in protractor. But none of them is working.

Case 1:

browser.get('http://localhost:8080');
browser.driver.manage().addCookie('test', 'test');

Case 2:

browser.get('http://localhost:8080');
protractor.browser.manage().addCookie({ 'name': 'test', 'value': 'test', 'path': '/', 'domain': 'localhost' });

Case 3:

browser.get('http://localhost:8080');
browser.manage().addCookie('test', 'test', '/', 'localhost');

I am running code with protractor version 5.2.

Can anyone help me out? Thanks in advance.

3 Answers

I got solution of my problem. Below code worked for me.

(browser.manage() as any).addCookie({ name: 'test', value: 'test', domain: 'localhost' });
browser.get('http://127.0.0.1:8000');
browser.manage().addCookie('test', 'test', '/', '127.0.0.1');

Issue is with using localhost, use 127.0.0.1 instead

Please read further with issue with protractor here

Prefer using browser.driver.get() over browser.get().

As far as browser.driver.get() returns a promise, you should take it into account:

browser.driver.get('http://' + domain + '/').then(() => {
  browser.manage().addCookie({name: 'foo', value: 'boo'});
});

Also notice, in this case you don't need to pass domain value to addCookie because protractor will use the current domain by default. This will also solve an issue with localhost which Mohan Ram mentioned

Related