In the manual test case, which I'm trying to automate in Selenium using C#, it says: "Login with checkbox 'remember me' activated, close the browser, open the browser, check if user is still logged in."
Manually executed, this is of course successfull. With Selenium in Chrome, I always lose the cookies across sessions.
What I tried so far:
public static void RefreshDriver(TestParams testParams)
{
var cookies = testParams.Target.Driver.Manage().Cookies.AllCookies;
var url = new Uri(testParams.Target.Driver.Url);
testParams.Target.Driver.Quit();
testParams.Target = testParams.Target.Clone(); // creates new ChromeDriver()
string temp = url.GetLeftPart(UriPartial.Authority);
testParams.Target.Driver.Navigate().GoToUrl(temp);
foreach (Cookie cookie in cookies)
{
testParams.Target.Driver.Manage().Cookies.AddCookie(cookie);
}
testParams.Target.Driver.Navigate().GoToUrl(url);
}
This is how I create the ChromeDriver:
private ChromeDriver _CreateChromeDriver(string httpProxy)
{
var options = new ChromeOptions();
string tempDir = @"C:\Users\Kimmy\Documents\MyUserDataDir";
options.AddArgument("user-data-dir=" + tempDir);
if (!string.IsNullOrEmpty(httpProxy))
{
options.Proxy = new Proxy {HttpProxy = httpProxy};
}
return new ChromeDriver(options);
}
When I execute my test case and reach the part with RefreshDriver()
, then at first my user is logged in again. But as soon as I start adding products to the shopping basket, my user is suddenly not logged in any more.
Is there a way to tell Chrome to keep the cookies across sessions, without manually saving and restoring the cookies?
I wrote a similar program using java programming language. Please refer the below code. It may be helpful for you. Program has many comments for readability purpose.