Restoring Firefox Session in Selenium

Viewed 1157

I am currently automating a website and have a test which checks the functionality of the Remember Me option. My test script logs in, entering a valid username and password and checks the Remember Me checkbox before clicking Log In button.

To test this functionality I save the cookies to file using pickle, close the browser and then reopen the browser (loading the cookies file).

def closeWebsite(self, saveCookies=False):
    if saveCookies:
        pickle.dump(self.driver.get_cookies(), open('cookies.pkl', 'wb'))
    self.driver.close()

def openWebsite(self, loadCookies=False):
    desired_caps = {}
    desired_caps['browserName'] = 'firefox'
    profile = webdriver.FirefoxProfile(firefoxProfile)
    self.driver = webdriver.Firefox(profile)
    self.driver.get(appUrl)
    if loadCookies:
        for cookie in pickle.load(open('cookies.pkl', 'rb')):
            self.driver.add_cookie(cookie)

However, when I do this, the new browser is not logged in. I understand that everytime you call the open the browser a new Session is created and this session ID can be obtained using driver.session_id Is it possible, in the openWebsite method to load a driver and specify the sessionID? When I test this manually, the Remember Me option works as expected. I'm just having trouble understanding how Selenium handles this case.

1 Answers
Related