4

I'm trying to set a cookie in a protractor test case. We're using Protractor 3.3.0, Angular 1.5.x and Node.js 6.9.1

This is the spec:

(function() {
    'use strict';

    describe('Dummytest', function() {
        beforeEach(function() {
            browser.get('./');
        });

        it('should set a cookie', function() {
            browser.manage().addCookie("test", "fail_cookie", '/', 'localhost');
        });
    });
})();

This is the error message I get:

  Message:
    Failed: {"errorMessage":"Unable to set Cookie","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"165","Content-Type":"application/json; charset=utf-8","Host":"localhost:45556","User-Agent":"Apache-HttpClient/4.5.1 (Java/1.8.0_77)"},"httpVersion":"1.1","method":"POST","post":"{\"cookie\":{\"path\":\"/\",\"domain\":\"localhost\",\"name\":\"test\",\"httpOnly\":false,\"hCode\":3556498,\"secure\":false,\"value\":\"fail_cookie\",\"class\":\"org.openqa.selenium.Cookie\"}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/68875d10-f7b4-11f6-8f37-71161903ed83/cookie"}}
    Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42'
    System info: host: 'XXXXXXXXX', ip: '10.##.##.230', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_77'
    Driver info: driver.version: unknown
  Stack:
    UnableToSetCookieError: {"errorMessage":"Unable to set Cookie","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"165","Content-Type":"application/json; charset=utf-8","Host":"localhost:45556","User-Agent":"Apache-HttpClient/4.5.1 (Java/1.8.0_77)"},"httpVersion":"1.1","method":"POST","post":"{\"cookie\":{\"path\":\"/\",\"domain\":\"localhost\",\"name\":\"test\",\"httpOnly\":false,\"hCode\":3556498,\"secure\":false,\"value\":\"fail_cookie\",\"class\":\"org.openqa.selenium.Cookie\"}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/68875d10-f7b4-11f6-8f37-71161903ed83/cookie"}}
    Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42'
    System info: host: 'XXXXXXXXXXXXX', ip: '10.##.##.230', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_77'
    Driver info: driver.version: unknown
        at WebDriverError (C:\Users\[username]\dev\project\node_modules\selenium-webdriver\error.js:27:5)

What am I doing wrong?

EDIT:

I've done some further experiments. Notice that I've changed the domain, and that there is no browser.get before the successful cookie read/write.

(function() {
    'use strict';

    describe('Dummytest', function() {
        it('should set a cookie', function() {
            browser.manage().addCookie("test", "fail_cookie", '/', '127.0.0.1');
            browser.manage().getCookie('test').then(function(cookie) {
                console.log('cookie test', cookie);
                browser.get('./');
                browser.manage().getCookie('test').then(function(cookie) {
                    console.log('cookie test 2', cookie);
                });
            });
        });
    });
})();

And this is the output I get:

cookie test { path: '/',
  domain: '127.0.0.1',
  name: 'test',
  httpOnly: false,
  hCode: 3556498,
  secure: false,
  value: 'fail_cookie',
  class: 'org.openqa.selenium.Cookie' }
cookie test 2 null

As you can see the cookie is not set for the domain I arrive at when calling browser.get('./'). How can I set a cookie for that domain without triggering the error in my original question?

EDIT 2:

Found another question (Setting cookies before browser.get) that says to call browser.driver.get first, and then setting the cookie, but doing that I get the same UnableToSetCookieError as I've always been getting. So the solution is something else.

EDIT 3:

On a semi-related note I've been having a real hard time finding documentation for the addCookie function and its parameters. Especially now since in the latest (as of 2016-01-31) version of protractor it expects an object and not a list of parameters. But I finally found it and thought I might as well include a link here: https://github.com/SeleniumHQ/selenium/blob/022644c47c643ce6fe797d65e074329190129c1b/javascript/node/selenium-webdriver/lib/webdriver.js#L1075

EDIT 4:

Getting closer! Setting the domain to .localhost (notice the dot (.) at the beginning) seems to work. Will do further investigations and report back. In the meantime, if anyone can tell my why the dot is significant and why that exact domain works I'd be very grateful :)

Community
  • 1
  • 1
Tobbe
  • 3,282
  • 6
  • 41
  • 53
  • Take a look at this: http://stackoverflow.com/questions/489369/can-i-use-localhost-as-the-domain-when-setting-an-http-cookie. Possibly this could help? – cnishina Jan 31 '17 at 16:34
  • 1
    Hi @Tobbe, Any news? Maybe you can answer your own question for the sake of sharing results? I read that there are problems using `localhost` and that using `127.0.0.1` is more advisable but I'm still new and try to keep sessions alive as well. – firepol May 17 '17 at 12:43
  • 1
    Crap. Sorry, forgot to come back to this, and now I've forgotten what the solution was :( I'm with a new company now and no longer have access to the code where this was needed. – Tobbe May 18 '17 at 10:38

1 Answers1

1

Setting baseUrl to 127.0.0.1 in my protractor.conf.js seems to solve a similar issue that I've been facing.

unzoomed
  • 580
  • 1
  • 6
  • 23