1

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 and cache 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, so the 2nd factor authentication page will be bypassed. I need this for IE, Chrome, Firefox and Safari.

I don’t have any code that clears this explicitly, but I also don’t have anything that attempts to force it to not clear (if that’s even in existence.) I haven’t tried much of anything else because I have no idea what to try.

Versions: Selenium WebDriver: 2.45.0.0, Selenium Grid: 2.45.0

Thanks!

RKelley
  • 1,099
  • 8
  • 14
  • Possible duplicate of [How can I test a "Remember Me" checkbox feature in Selenium](https://stackoverflow.com/questions/29338944/how-can-i-test-a-remember-me-checkbox-feature-in-selenium) – stiemannkj1 Oct 26 '17 at 15:11

1 Answers1

1

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:

  1. Get all the cookies when test ends:

    ReadOnlyCollection<Cookie> cookies = driver.Manage().Cookies.AllCookies;

  2. Store them somewhere. How you do it depends on the automation setup, but usually simple serialization to disk should work just fine.

  3. De-serialize your cookies on test start and add them through the WebDriver:

    foreach (var cookie in cookies) driver.Manage().Cookies.AddCookie(cookie);

Community
  • 1
  • 1
orom
  • 851
  • 1
  • 10
  • 22
  • 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 Mar 20 '15 at 23:22
  • I tried: DesiredCapabilities caps; caps.SetCapability("firefox_profile", "profileToolsQA"); and it doesn't work either. – RKelley Mar 20 '15 at 23:33
  • 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 Mar 20 '15 at 23:53
  • 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 Mar 21 '15 at 01:01
  • 1
    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 Mar 21 '15 at 01:36
  • 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 Mar 23 '15 at 18:49
  • @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 Mar 24 '15 at 14:09
  • 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 Mar 24 '15 at 17:53
  • "Store them somewhere"... Cookies are not serializable, how would one go about serializing [a non-serilizable object](http://stackoverflow.com/questions/14628899/whats-the-easiest-way-to-save-an-object-to-a-file-without-serialization-attribu), most of these approaches don't work in this example, could you provide more detail for step 2? – David Rogers Oct 07 '16 at 16:48
  • What about Safari?@RKelley @orom – Mr.O Nov 28 '19 at 09:32