ariya/phantomjs

Separate CookieJar objects for WebPages

jrollinson opened this issue · 16 comments

Currently, there exists a single CookieJar object for all web pages. This is in line with the usual behaviour of web pages, but causes difficulty when treating pages as separate entities.

To fix: CookieJar should not be a singleton, and objects that use a CookieJar should have one passed in on construction.

Interesting. Let me come up with a hypothetical API and code:

var myJar = new CookieJar();
page.cookieJar = myJar;
myJar.save('/secret/path/to/my/locker/cookie.dat');

Does that make sense? Obviously, there can be a default cookie jar for a new page instance.

What about for loading an existing CookieJar? Specify a path in the constructor, maybe?

@ariya Are you suggesting a new CookieJar module like this?

var cookieJarCreator = require('cookieJar');
var myJar = cookieJarCreator.create();

I think an optional path in the constructor would be a great way for loading existing cookie jars.

Whether it's in its own module, conveniently constructed, etc are small details. What we need to come up with is the answer to the following question "if such an API is provided, how would your code look like".

Ok. I think the code would look like this:

var myJar = new CookieJar();
page1.cookieJar = myJar;
page2.cookieJar = myJar;
myJar.addCookie(cookie);

... do whatever ...

page1.close();
page2.close();
myJar.save(path);
myJar.close();

A new page would have the default CookieJar and then you can set the page's CookieJar to be whatever you want.
Making the user explicitly close the CookieJar would save a bit of a nightmare.

Also, I think that the CookieJar should have the same API that both the phantom and webpage objects have with cookies.
In other words it should have the following functions and properties:

addCookie(Cookie);
deleteCookie(cookieName);
cookies;
clearCookies();
save(path);
close();

We should also add a CookieJar getter on the phantom object and both a getter and a setter for the CookieJar in webpages.

Running into this problem with trying to run tests in parallel in CasperJS. Having the ability to set CookieJars per instance of WebPage would be extremely helpful.

I would like to be responsible for this issue. What do you think about the interface I described above?

Any chance that this update could be pulled in to PhantomJS? Would love to use PhantomJS with GhostDriver and run sessions in parallel, and this would help eliminate any shared cookie issues, to mitigate issues concerning logging in as different users and such.

waiting for this issues as well, it's a show-stopper for using PhantomJS with GhostDriver in our web-tests

This issue is closed, I guess

It's not closed and this problem is in out TODO list. I believe this is a crucial feature and it should be implemented.

@vitallium have you tried 2.0.0+ version? 244cf25

The problem is that in the current state this feature requires some work to do. For example. it can't handle when the user closes a web page. We need to improve it and fix some nasty bugs.

stale commented

Due to our very limited maintenance capacity, we need to prioritize our development focus on other tasks. Therefore, this issue will be automatically closed (see #15395 for more details). In the future, if we see the need to attend to this issue again, then it will be reopened. Thank you for your contribution!