c# - Starting a specific Firefox Profile with Selenium 3 -
i trying upgrade selenium 2 selenium 3 old handling, pretty easy , fast doesn't work anymore (and documentation nonexisting seems)
this program @ moment , want open firefox driver profile: selenium
sadly doesn't work , shuts down error:
an unhandled exception of type 'system.invalidoperationexception' occurred in webdriver.dll additional information: corrupt deflate stream
this program @ moment:
public program() { firefoxoptions _options = new firefoxoptions(); firefoxprofilemanager _profileini = new firefoxprofilemanager(); firefoxdriverservice _service = firefoxdriverservice.createdefaultservice(@"c:\programme\imat\output\release\bin"); _service.firefoxbinarypath = @"c:\program files (x86)\mozilla firefox\firefox.exe"; try { if ((_options.profile = _profileini.getprofile("selenium")) == null) { console.writeline("selenium profile not found"); _profile.setpreference("network.proxy.type", 0); // disable proxy _profile = new firefoxprofile(); } } catch { throw new exception("firefox needs profile \"selenium\""); } iwebdriver driver = new firefoxdriver(_service,_options,new system.timespan(0,0,30)); driver.navigate().gotourl("ld-hybrid.fronius.com"); console.write("rtest"); } static void main(string[] args) { new program(); }
without loading profile works new firefoxdriver(_service) profile mandatory.
in selenium 2 handled code:
firefoxprofilemanager _profileini = new firefoxprofilemanager(); // use custom temporary profile try { if ((_profile = _profileini.getprofile("selenium")) == null) { console.writeline("selenium profile not found"); _profile.setpreference("network.proxy.type", 0); // disable proxy _profile = new firefoxprofile(); } } catch { throw new exception("firefox needs profile \"selenium\""); } _profile.setpreference("intl.accept_languages", _languageconfig); _driver = new firefoxdriver(_profile);
fast , simple, driver doesn't support constructor service , profile don't know how work, appreciated
this exception due bug in .net library. code generating zip of profile failing provide proper zip.
one way overcome issue overload firefoxoptions
, use archiver .net framework (system.io.compression.ziparchive) instead of faulty zipstorer
:
var options = new firefoxoptionsex(); options.profile = @"c:\users\...\appdata\roaming\mozilla\firefox\profiles\ez3krw80.selenium"; options.setpreference("network.proxy.type", 0); var service = firefoxdriverservice.createdefaultservice(@"c:\downloads", "geckodriver.exe"); var driver = new firefoxdriver(service, options, timespan.fromminutes(1));
class firefoxoptionsex : firefoxoptions { public new string profile { get; set; } public override icapabilities tocapabilities() { var capabilities = (desiredcapabilities)base.tocapabilities(); var options = (idictionary)capabilities.getcapability("moz:firefoxoptions"); var mstream = new memorystream(); using (var archive = new ziparchive(mstream, ziparchivemode.create, true)) { foreach (string file in directory.enumeratefiles(profile, "*", searchoption.alldirectories)) { string name = file.substring(profile.length + 1).replace('\\', '/'); if (name != "parent.lock") { using (stream src = file.openread(file), dest = archive.createentry(name).open()) src.copyto(dest); } } } options["profile"] = convert.tobase64string(mstream.getbuffer(), 0, (int)mstream.length); return capabilities; } }
and directory profile name:
var manager = new firefoxprofilemanager(); var profiles = (dictionary<string, string>)manager.gettype() .getfield("profiles", bindingflags.instance | bindingflags.nonpublic) .getvalue(manager); string directory; if (profiles.trygetvalue("selenium", out directory)) options.profile = directory;
Comments
Post a Comment