问题
There is a bug in my app wherein Logout doesn't work. I need to workaround this issue in my automation that is in Java using WebDriver. The workaround is to close the browser and reopen it and open the Login page.
To automate this workaround, here is what I have tried:
browserDriver.quit();
browserDriver = new FirefoxDriver(capabilities);
browserDriver.get(loginPageURL);
This returns a new session id. Is there a way to retain the previous session id and set it back. I can get the previous session id using
((RemoteWebDriver)browserDriver).getSessionId();
I also tried deleting all the cookies for the current domain using the following code, but the user was still logged in.
browserDriver.manage().deleteAllCookies();
browserDriver.navigate().refresh();
browserDriver.get(loginPageURL);
Appreciate any help on this.
回答1:
Up to my knowledge after calling the quit() method on driver it will not retain the previous session id.
Anyway try to launch the browser using specific firefox profile by disabling cache in that.
FirefoxProfile profile = new ProfilesIni().getProfile(profilePath);
profile.setPreference("browser.cache.disk.enable", false);
profile.setPreference("browser.cache.memory.enable", false);
profile.setPreference("browser.cache.offline.enable", false);
profile.setPreference("network.http.use-cache", false);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
driver = new FirefoxDriver(dc);
driver.get(url);
Firefox Profile creation ==> https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles
Edit-I Change below setting in that profile
In "about:config" you can set "Browser.sessionstore.enabled" to false, in which case firefox will not restore your browsing session after it closed.
回答2:
When you either use
driver.quit();
or
driver.close();
Selenium always starts a new session. This means it's always directs you to the 'Login' page. Just use below piece of code, no need to set capabilities.
browserDriver.quit();
browserDriver.get(loginPageURL);
You will see the Login page.
来源:https://stackoverflow.com/questions/21906556/how-to-relaunch-firefox-browser-using-webdriver-while-retaining-the-previous-ses