Protractor: Having trouble adding cookies...

Created on 12 Dec 2013  路  12Comments  路  Source: angular/protractor

Hello, I am trying to do a very simple add and retrieve cookie test:

describe("Homepage", function () {
    var ptor;

    beforeEach(function () {
        ptor = protractor.getInstance();
        browser.get('/');
        ptor.manage().addCookie("test", "value");
    });


    it('should have cookie named test', function () {
        cookies = ptor.manage().getCookie("test").then(function(data){
            console.log(data);
        });
    });
});

The console prints out null and I can't figure out why... I would really appreciate any help on this.

external bug needs to be filed

Most helpful comment

Just a quick update:
the link to https://github.com/angular/protractor/blob/master/spec/login/viaTestSpec.js is 404 not found. I believe that the file was renamed and here is what the folder includes now:
https://github.com/angular/protractor/blob/master/spec/login/login_spec.js

All 12 comments

Please ask questions like this on StackOverflow! You can tag with #protractor http://stackoverflow.com/questions/tagged/protractor

Buuut since you're already here - check out https://github.com/angular/protractor/blob/master/spec/login/viaTestSpec.js to see cookies being used.

Ah I'm sorry about that! Thank you for the link

@juliemr I'm sorry for posting here again, but I've been trying to get this working for quite a while now and cannot figure out why the addCookie method doesn't work. The link you specified shows how to get cookies that are being set by a script upon login. All I want to do is use the addCookie method to a cookie in the test itself and then retrieve it.

You answered a related question #130 and I followed that exactly that and read the source code, but am still unable to get it to work. The protractor tests don't seem to cover this use case.

I wonder if you're running into this webdriver behavior: https://code.google.com/p/selenium/issues/detail?id=1953

Hmm this doesn't seem to be the issue. I definitely get() a page before I set the cookie. Also I don't get a "NS_ERROR_FAILURE". Everything executes, just that when the data returns from getCookie, I get null instead of the cookie.

Has anyone else tried to add a cookie?

I did some investigating, and I suspect this is due to how setting cookies on the domain 'localhost' works (I assume you're testing against a site at localhost?).

Check out the issue here: https://code.google.com/p/chromedriver/issues/detail?id=386

I tried your test and I can repro when testing against an app on localhost, but things work fine when I add a cookie to www.angularjs.org instead.

Unfortunately, the recommended answer is to set the domain to NULL, but webdriver overrides that. This is a chromedriver bug.

WORKAROUND
use 127.0.0.1 instead of localhost. Note that you will have to use 127.0.0.1 both for the domain of the cookie you are setting and the URL you are grabbing.

browser.get('http://127.0.0.1:8000');
browser.manage().addCookie('foo', 'bar', '/', '127.0.0.1');

edit: found a better issue to link to.

Ah that makes perfect sense. Yes I am using a localhost and I had tried "null" too which as you said does not work. Workaround looks good. Thanks so much Julie!!

Julie rocks! #goSdets

Hi Julie, this might be a silly question but when I call browser.manage().addCookie() is this supposed to set a cookie that gets sent to the server? If so I can't seem to get the protractor browser to send any cookies to the server no matter what I do. If not then is there some other way to send cookies to the server?

It does seem strange to me to set a cookie after calling browser.get(). Like maybe we're just setting the cookie in the browser state, but not trying to actually send it to the server in the request header? Many of our tests would be much easier if I can set up certain cookie states before they start. Thank you.

@MattSavino unfortunately no, you can't set up a cookie before you navigate to a site. WebDriver requires that when you set cookies, your current site is in the same domain. So the best you can do is navigate to your site in beforeEach, set up the required cookies, and then re-do the navigation.

Thanks Julie. I've actually been trying that but browser.get() still never seems to send the cookie to the server. Is this the expected behavior?

Just a quick update:
the link to https://github.com/angular/protractor/blob/master/spec/login/viaTestSpec.js is 404 not found. I believe that the file was renamed and here is what the folder includes now:
https://github.com/angular/protractor/blob/master/spec/login/login_spec.js

Was this page helpful?
0 / 5 - 0 ratings