Setting cookies before browser.get

Petr Peller picture Petr Peller · Jun 10, 2014 · Viewed 14.8k times · Source

Our (PHP) application requires certain cookies to be set in order to load an Angular.js client app. If the cookies are not set an exception is thrown and error page is shown.

This means in order to run E2E tests we need to set the cookies, but the following fails because Protractor is trying to find Angular right after the browser.get call (it's not there because the exception was thrown).

browser.get('http://' + domain + '/');
browser.manage().addCookie('foo', 'boo', '/', domain);

I tried to call browser.get after setting the cookies:

browser.manage().addCookie('foo', 'boo', '/', domain);
browser.get('http://' + domain + '/');

But this produces the following error:

Failed to set the 'cookie' property on 'Document': Cookies are disabled inside 'data:' URLs.

Is there a way how to handle this situation? Perhaps to tell Protractor not to check for Angular when doing the first browser.get call or somehow set cookies for our domain before calling getting the URL?

Answer

Petr Peller picture Petr Peller · Jun 10, 2014

I found the solution in the Protractor Getting Started doc:

browser.driver.get('http://' + domain + '/');
browser.manage().addCookie('foo', 'boo', '/', domain);

Note the browser.driver.get instead of browser.get. This will prevent Protractor looking for the Angular app and cookies can be set. I then use another browser.get inside of it statement.