How to have cookies stored and persisted for different browser instances with RemoteWebDriver/Selenium Grid
Use case: Login with a username, navigated to a 2nd factor authentication page to do one of a number of things (i.e. answer a knowledge based question), and then navigated to a final page to enter a password. Close the browser and attempt to login again with the username. This time the 2nd factor authentication page is bypassed because the app recognizes the cookies and the user is prompted to enter their password directly.
Problem: I am using Selenium RemoteWebDriver to run these tests on a separate test machine and when I close the first browser and open a new instance of RemoteWebDriver it appears it starts by clearing cookies (or clears when closing each instance) and the 2nd factor authentication page comes up every time I attempt to login.
What I need: Help to figure out how to create a new instance of RemoteWebDriver without it automatically clearing any cookies or cache at opening or closing, so the 2nd factor authentication page will be bypassed. I need this for IE, Chrome, Firefox and Safari. I’m currently trying to get this to work on Firefox first.
I have created a custom profile on the test machine for Firefox and have the selenium server setup to use that profile which it is doing. If I manually use that profile, the cookies are saved and works as expected.
Versions: Selenium WebDriver: 18.104.22.168, Selenium Grid: 2.45.0
Answer shared from stackoverflow.com http://stackoverflow.com/questions/29176528/how-to-start-selenium-remotewebdriver-or-webdriver-without-clearing-cookies-or-c
To preserve cookies on Firefox; create a custom profile (see here for an example) and use it each time when starting new RemoteWebDriver instance. That way Firefox will reuse same profile with all the existing cookies across sessions. This however doesn’t save cookies received during the test itself. See the alternative approach below for a solution.
A similar approach works for Chrome – link.
For Internet Explorer instead of a custom profile, ensureCleanSession capability needs to be set to false to prevent cleaning cookies on session start – link.
Alternative solution: Cookies can be also manipulated from within the test itself:
Get all the cookies when test ends:
ReadOnlyCollection<Cookie> cookies = driver.Manage().Cookies.AllCookies;
Store them somewhere. How you do it depends on the automation setup, but usually simple serialization to disk should work just fine.
De-serialize your cookies on test start and add them through the WebDriver:
foreach (var cookie in cookies) driver.Manage().Cookies.AddCookie(cookie);
Thanks for fast reply! The first link is broken, but I figured where you were trying to go. I see the firefox instructions for webdriver, but not sure how to get it for remoteWebDriver. I’m using C# and trying to add it to desiredcapabilities, but I’m having trouble figuring out what capability to set for the profile. I’ve tried “webdriver.firefox.profile” and FirefoxDriver.ProfileCapabilityName with no luck. I already created a custom profile on the test machine. – RKelley
I tried: DesiredCapabilities caps; caps.SetCapability(“firefox_profile”, “profileToolsQA”); and it doesn’t work either. – RKelley
I am not entirely sure how to do it in C# as it varies by language, but if you don’t have any specific need for doing it from within the code you can instead use a command line switch when starting the Selenium server: -Dwebdriver.firefox.profile=your_profile_name – orom
I tried to add “webdriver.firefox.profile”: “profileName” to my config file which I’ve been using when starting selenium server on the node, but it didn’t work so I started up the server with the switch you gave & it used my profile. Thanks! I still had a problem though. When I ran my test & it closed my browser & started a new one (with the profile), it doesn’t appear to be saving the cookies because the login process goes into the 2nd factor page right after entering the username. It should go to the password page if it was working properly (which it does if I manually do it). – RKelley
When WebDriver creates new browser instance it copies the profile and uses the copy, so any cookies set during the test won’t be available for the next one. To resolve this you’ll need to start firefox manually (under the custom profile) and login manually to get the cookies saved. After that any new tests executed with this profile will reuse existing cookies. – orom
Is there any other way to do this? Part of the test is making sure that cookies are being stored properly the first time through. If I have to manually login in before each test then it defeats the point of using automation. When RemoteWebDriver runs, I assume it must be storing cookies somewhere. Why are these not available later? If not, is there no way to make it store cookies and make them available in the future? – RKelley
@RKelley I’ve updated my answer with an alternative approach for dealing with cookies. But just in case you are wondering why cookies aren’t preserved in the custom profile, it’s because the browser locks the profile on start and it would be impossible to start multiple instances otherwise. – orom
LIFE IS GOOD AGAIN! Thanks so much for the help!!! I wasn’t able to use base in your solution, but I replaced that with my webdriver instance and it was happy. I then had to make sure I went to the domain first before adding the cookies back in and then doing a page refresh. Once I did all that, it worked!!! Thank you again for all your help!!! – RKelley