Context: Microsoft Azure VM; VS2015 Community; C#; Selenium.WebDriver.2.52.0; Firefox 44.0.2; Console (i.e. not running under IIS)
How is state maintained under Firefox using Selenium. Programmatically I go to one page, enter login details, and traverse to a second domain. Then I close the browser, re-open and try to go to a link on the second domain. I end up at the login page again being requested for login details.
When doing this interactively, the browser remembers that I logged in on the first page and automagically transports me the second domain's page.
I have set up a webserver
profile in C:\Users\Bruce\AppData\Roaming\Mozilla\Firefox\profiles.ini
as
[General]
StartWithLastProfile=1
[Profile0]
Name=default
IsRelative=1
Path=Profiles/eracklz4.default
[Profile1]
Name=webserver
IsRelative=1
Path=Profiles/webserver.default
Default=1
So one would think that even if I didn't explicitly state that I wanted to use the webserver
profile, nevertheless, it would choose that profile and work with it. Just in case I go on to state it explicitly
var seleniumProxy = new Proxy();
seleniumProxy.HttpProxy = "localhost:" + port; // port provided by BrowserMob Proxy
...
FirefoxBinary fb = new FirefoxBinary(@" C:\Program Files (x86)\Mozilla Firefox\firefox.exe");
FirefoxProfileManager fpm = new FirefoxProfileManager();
FirefoxProfile fp = fpm.GetProfile("webserver");
fp.DeleteAfterUse = false;
fp.SetProxyPreferences(seleniumProxy);
IWebDriver wd = null;
try
{
wd = new FirefoxDriver(fb, fp);
}
catch (Exception exc)
{
System.Diagnostics.Debug.Print(exc.Message);
}
IJavaScriptExecutor js = wd as IJavaScriptExecutor;
ExistingProfiles
in the FirefoxProfileManager
lists webserver
so the fpm
var is not null.
At this point, I'm getting pretty confident that the session's data will be persisted, because when I look at fpm
in the debugger, Non-Public members
-> profiles
-> [1]
-> Value
is given as "C:\\Users\\Bruce\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles/webserver.default"
.
Having transferred the profile data from fpm
to fp
, with the .GetProfile
method, the ProfileDirectory
property of fp
reads as null
, Non-Public members
-> profileDir
is also null
and Non-Public members
-> sourceProfileDir
is "C:\\Users\\Bruce\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles/webserver.default"
By rights, therefore, one should expect passwords to be persisted to the webserver
profile, especially when one explicitly saves data to the profile with regular calls to fp.WriteToDisk();
.
However! The last time I ran the code, I got a anonymous.359f853715d5418c87c7393c629dcb1a.webdriver-profile
in C:\Users\Bruce\AppData\Local\Temp
Granted, there did appear to be some activity in the Roaming
profile. However, the password for the login page was not persisted.
What's happening here? Is session data persistable in Selenium + Firefox, or was there a design decision somewhere that no matter how you specified it, no state would be saved? Am I wanting to do something that is intrinsically forbidden?
NEXT DAY
Added the following code after reading a StackOverflow posting from 2014 discussing similar issues. However, I'm still seeing a temporary profile being created in AppData\Local\Temp
. And no changes were made to the Roaming
profile. Problem not solved.
A LITTLE LATER
Am currently experimenting with Google Chrome instead of Firefox, viz
ChromeOptions co = new ChromeOptions();
string tfn = @"C:\Temp";
co.AddArgument("user-data-dir=" + tfn);
co.Proxy = seleniumProxy;
IWebDriver wd = new ChromeDriver(co);
I'm not very confident that this will make much difference. Notably, I can't figure out how to set or get the name of the profile folder created in Temp. The above code creates a Default
folder, but there's no mention of that in the properties view in VS2015C.
FINALLY
Surprise, surprise, that actually worked. Bye bye Firefox. Hello Google Chrome. Seems there's enough session data stored to make the traversal to the second site feasible. Will probably change the directory away from Temp and have it client-specific. Kudos to the folk at ActiveState Python.