MachinePublishers / jBrowserDriver

A programmable, embeddable web browser driver compatible with the Selenium WebDriver spec -- headless, WebKit-based, pure Java
Other
809 stars 143 forks source link

getCookies() not giving all the cookies? #288

Open Gintasz opened 6 years ago

Gintasz commented 6 years ago

I'm facing a super weird bug. I've automated Google login with JBrowserDriver - it logs in fine. But if I save cookies and then load them back in, Google claims I logged out!

// ...login functionality goes here
System.out.println("1: " + verifyLoggedIn()); // PRINTS TRUE
String currentCookies = cookiesToString(driver.manage().getCookies());
driver.manage().deleteAllCookies();
ArrayList<Cookie> cookiesArray = null;
try
{
    cookiesArray = new Gson().fromJson(currentCookies, new TypeToken<ArrayList<Cookie>>(){}.getType());
}
catch(Exception e)
{
    e.printStackTrace();
}
for(Cookie cookie : cookiesArray)
    driver.manage().addCookie(cookie);
System.out.println("2: " + verifyLoggedIn()); // PRINTS FALSE?!??

protected String cookiesToString(Set<Cookie> cookies)
    {
        ArrayList<Cookie> list = new ArrayList<>(cookies);
        String str = new Gson().toJson(cookies);
        return str;
    }

private boolean verifyLoggedIn()
    {
        driver.get("https://plus.google.com/u/0/me");
        // waits for page load
        return !driver.getCurrentUrl().toLowerCase().contains("signin") && !driver.getCurrentUrl().toLowerCase().contains("login");
    }

I've tried checking with debugger what currentCookies variable is and what cookiesToString(driver.manage().getCookies()) give after I reload these cookies back in - it's the same string! Is it possible that driver.manager().getCookies() might not be returning all the cookies?

@hollingsworthd Any clue man?

P.S. I'm using proxy option.

Gintasz commented 6 years ago

@hollingsworthd I've minimized it to the minimum:

System.out.println("1: " + verifyLoggedIn()); // PRINTS TRUE
Set<Cookie> cookies = new LinkedHashSet<>(driver.manage().getCookies());
driver.manage().deleteAllCookies();
Iterator it = cookies.iterator();
while(it.hasNext())
    driver.manage().addCookie((Cookie) it.next());
System.out.println("2: " + verifyLoggedIn()); // PRINTS FALSE!!

Or could something be wrong with addCookie maybe?

Gintasz commented 6 years ago

@hollingsworthd ok, this is a bug with cookie spec... Confirmed. I've been debugging this cancer for almost a week, here is why it doesn't work: google gives some cookies that have domain "google.com" in them and then jbrowserdriver uses them for all google.com subdomains (e.g. plus.google.com, so it works 100% fine) - ok.

However, when you import such cookie manually via driver.manage().addCookie(), it gets used only on google.com domain and doesn't work on it's subdomains (e.g. plus.google.com, so it breaks).

So it seems that when cookie gets set by the browser, it uses specification RFC 6265 (which is great), but when you import it manually, it uses specification RFC 2109 somehow somewhy (source). I checked LaxCookieSpecProvider in jbrowserdriver's code and I see some hints that seem to confirm this. I don't know how to fix it, so I hope library's maintainer can do that as soon as he's available :). I think just renaming a few things in that class should work.

Gintasz commented 6 years ago

Also, just for the reference, google gives some cookies with domain "google.co.in" that get used on "plus.google.com" (which is a good thing, otherwise it doesn't work), but this ".co.in cookie into .com" thing might be just another RFC 6265 gimmick.

hollingsworthd commented 6 years ago

@Gintasz you are solving the hard problems. Thank you! I'll take look.

hollingsworthd commented 6 years ago

@Gintasz you've done some great work reporting bugs. If you want to be a collaborator and modify the source and commit patches directly let me know.

hollingsworthd commented 6 years ago

At the moment this project is working well for me in production so bug fixes and enhancements in general for me are a lower priority. I get the sense this project is important in your work. Please consider what's possible if you can fix bugs directly with my help and the ability to make releases even, @Gintasz

Gintasz commented 6 years ago

thanks, but I'm not really into software development that much, I just know how to code (so just temporarily applying that for this little project), nor do I, honestly, really know how to use git right now without seriously breaking something

hollingsworthd commented 6 years ago

Sounds good. For the record patches are always welcome and can be provided in any form, even just emailing them or posting them as text in a comment here or on pastebin. And all patches are tested and reviewed. Any and all contributions are welcome, as long as contributors agree to the CLA which essentially just says contributors license their patches as Apache 2.0 which is what the rest of the project is licensed as (but I'm not a lawyer).

Just wanted to mention that. I also don't want people to feel like they can't report bugs unless they're willing to fix them. People filing bug reports and enhancements is what keeps this project going.

And I didn't mean to pontificate here directly in response to Gintasz. Just want to get my thoughts out there on this topic.