5

I have a test case in which the user shuts down the browser and re-opens it, to test if some login cookies are loaded correctly.

One of the suggested ways was

browser.get("domain1.com")
cookies_domain1 = browser.get_cookies()
browser.get("domain2.com")
cookies_domain2 = browser.get_cookies()
//close browser

//re-open browser
browser.get("domain1.com")
for cookie in cookies_domain1:
    driver.add_cookie(cookie)
browser.get("domain2.com")
for cookie in cookies_domain2:
    driver.add_cookie(cookie)

But this doesn't look good. I need the cookies loaded before opening the page. Selenium seems to allow only adding the cookies from a page to that page only!

Another way i've tried to do this was to save the browser.profile.path directory, back it up, and on re-connect, pass it on a FirefoxProfile, which gets passed to a webdriver.Firefox(FirefoxProfile(path_to_directory_backup)). Unfortunately, this doesn't seem to save the cookies.

Community
  • 1
  • 1
user37203
  • 676
  • 5
  • 21
  • 2
    There is no way at all in Selenium Webdriver to add the cookies before loading the webpage. – Vikas Ojha Jun 11 '15 at 21:28
  • Can you explain why you would need to load the cookies prior to loading the page? Couldn't you load the page, load the cookies, then refresh? What behavior are you trying to replicate? – Dan O'Boyle Jun 11 '15 at 22:20
  • The behaviour is the user loading a page for which it already has cookies and seeing the correct content from the first shot. There are multiple domains involved in the test case, and i can't simply run around each of them to re-create the cookies. I'm testing a adserver display thing, and everything is filled from a different site. – user37203 Jun 11 '15 at 22:38
  • I have a similar problem - users log in and their cookies should remain for a few years. I want to test it with Selenium. https://stackoverflow.com/questions/63901136/using-permanent-profiles-and-cookies-in-selenium – Uri Sep 15 '20 at 14:04
  • There's an answer in the linked question that explains how to save the Firefox profile to a directory. – user202729 Jan 02 '21 at 03:57

0 Answers0