2

I test a OAuth 2 Login page. It allows me to remember my login. This is done with a persistant cookie. Now I want to test if this cookie remains present after a browser restart which is its intent:

..load Page..
..login which sets the cookie..
browser.Quit();
browser = new FirefoxDriver();
..load Page..
Cookie is gone

Cookies of the test browser are enabled. I tested it with Firefox, Chrome and IE driver. The cookie is a persistent cookie and works perfectly with a manually started instance of one of the browsers.

What I do as a workaround:

..load Page..
..login which sets the cookie..
var cookie = driver.Manage().Cookies.GetCookieNamed("cookie");
browser.Quit();
browser = new FirefoxDriver();
driver.Manage().Cookies.AddCookie(cookie);
..load Page..

Are those cookies deleted when Quitting the browser? Why does it not work as intended?

1 Answer 1

2

What you could do, is try to use an existing profile in the browser, so that way it would actually save the cookies somewhere. Otherwise the webdriver starts up an anonymous profile every time that doesn't save the cookies, it just throws them away when the browser is disposed of. This is mentioned in this SO Answer to Selenium Webdriver not respecting cookies or cached images

Here is a C# version from another SO Answer

private IWebDriver driver;  
string pathToCurrentUserProfiles = Environment.ExpandEnvironmentVariables("%APPDATA%") + @"\Mozilla\Firefox\Profiles"; // Path to profile
string[] pathsToProfiles = Directory.GetDirectories(pathToCurrentUserProfiles, "*.default", SearchOption.TopDirectoryOnly);
if (pathsToProfiles.Length != 0)
{
     FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]);
     profile.SetPreference("browser.tabs.loadInBackground", false); // set preferences you need
     driver = new FirefoxDriver(new FirefoxBinary(), profile, serverTimeout);
}
else
{
     driver = new FirefoxDriver();
}

but they are loading a default profile that they must have created already.

I suggest looking into this further, I haven't gotten to this point in building my automated tests, but it is coming soon.

2
  • Thanks for the info with the new anonymous profile! This helps a lot to find a proper solution. I have to implement my tests in a way that makes it easy to switch the test browser. Hence I either have to implement a profile-switch method for each browser or just grab the cookies/preferences I want to stay browser independent in the code. Do you know if the profile is deleted after closing the browser?
    – Lukas
    Commented Jun 24, 2016 at 7:19
  • You should create something that is polymorphic in that you can pass it any web driver and it will handle the profiles for you. I don't think that the code will be much different between the different web drivers.
    – Malachi
    Commented Jun 24, 2016 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.