java - How to close IEDriverServer.exe in selenium? -
i have program opens desired url in ie using selenium driver. want close driver exe after url loading.
i used driver.quit();
results cleaning exe , closing browser. don't want close opened browser
there way achieve without using runtime.getruntime().exec("taskkill /f /im iedriverserver.exe");
?
helium automatically - if haven't called driver.quit();
when jvm terminates, helium leaves browser window open cleans driver exe.
here's how implementation works: selenium class driverservice
manages driver.exe process. need manipulate fields of correct driverservice
instance shut down exe. fields not accessible default, need use java reflection access them.
first, when starting ie, need manually pass in driverservice
instance:
internetexplorerdriverservice.builder servicebuilder = new internetexplorerdriverservice.builder().usinganyfreeport(); internetexplorerdriverservice service = servicebuilder.build(); webdriver ie = new internetexplorerdriver(service); // open url: ie.get("http://www.google.com");
to kill driver exe, first need obtain service's lock
field. not public need use reflection access it:
field lockfield = driverservice.class.getdeclaredfield("lock"); lockfield.setaccessible(true); reentrantlock lock = (reentrantlock) lockfield.get(service);
the lock
used prevent concurrent access driver exe. safely access driver exe, need wrap rest of code in try ... finally
block:
try { lock.lock(); [...] } { lock.unlock(); }
now [...]
: need access process
field, again via reflection:
field processfield = driverservice.class.getdeclaredfield("process"); processfield.setaccessible(true); commandline process = (commandline) processfield.get(service);
it may happen process
null
, in case can't anything:
if (process == null) return;
otherwise, call process.destroy()
- kills driver exe process:
process.destroy();
... , set process
null
:
processfield.set(service, null);
that's it! it's of stuff going on behind helium's startie(...)
command.
Comments
Post a Comment